目前正在深入研究 Canvas 并绘制自定义视图。我刚刚使用此代码创建了一个带边框的圆形矩形(我的
onDraw
方法):
paint.color = Color.RED
paint.style = Paint.Style.FILL
val cornerRadius = 150f
val strokeWidth = 12f
val rect = RectF(
rootView.left.toFloat() + strokeWidth,
rootView.top.toFloat() + strokeWidth,
rootView.right.toFloat() - strokeWidth,
rootView.bottom.toFloat() - strokeWidth
)
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint)
paint.style = Paint.Style.STROKE
paint.color = Color.GREEN
paint.strokeWidth = strokeWidth
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint)
我想要实现的是这个边框的动画(蛇形进度)。上次我做了类似的事情是圆形边框动画,其中
drawArc
与 Animation
对象中动态变化的角度完成了完美的工作。但是,这次我不能使用drawArc
,因为需要不同的形状。我附上一张图像,指出我想要动画开始的位置。我正在考虑创建 Path 对象,但我不知道如何从右下角(就在圆形部分之后)开始绘制。有人已经做过类似的事情了吗?
上次(对于圆圈,如前所述)我做了这样的事情:
canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint)
和动画类:
private inner class CountdownAnimation(private val roundCountdownView: RoundCountdownView): Animation() {
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
roundCountdownView.angle = 360f * interpolatedTime
roundCountdownView.requestLayout()
}
}
我想实现类似的目标,但在互联网上找不到任何有用的东西。已经发现了这个问题,但我认为通过位图来解决这个特定问题是多余的。我将不胜感激任何建议。提前致谢! :)
也许尝试将drawRoundRect变成这样的路径:
final Path path = new Path();
path.addRoundRect(rect, corners, Path.Direction.CW);
// Draw
canvas.drawPath(path, p);