Android水波纹扩散
TMusketeer 人气:0效果:水波纹扩散
场景:雷达、按钮点击效果、搜索等
实现:先上效果图,之前记得支付宝有一个咻一咻,当时就是水波纹效果,实现起来一共两步,第一画内圆,第二画多个外圆,不同时创建有间隔创建然后缓慢增大外圆半径,到达最远距离时移除掉,扩散时把透明度从255-1不断赋值即可。复杂在第二步,开工。
开工
1、创建RippleView.class, 继承与View
RippleView主要初始化一些数据,
onSizeChanged主要获取位置坐标
onDraw主要绘制图像,关键
public class RippleView extends View { public RippleView(Context context) { this(context, null); } public RippleView(Context context, @Nullable AttributeSet attrs) { //this(context, null, 0);//如果第二个参数写null,则自定义属性将不可用 this(context, attrs, 0); } public RippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); ........ } <br> @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); ........ } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); ........ } }
1.1特殊属性解释
alpha数组:目的是让每个外圆(扩散圆)透明度从不透明到透明(255-1)
spreadRadius:扩散圆的半径是递增的
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 = 30;//扩散延迟间隔,越大扩散越慢 private List<Integer> spreadRadius = new ArrayList<>();//扩散圆层级数,元素为扩散的距离 private List<Integer> alphas = new ArrayList<>();//对应每层圆的透明度
1.2新建attrs.xml文件(res/values)
我们需要在xml中使用自定义属性来控制初始值,如内圆半径,扩散颜色,内圆颜色等
<resources> <declare-styleable name="SpreadView"> <!--中心圆颜色--> <attr name="spread_center_color" format="color" /> <!--中心圆半径--> <attr name="spread_radius" format="integer" /> <!--扩散圆颜色--> <attr name="spread_spread_color" format="color" /> <!--扩散间距--> <attr name="spread_distance" format="integer" /> <!--扩散最大半径--> <attr name="spread_max_radius" format="integer" /> <!--扩散延迟间隔--> <attr name="spread_delay_milliseconds" format="integer" /> </declare-styleable> </resources>
在RippleView中拿到值
public RippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0); radius = typedArray.getInt(R.styleable.SpreadView_spread_radius, radius); maxRadius = typedArray.getInt(R.styleable.SpreadView_spread_max_radius, maxRadius); int centerColor = typedArray.getColor(R.styleable.SpreadView_spread_center_color, ContextCompat.getColor(context, android.R.color.holo_red_light)); int spreadColor = typedArray.getColor(R.styleable.SpreadView_spread_spread_color, ContextCompat.getColor(context, R.color.color_F71816)); distance = typedArray.getInt(R.styleable.SpreadView_spread_distance, distance); typedArray.recycle(); }
context.obtainStyledAttributes可以获取我们在xml文件的属性值,最后typedArray.recycle();释放掉,为什么释放掉这也是一个学问,自行百度。
centerColor为内圆颜色,
spreadColor扩散颜色
1.3初始化画笔
public RippleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); ..... //中心圆 centerPaint = new Paint(); centerPaint.setColor(centerColor);<br> //消除锯齿 centerPaint.setAntiAlias(true); //水波纹扩散 spreadPaint = new Paint(); spreadPaint.setColor(spreadColor); spreadPaint.setAntiAlias(true);<br> //填充和描边,上面2张图片效果不同取决于该属性 spreadPaint.setStyle(Paint.Style.STROKE); spreadPaint.setAlpha(255); //初始化第一个水波纹,扩散半径为0,透明度为255(不透明) spreadRadius.add(0); alphas.add(255); } /**在控件大小发生改变时调用。所以这里初始化会被调用一次*/<br>@Override<br>protected void onSizeChanged(int w, int h, int oldw, int oldh) {<br> super.onSizeChanged(w, h, oldw, oldh);<br> //圆心位置<br> centerX = w / 2;<br> centerY = h / 2;<br> }
2、开始绘制onDraw()
我们已经做了好前奏,剩下的就开始绘制了,首先我们要确定几个圆才能形成水波纹效果,1,2还是3,不确定那就先从一个开始,spreadRadius我们在创建画笔时已经添加了一个圆,那我们就遍历spreadRadius数组,透明度alphas[i]把值递减(255-1),spreadRadius[i]圆半径递增,圆数量超过8个就移除第1个,如果最外圆扩散半径达到最大半径时添加新扩散圆。最后通过postInvalidateDelayed(30),延迟刷新来达到扩散的样式。
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < spreadRadius.size(); i++) { //透明度 int alpha = alphas.get(i); //半径 int width = spreadRadius.get(i); spreadPaint.setAlpha(alpha); //绘制扩散的圆 canvas.drawCircle(centerX, centerY, radius + width, spreadPaint); if (alpha > 0 && width < 255) { //递减 alpha = (alpha - distance) > 0 ? (alpha - distance) : 1; alphas.set(i, alpha); //递增 spreadRadius.set(i, width + distance); } } if (spreadRadius.get(spreadRadius.size() - 1) > maxRadius) { spreadRadius.add(0); alphas.add(255); } if (spreadRadius.size() > 8) { spreadRadius.remove(0); alphas.remove(0); } //中间的圆 canvas.drawCircle(centerX, centerY, radius, centerPaint); //延迟更新,达到扩散视觉差效果 postInvalidateDelayed(delayMilliseconds); }
加载全部内容