两种方式实现类似水波扩散效果,先上图为敬
自定义view实现
动画实现
自定义view实现
思路分析:通过canvas画圆,每次改变圆半径和透明度,当半径达到一定程度,再次从中心开始绘圆,达到不同层级的效果,通过不断绘制达到view扩散效果
private Paint centerPaint; //中心圆paint
private int radius = 100; //中心圆半径
private Paint spreadPaint; //扩散圆paint
private float centerX;//圆心x
private float centerY;//圆心y
private int distance = 5; //每次圆递增间距
private int maxRadius = 80; //最大圆半径
private int delayMilliseconds = 33;//扩散延迟间隔,越大扩散越慢
private List spreadRadius = new ArrayList<>();//扩散圆层级数,元素为扩散的距离
private List alphas = new ArrayList<>();//对应每层圆的透明度
style文件里自定义属性
初始化
public SpreadView(Context context) {
this(context, null, 0);
}
public SpreadView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SpreadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0);
radius = a.getInt(R.styleable.SpreadView_spread_radius, radius);
maxRadius = a.getInt(R.styleable.SpreadView_