Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼
概述:
開(kāi)發(fā)過(guò)程中,看到有些界面用到一道光線(xiàn)在屏幕中掠過(guò)的效果,覺(jué)得挺炫的。所以查找相關(guān)資料自己實(shí)現(xiàn)了一遍。
先上個(gè)預(yù)覽圖:
實(shí)現(xiàn)思路:
簡(jiǎn)單來(lái)說(shuō)就是在一個(gè)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 * * * 翻譯過(guò)來(lái): * x0,y0為漸變起點(diǎn),x1,y1為漸變的終點(diǎn) * * colors數(shù)組為兩點(diǎn)間的漸變顏色值,positions數(shù)組取值范圍是0~1 * 傳入的colors[]長(zhǎng)度和positions[]長(zhǎng)度必須相等,一一對(duì)應(yīng)關(guān)系,否則報(bào)錯(cuò) * position傳入null則代表colors均衡分布 * * tile有三種模式 * Shader.TileMode.CLAMP: 邊緣拉伸模式,它會(huì)拉伸邊緣的一個(gè)像素來(lái)填充其他區(qū)域 * Shader.TileMode.MIRROR: 鏡像模式,通過(guò)鏡像變化來(lái)填充其他區(qū)域 * Shader.TileMode.REPEAT:重復(fù)模式,通過(guò)復(fù)制來(lái)填充其他區(qū)域 */ LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int[] colors, @Nullable float[] positions, @NonNull TileMode tile)
colors[]和positions[]的說(shuō)明結(jié)合下圖,這樣理解起來(lái)應(yīng)該就比較明朗了
回到正題,如何繪制光影。我們看到的那道光可以參照下圖:
根據(jù)分析得到我們的著色器是線(xiàn)性著色器(其他著色器請(qǐng)查詢(xún)相關(guān)api):
LinearGradient(a的x坐標(biāo), a的y坐標(biāo), c的x坐標(biāo), c的y坐標(biāo), new int[]{Color.parseColor("#00FFFFFF"), Color.parseColor("#FFFFFFFF"), Color.parseColor("#00FFFFFF")}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.CLAMP)
2.給畫(huà)筆上色。設(shè)置著色器mPaint.setShader(mLinearGradient)
3.給定一個(gè)數(shù)值范圍利用數(shù)值生成器ValueAnimator產(chǎn)生數(shù)值,監(jiān)聽(tīng)數(shù)值變化。每次回調(diào)都將該數(shù)值傳入光影的起點(diǎn)和終點(diǎn)并進(jìn)行繪制
代碼如下:
/** * 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è)) // 目的是使光影走完一遍,加一些時(shí)間緩沖,不至于每次光影移動(dòng)的間隔都那么急促 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(); } }
注意點(diǎn):
LinearGradient里參數(shù)之一:
color[]參數(shù)只能是16進(jìn)制的RGB數(shù)值,不能傳R.color.xxx。R.color.xxx雖然是int型,但拿到的是資源ID,并不是16進(jìn)制RGB
到此這篇關(guān)于Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼的文章就介紹到這了,更多相關(guān)Android 流光和光影移動(dòng)效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開(kāi)發(fā)之登錄驗(yàn)證實(shí)例教程
這篇文章主要介紹了Android開(kāi)發(fā)之登錄驗(yàn)證實(shí)現(xiàn)方法,包括發(fā)送數(shù)據(jù)、服務(wù)器端驗(yàn)證、配置文件等,需要的朋友可以參考下2014-08-08使用Android Studio創(chuàng)建OpenCV4.1.0 項(xiàng)目的步驟
這篇文章主要介紹了使用Android Studio創(chuàng)建OpenCV4.1.0 項(xiàng)目的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Eclipse+ADT+Android SDK搭建安卓開(kāi)發(fā)環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了Eclipse+ADT+Android SDK搭建安卓開(kāi)發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android 軟件自動(dòng)更新功能實(shí)現(xiàn)的方法
本篇文章小編為大家介紹,Android 軟件自動(dòng)更新功能實(shí)現(xiàn)的方法。需要的朋友參考下2013-04-04Android實(shí)現(xiàn)apk插件方式換膚的實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于Android實(shí)現(xiàn)apk插件方式換膚的實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-10-10Android多國(guó)語(yǔ)言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國(guó)語(yǔ)言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼,準(zhǔn)確獲取周邊地理位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Android動(dòng)態(tài)人臉檢測(cè)的示例代碼(臉數(shù)可調(diào))
本篇文章主要介紹了Android動(dòng)態(tài)人臉檢測(cè)的示例代碼(臉數(shù)可調(diào)),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android實(shí)現(xiàn)歡迎滑動(dòng)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歡迎滑動(dòng)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04