1 先上图
2 思路,使用drawArc+属性动画 ObjectAnimation。
ObjectAnimation:
直接对任意对象的任意属性进行动画操作。
比如
- ObjectAnimator
animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f); - animator.setDuration(5000);
- animator.start();
注:要是animator.start();不行可调用View.startAnimation
通过自定义一个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() :)