本文地址:http://blog.csdn.net/u011181236/article/details/51396833
public class CustomView01 extends View {
private static final String TAG = CustomView01.class.getSimpleName();
private int fromRadius;
private int toRadius;
private int color;
private int strokeWidth;
private int duration;
private Interpolator interpolator;
private float x,y;
private int currentRadius;
private Paint paint = new Paint();
{
paint.setDither(true);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
}
public CustomView01(Context context) {
this(context, null);
}
public CustomView01(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView01(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
@TargetApi(21)
public CustomView01(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
init(0,300,Color.parseColor("#111111"),5,2000,new LinearInterpolator());
}
@Override
protected void onDraw(Canvas canvas) {
// Log.d(TAG,"onDraw ");
if (duration == 0)
return;
canvas.translate(getWidth() / 2, getHeight() / 2);
paint.setColor(color);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Paint.Style.FILL);
float perAlpha = (float) 255 / (float) (toRadius - fromRadius);
int useAlpha = (int) (255 - (perAlpha * (float) (currentRadius - fromRadius)));
paint.setAlpha(useAlpha);
canvas.drawCircle(x, y, currentRadius - strokeWidth, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX()- getWidth()/2;
y = event.getY() - getHeight()/2;
start(new Runnable() {
@Override
public void run() {
}
});
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
/**
* @param fromRadius 初始半径
* @param toRadius 目标半径
* @param color 颜色
* @param strokeWidth 圆圈的strokeWidth
* @param duration 动画时间
*/
public void init(int fromRadius, int toRadius, int color, int strokeWidth, int duration, Interpolator interpolator) {
this.fromRadius = fromRadius;
this.toRadius = toRadius;
this.color = color;
this.strokeWidth = strokeWidth;
this.duration = duration;
this.interpolator = interpolator;
}
public void start(final Runnable overCallback) {
if (duration == 0)
return;
Log.d(TAG,"start ");
ValueAnimator anim = ValueAnimator.ofInt(fromRadius, toRadius);
anim.setDuration(duration);
anim.setInterpolator(interpolator);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
currentRadius = (int) animation.getAnimatedValue();
invalidate();//不停的调用 onDraw
// Log.d(TAG,"addUpdateListener ");
}
});
anim.addListener(new Animator.AnimatorListener() {
public void onAnimationStart(Animator animation) {
}
public void onAnimationEnd(Animator animation) {
if (overCallback != null)
overCallback.run();
}
public void onAnimationCancel(Animator animation) {
}
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
}
参考文章:http://blog.csdn.net/a750457103/article/details/50629175