是否可以将动画师附加到路径?
还有其他方法可以在 Canvas 上绘制动画线条吗?
我在发布之前搜索了这个,但我找不到任何东西。在另外两个帖子中Draw a path as animation on Canvas in Android和 How 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/