Android: animate alpha of one outline on canvas

I would like to animate one Path on an android canvas.

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

Now I have the ways [], I would like to animate each separately. I would like him to change alpha, as if it were growing. At first there is only a small point, then it grows into a line, repeats.

Can this be done?

How?

+4
source share
1 answer

I would add a new float from 0 to 1, which saves the current percentage of animation and uses it to set the current 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(); } } 
0
source

Source: https://habr.com/ru/post/1335020/


All Articles