我正在尝试使用ObjectAnimator和Path作为值的来源来沿弯曲路径设置视图的动画。我正在使用arcTo将弯曲部分附加到路径。但是由于某种原因,我无法弄清楚,该视图在Android版本7.1.1上无法翻译(在Android 8上确实可以使用)。它只是停留在屏幕上的初始位置。
我尝试了多种方法将弯曲部分附加到Path。 arcTo,addArc和其他弯曲截面方法。但是,它们似乎都无法在Android 7.1.1上运行。
以下是我使用的代码段
// creating path to animate along
val path = Path().apply {
arcTo((pivot[0] - radius).toFloat(),
(pivot[1] - radius).toFloat(),
(pivot[0] + radius).toFloat(),
(pivot[1] + radius).toFloat(),
startAngle,
sweepAngle,
true)
}
// Animating the centre of view along the path
ObjectAnimator.ofFloat(startView, startView.centerXProperty(), startView.centerYProperty(), path).apply {
duration = 2000
interpolator = LinearInterpolator()
start()
}
// To animate center instead of Top Right Corner
fun View.centerXProperty() = object : Property<View, Float>(Float::class.java, "centerX") {
override fun get(`object`: View?): Float {
if (`object` == null) return 0.0f
return `object`.x + (`object`.measuredWidth / 2)
}
override fun set(`object`: View?, value: Float?) {
if (`object` == null || value == null) return
val x = value - (measuredWidth / 2)
`object`.x = x
}
}
// To animate center instead of Top Right Corner
fun View.centerYProperty() = object : Property<View, Float>(Float::class.java, "centerY") {
override fun get(`object`: View?): Float {
if (`object` == null) return 0.0f
return `object`.y + (`object`.measuredHeight / 2)
}
override fun set(`object`: View?, value: Float?) {
if (`object` == null || value == null) return
val y = value - (measuredHeight / 2)
`object`.y = y
}
}
我基本上是试图沿围绕枢轴视图绘制的圆平移视图。但是,这似乎在Android 7.1.1上不起作用。我做错了什么吗?请建议是否有更好的方法来实现此目的。预先感谢。