绘制圆角笔划动画

时间:2020-10-20 17:45:06

标签: android kotlin canvas android-custom-view

当前正在研究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)

和Animation类:

    private inner class CountdownAnimation(private val roundCountdownView: RoundCountdownView): Animation() {

        override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
            roundCountdownView.angle = 360f * interpolatedTime
            roundCountdownView.requestLayout()
        }
    }

我想达到类似的目的,但是在互联网上找不到任何有用的东西。已经找到this SO question,但我认为通过位图完成此操作对于此特定问题而言是过大的。我将不胜感激任何建议。提前致谢! :)

0 个答案:

没有答案
相关问题