我有一个代码可以沿着圆形路径移动图像。但是我需要沿着另一条路径以五边形形状移动图像 下面给出了用于循环路径的代码
class CircularRotateAnimation
(
private val view: View // view you want to animate
, private val r: Float // radius of circle
) : Animation() {
private var cx: Float = 0.toFloat()
private var cy: Float = 0.toFloat() // center x,y position of circular path
private var prevX: Float = 0.toFloat()
private var prevY: Float = 0.toFloat() // previous x,y position of image during animation
private var prevDx: Float = 0.toFloat()
private var prevDy: Float = 0.toFloat()
override fun willChangeBounds(): Boolean {
return true
}
override fun initialize(width: Int, height: Int, parentWidth: Int, parentHeight: Int) {
val cxImage = width / 2
val cyImage = height / 2
cx = (view.left + cxImage).toFloat()
cy = (view.top + cyImage).toFloat()
// set previous position to center
prevX = cx
prevY = cy
}
protected override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
if (interpolatedTime == 0f) {
t.getMatrix().setTranslate(prevDx, prevDy)
return
}
val angleDeg = (interpolatedTime * 360f + 90) % 360
val angleRad = Math.toRadians(angleDeg.toDouble()).toFloat()
// r = radius, cx and cy = center point, a = angle (radians)
val x = (cx + r * Math.cos(angleRad.toDouble())).toFloat()
val y = (cy + r * Math.sin(angleRad.toDouble())).toFloat()
val dx = prevX - x
val dy = prevY - y
prevX = x
prevY = y
prevDx = dx
prevDy = dy
t.matrix.setTranslate(dx, dy)
}
}
如何使用applyTransformation覆盖方法绘制五边形路径。 或其他任何方法绘制五角形动画路径。