Chi*_*nux 5 animation android canvas path
我想在 android 画布上为单个 Path 设置动画。
public class MyView extends View {
private Path paths[];
protected void onDraw( Canvas canvas ) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth( 8 );
paint.setColor(Color.BLUE);
Path path = new Path();
path.moveTo(75, 11);
path.quadTo(62, 87, 10, 144);
canvas.drawPath( path, paint );
paths[0] = path;
path.reset();
path.moveTo(50, 100);
path.lineTo(150, 200);
canvas.drawPath( path, paint );
paths[1] = path;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有了路径 [],我想分别为每个路径设置动画。我希望它改变 alpha 就像它在增长一样。起初只有一个小点,然后它长成一条线,重复。
可以做到吗?
如何?
小智 0
我将添加一个范围从 0 到 1 的新浮点数,以保留当前动画百分比并使用它来设置当前的 alpha
public class MyView extends View {
private Path paths[];
private float mAnimPercentage = 0.0f;
private static final int clip(int value){ //forces the value to be between 0 and 255
return Math.max(0, Math.min(255, value));
}
protected void onDraw( Canvas canvas ) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth( 8 );
paint.setColor(Color.BLUE);
Path path = new Path();
path.moveTo(75, 11);
path.quadTo(62, 87, 10, 144);
//edited here
paint.setAlpha(clip(mAnimPercentage*255*2));
canvas.drawPath( path, paint );
paths[0] = path;
path.reset();
path.moveTo(50, 100);
path.lineTo(150, 200);
//edited here
paint.setAlpha(clip(-127 + mAnimPercentage*255*2)); //the biggest is the negative value, the latter this path will show, the biggest is the number multiplied by mAnimPercentage, the fastest this path will get completely opaque
canvas.drawPath( path, paint );
paths[1] = path;
mAnimPercentage+= 0.01; //FIXME this is for TEST only, you should update it with an animator
postInvalidate();
}
}
Run Code Online (Sandbox Code Playgroud)