Android实现流光效果、光影移动效果

概述:

开发过程中,看到有些界面用到一道光线在屏幕中掠过的效果,觉得挺炫的。所以查找相关资料自己实现了一遍。

先上个预览图:

在这里插入图片描述

实现思路:

简单来说就是在一个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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值