今天自定义个水波纹扩散 或者收缩 效果的view;先上效果
/**
* 自定义view实现 水波纹 效果
*/
public class MoireView extends View {
private Paint paint;
//最大半径
private int radius;
//绘制半径
private int drawRadius;
//屏幕宽高
private int w, h;
//背景颜色
private int backColor = Color.TRANSPARENT;
//水波纹颜色
private int moireColor = Color.WHITE;
//动画执行时间
private int duration = 2000;
//是否是扩散水波纹
private boolean isDiffusion = true;
public MoireView(Context context) {
super(context);
init();
}
public MoireView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(moireColor);
MyAnimation myAnimation = new MyAnimation();
myAnimation.setDuration(duration);
startAnimation(myAnimation);
}
/**
* 动画
*/
public class MyAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (isDiffusion){
//扩散水波纹效果
drawRadius = (int) (radius * interpolatedTime);
}else {
//收缩水波纹效果
drawRadius=radius-(int) (radius * interpolatedTime);
}
postInvalidate();
}
}
/**
* 测量
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
w = MeasureSpec.getSize(widthMeasureSpec);
h = MeasureSpec.getSize(heightMeasureSpec);
radius = (int) Math.sqrt(w * w + h * h) / 2;
}
/**
* 绘制
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
//设置背景颜色
canvas.drawColor(backColor);
//绘制圆
canvas.drawCircle(w / 2, h / 2, drawRadius, paint);
}
}
来源:CSDN
作者:zhudaihai
链接:https://blog.csdn.net/qq_36237165/article/details/103475340