package com.example.androidtest; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.DecelerateInterpolator; import android.view.animation.LinearInterpolator; import android.widget.Scroller; public class CircleProgress extends View { public CircleProgress(Context context) { super(context); init(); } public CircleProgress(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CircleProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { timer=new Scroller(getContext(),new LinearInterpolator()); paint=new Paint(); paint.setStrokeWidth(circleWidth); paint.setStyle(Style.STROKE); paint.setColor(Color.BLUE); paint.setAntiAlias(true); } private Scroller timer; private int DURATION=5000;//动画持续时间 private Paint paint; private int circleWidth=50;//环的宽度 private boolean flag; private boolean isInit; private RectF arcRectf; private int centerX; private int centerY; private int radius; @Override protected void onDraw(Canvas canvas) { if (!isInit) { int min=Math.min(getWidth(), getHeight())-circleWidth; centerX=getWidth()/2; centerY=getHeight()/2; radius=(min-circleWidth-circleWidth)/2; arcRectf=new RectF(centerX-radius ,centerY-radius, centerX+radius,centerY+radius); isInit=true; } if (flag) { paint.setColor(Color.RED); canvas.drawCircle(centerX,centerY,radius,paint); paint.setColor(Color.BLUE); canvas.drawArc(arcRectf, timer.getCurrX(),timer.getCurrY(), false, paint); }else{ paint.setColor(Color.BLUE); canvas.drawCircle(centerX,centerY,radius,paint); paint.setColor(Color.RED); canvas.drawArc(arcRectf, timer.getCurrX(),timer.getCurrY(), false, paint); } } @Override public void computeScroll() { if (timer.computeScrollOffset()) { invalidate(); }else{ flag=!flag; timer.startScroll(-90, 0, 360, 360, DURATION); invalidate(); } } }