概述:
开发过程中,看到有些界面用到一道光线在屏幕中掠过的效果,觉得挺炫的。所以查找相关资料自己实现了一遍。
先上个预览图:
实现思路:
简单来说就是在一个view中绘制好一道光影,并不断改变光影在view中的位置。
1.首先我们先了解一下光影怎么绘制
在了解如何绘制之前,我们先看一下lineargradient的构造方法
/** * create a shader that draws a linear gradient along a line. * * @param x0 the x-coordinate for the start of the gradient line * @param y0 the y-coordinate for the start of the gradient line * @param x1 the x-coordinate for the end of the gradient line * @param y1 the y-coordinate for the end of the gradient line * @param colors the srgb colors to be distributed along the gradient line * @param positions may be null. the relative positions [0..1] of * each corresponding color in the colors array. if this is null, * the the colors are distributed evenly along the gradient line. * @param tile the shader tiling mode * * * 翻译过来: * x0,y0为渐变起点,x1,y1为渐变的终点 * * colors数组为两点间的渐变颜色值,positions数组取值范围是0~1 * 传入的colors[]长度和positions[]长度必须相等,一一对应关系,否则报错 * position传入null则代表colors均衡分布 * * tile有三种模式 * shader.tilemode.clamp: 边缘拉伸模式,它会拉伸边缘的一个像素来填充其他区域 * shader.tilemode.mirror: 镜像模式,通过镜像变化来填充其他区域 * shader.tilemode.repeat:重复模式,通过复制来填充其他区域 */ lineargradient(float x0, float y0, float x1, float y1, @nonnull @colorint int[] colors, @nullable float[] positions, @nonnull tilemode tile)
colors[]和positions[]的说明结合下图,这样理解起来应该就比较明朗了
回到正题,如何绘制光影。我们看到的那道光可以参照下图:
根据分析得到我们的着色器是线性着色器(其他着色器请查询相关api):
lineargradient(a的x坐标, a的y坐标, c的x坐标, c的y坐标, new int[]{color.parsecolor("#00ffffff"), color.parsecolor("#ffffffff"), color.parsecolor("#00ffffff")}, new float[]{0f, 0.5f, 1f}, shader.tilemode.clamp)
2.给画笔上色。设置着色器mpaint.setshader(mlineargradient)
3.给定一个数值范围利用数值生成器valueanimator产生数值,监听数值变化。每次回调都将该数值传入光影的起点和终点并进行绘制
代码如下:
/** * author: caoyb * created on: 2021/12/20 15:13 * description: */ public class configloadingview extends view { private paint mpaint; private path mpath; private lineargradient mlineargradient; private valueanimator mvalueanimator; public configloadingview(context context) { this(context, null); } public configloadingview(context context, @nullable attributeset attrs) { this(context, attrs, 0); } public configloadingview(context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } private void init() { mpaint = new paint(); mpath = new path(); } private void initpointandanimator(int w, int h) { point point1 = new point(0, 0); point point2 = new point(w, 0); point point3 = new point(w, h); point point4 = new point(0, h); mpath.moveto(point1.x, point1.y); mpath.lineto(point2.x, point2.y); mpath.lineto(point3.x, point3.y); mpath.lineto(point4.x, point4.y); mpath.close(); // 斜率k float k = 1f * h / w; // 偏移 float offset = 1f * w / 2; // 0f - offset * 2 为数值左边界(屏幕外左侧), w + offset * 2为数值右边界(屏幕外右侧) // 目的是使光影走完一遍,加一些时间缓冲,不至于每次光影移动的间隔都那么急促 mvalueanimator = valueanimator.offloat(0f - offset * 2, w + offset * 2); mvalueanimator.setrepeatcount(-1); mvalueanimator.setinterpolator(new linearinterpolator()); mvalueanimator.setduration(1500); mvalueanimator.addupdatelistener(new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator animation) { float value = (float) animation.getanimatedvalue(); mlineargradient = new lineargradient(value, k * value, value + offset, k * (value + offset), new int[]{color.parsecolor("#00ffffff"), color.parsecolor("#1affffff"), color.parsecolor("#00ffffff")}, null, shader.tilemode.clamp); mpaint.setshader(mlineargradient); invalidate(); } }); mvalueanimator.start(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); int widthsize = measurespec.getsize(widthmeasurespec); int heightsize = measurespec.getsize(heightmeasurespec); initpointandanimator(widthsize, heightsize); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawpath(mpath, mpaint); } @override protected void ondetachedfromwindow() { super.ondetachedfromwindow(); mvalueanimator.cancel(); } }
注意点:
lineargradient里参数之一:
color[]参数只能是16进制的rgb数值,不能传r.color.xxx。r.color.xxx虽然是int型,但拿到的是资源id,并不是16进制rgb
到此这篇关于android实现流光和光影移动效果代码的文章就介绍到这了,更多相关android 流光和光影移动效果内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!