android - 如何在 Android Canvas 上为路径设置动画

标签 android animation canvas path

是否可以将动画师附加到路径?

还有其他方法可以在 Canvas 上绘制动画线条吗?

我在发布之前搜索了这个,但我找不到任何东西。在另外两个帖子中Draw a path as animation on Canvas in AndroidHow to draw a path on an Android Canvas with animation有一些对我不起作用的解决方案。

我在 onDraw 方法中发布我的代码来指定我到底想要什么。

paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.BLACK);

    Path path = new Path();
    path.moveTo(10, 50);   // THIS TRANSFORMATIONS TO BE ANIMATED!!!!!!!!
    path.lineTo(40, 50);
    path.moveTo(40, 50);
    path.lineTo(50, 40);
    // and so on...

    canvas.drawPath(path, paint);

最佳答案

您可以按时间变换您的 Canvas ,即:

class MyView extends View {

    int framesPerSecond = 60;
    long animationDuration = 10000; // 10 seconds

    Matrix matrix = new Matrix(); // transformation matrix

    Path path = new Path();       // your path
    Paint paint = new Paint();    // your paint

    long startTime;

    public MyView(Context context) {
        super(context);

        // start the animation:
        this.startTime = System.currentTimeMillis();
        this.postInvalidate(); 
    }

    @Override
    protected void onDraw(Canvas canvas) {

        long elapsedTime = System.currentTimeMillis() - startTime;

        matrix.postRotate(30 * elapsedTime/1000);        // rotate 30° every second
        matrix.postTranslate(100 * elapsedTime/1000, 0); // move 100 pixels to the right
        // other transformations...

        canvas.concat(matrix);        // call this before drawing on the canvas!!

        canvas.drawPath(path, paint); // draw on canvas

        if(elapsedTime < animationDuration)
            this.postInvalidateDelayed( 1000 / framesPerSecond);
    }

}

关于android - 如何在 Android Canvas 上为路径设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616035/

相关文章:

javascript - Cropit 将 Canvas 图像上传到 NodeJS

java - Resources.getSystem().openRawResource(random.txt) 返回 Resource$NotFoundException

python tkinter canvas create_line 函数返回值?

android - 适用于 Android 的烛台图表组件

android - 在具有位置依赖性的 View 上转换动画

css - AngularJS 没有在 plunker 中加载

javascript - Canvas 雪动画不起作用

javascript - putImageData 比 drawImage 快吗?

android - 使用 "copy code"按钮发送包含验证码的短信

android - ScrollView android中的不可滚动 ListView