天天看点

圆形进度条

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

继续阅读