網(wǎng)站首頁 編程語言 正文
概述:
開發(fā)過程中,看到有些界面用到一道光線在屏幕中掠過的效果,覺得挺炫的。所以查找相關(guān)資料自己實現(xiàn)了一遍。
先上個預(yù)覽圖:
實現(xiàn)思路:
簡單來說就是在一個view中繪制好一道光影,并不斷改變光影在view中的位置。
1.首先我們先了解一下光影怎么繪制
在了解如何繪制之前,我們先看一下LinearGradient的構(gòu)造方法
/** * 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數(shù)組為兩點間的漸變顏色值,positions數(shù)組取值范圍是0~1 * 傳入的colors[]長度和positions[]長度必須相等,一一對應(yīng)關(guān)系,否則報錯 * position傳入null則代表colors均衡分布 * * tile有三種模式 * Shader.TileMode.CLAMP: 邊緣拉伸模式,它會拉伸邊緣的一個像素來填充其他區(qū)域 * Shader.TileMode.MIRROR: 鏡像模式,通過鏡像變化來填充其他區(qū)域 * Shader.TileMode.REPEAT:重復(fù)模式,通過復(fù)制來填充其他區(qū)域 */ LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors, @Nullable float[] positions, @NonNull TileMode tile)
colors[]和positions[]的說明結(jié)合下圖,這樣理解起來應(yīng)該就比較明朗了
回到正題,如何繪制光影。我們看到的那道光可以參照下圖:
根據(jù)分析得到我們的著色器是線性著色器(其他著色器請查詢相關(guān)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.給畫筆上色。設(shè)置著色器mPaint.setShader(mLinearGradient)
3.給定一個數(shù)值范圍利用數(shù)值生成器ValueAnimator產(chǎn)生數(shù)值,監(jiān)聽數(shù)值變化。每次回調(diào)都將該數(shù)值傳入光影的起點和終點并進行繪制
代碼如下:
/** * 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 為數(shù)值左邊界(屏幕外左側(cè)), w + offset * 2為數(shù)值右邊界(屏幕外右側(cè)) // 目的是使光影走完一遍,加一些時間緩沖,不至于每次光影移動的間隔都那么急促 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里參數(shù)之一:
color[]參數(shù)只能是16進制的RGB數(shù)值,不能傳R.color.xxx。R.color.xxx雖然是int型,但拿到的是資源ID,并不是16進制RGB
原文鏈接:https://blog.csdn.net/weixin_44270195/article/details/122069448
相關(guān)推薦
- 2022-12-29 React使用公共文件夾public問題_React
- 2022-07-07 Python實現(xiàn)8個概率分布公式的方法詳解_python
- 2022-12-06 React.memo?和?useMemo?的使用問題小結(jié)_React
- 2023-03-15 k8s編排之StatefulSet知識點詳解一_云其它
- 2022-04-15 Go?interface{}?轉(zhuǎn)切片類型的實現(xiàn)方法_Golang
- 2023-03-23 python實現(xiàn)簡易連點器_python
- 2022-12-07 C++AVL樹4種旋轉(zhuǎn)詳講(左單旋、右單旋、左右雙旋、右左雙旋)_C 語言
- 2022-06-18 Elasticsearch之文檔批量操作示例_其它綜合
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支