如何在画布上动画的路径 - 机器人 [英] How to animate a path on canvas - android

查看:147
本文介绍了如何在画布上动画的路径 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能的漫画家附加到路径?有没有画在画布上的动画线条任何其他方式?我搜索这个我张贴过,但没有什么这个问题。在这里和<一个其他两个职位 href="http://stackoverflow.com/questions/12037709/how-to-draw-a-path-on-an-android-canvas-with-animation">here有走动的解决方案,不适合我。 谢谢!

Is possible to attach an animator to a path? Is there any other way to draw on canvas animated lines? I searched this before i post , but there is nothing about this. In two other posts here and here there are walk around solutions and does not fit to me. Thank you!

我后我的code中的OnDraw的方法内指定究竟我想要的。

I post my code inside the onDraw method to specify what exactly i want.

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);

任何想法????

Any ideas????

推荐答案

您可以通过时间改变你的画布,即:

You can transform your canvas by time, i.e:

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);
    }

}

这篇关于如何在画布上动画的路径 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆