Canvas路径动画怎么做?

How to do Canvas Path animation?

我正在尝试为我的 Canvas 添加动画。我有一个 ArrayList of Points,我用它来绘制路径。

我的代码:

for (int i = 0; i < points.size(); i++) {

            Point point = points.get(i);

            if (pId == 0) {
                path.moveTo(point.x, point.y);                    
            } else {
                float midX = (prevPoint.x + point.x) / 2;
                float midY = (prevPoint.y + point.y) / 2;

                if (pId == 1) {
                    path.lineTo(midX, midY);
                } else {
                    path.quadTo(prevPoint.x, prevPoint.y, midX, midY);
                }
              pId++;
            }
            prevPoint = point;
}

这是上面的代码。所以,现在我想用动画来绘制路径。

请给我一些解决方案。

您需要创建 class 扩展视图并通过 animationValue 绘制。

public class XXX extends View {

    //...

    int valueAnimation;

    void animationStart() {

        ValueAnimator valueAnimatorLoading = ValueAnimator.ofInt(startValue, endValue);
        valueAnimatorLoading.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                valueAnimation = (int) (Integer) valueAnimator.getAnimatedValue(); //factor for use in onDraw()
                invalidate();// call onDraw
            }
        });
        valueAnimatorLoading.start();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //draw in canvas by valueAnimation
    }

}