天天看點

圓形進度條

1 先上圖

圓形進度條

2 思路,使用drawArc+屬性動畫 ObjectAnimation。 ObjectAnimation: 直接對任意對象的任意屬性進行動畫操作。 比如

  1. ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
  2. animator.setDuration(5000);  
  3. animator.start();  注:要是animator.start();不行可調用View.startAnimation

ofFloat第一個參數要求傳入一個object對象,我們想要對哪個對象進行動畫操作就傳入什麼,這裡我傳入了一個textview。第二個參數是想要對該對象的哪個屬性進行動畫操作,由于我們想要改變TextView的不透明度,是以這裡傳入"alpha"。後面的參數就是不固定長度了,想要完成什麼樣的動畫就傳入什麼值,這裡傳入的值就表示将TextView從正常變換成全透明,再從全透明變換成正常。屬性動畫會調用TextView中的setAlpha方法。以達到修改屬性的目的。

通過自定義一個View,在View中設定一個參數 progress(表示圓弧的角度),在建立一個setProgress(int progress)方法,被 ObjectAnimation,調用。

代碼: 自定義View代碼如下:

public class MyCircleProgressAnimationView extends View {
private int progress;

public MyCircleProgressAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public int getProgress() {
return progress;
}

public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
// 建立屬性動畫會調用setProgress方法
public void drawCicleAnimation(int swapArc) {
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this, "progress",
1, swapArc);
objectAnimator.setDuration(5000);
// 加速器,從慢到快到再到慢
objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
objectAnimator.start();
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true); // 設定畫筆為無鋸齒
paint.setColor(Color.GREEN); // 設定畫筆顔色
canvas.drawColor(Color.WHITE); // 白色背景
paint.setStrokeWidth((float) 3.0); // 線寬
paint.setStyle(Style.STROKE);
System.out.println(this.getMeasuredWidth() + ":"
+ this.getMeasuredHeight());
;
canvas.drawArc(new RectF(10f, 10f, (float)this.getMeasuredWidth()-10f,(float)this
.getMeasuredHeight()-10f)
, 0, progress, false, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
System.out.println("onMeasure"+widthMeasureSpec + ":"
+ heightMeasureSpec);
//設定為正方形
heightMeasureSpec=widthMeasureSpec;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
};
           

另一種思路 Handler+自定義View(canas.drawArc() :)

參考 屬性動畫完全解析: http://blog.csdn.net/guolin_blog/article/details/43536355

繼續閱讀