2011-01-11 134 views
4

我想在Android畫布上爲單個路徑創建動畫。Android:在畫布上爲單個路徑的Alpha創建動畫

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

現在我有路徑[],我想單獨爲每一個動畫。我希望它能夠像增長一樣改變alpha。起初只有一點點,然後它成長爲一條線,重複。

可以這樣做嗎?

怎麼樣?

+0

你解決呢? – 2011-04-09 20:03:30

回答

0

我想補充一個新的浮動範圍從0到1,保持當前動畫百分比,並用它來設置當前阿爾法

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