Android實(shí)現(xiàn)顏色漸變動(dòng)畫效果
本文實(shí)例為大家分享了Android顏色漸變動(dòng)畫效果的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
前言
案例效果的實(shí)現(xiàn)比較簡(jiǎn)單,利用Android自帶的顏色插值器ArgbEvaluator()進(jìn)行計(jì)算即可,而本文的重點(diǎn)就是講講插值器。
效果圖:
一、Android中插值器TypeEvaluator
TypeEvaluator是一個(gè)接口,在開(kāi)發(fā)中可以自定義該接口實(shí)例,利用ValueAnimator的setEvaluator(TypeEvaluator)方法來(lái)控制動(dòng)畫的更新計(jì)算表達(dá)式。在日常開(kāi)發(fā)中,不可能只是需要操縱單一數(shù)值的變化,如果需要同時(shí)操縱對(duì)象的多個(gè)屬性,如定義動(dòng)畫的x,y移動(dòng)的坐標(biāo)等,那就需要對(duì)TypeEvaluator有所了解了。
二、案例效果實(shí)現(xiàn)
1.利用Android自帶的顏色插值器ArgbEvaluator
ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE); ? ? ? ? colorAnim.setDuration(4000); ? ? ? ? colorAnim.setEvaluator(new ArgbEvaluator()); ? ? ? ? colorAnim.setRepeatCount(ValueAnimator.INFINITE); ? ? ? ? colorAnim.setRepeatMode(ValueAnimator.REVERSE); ? ? ? ? colorAnim.start();
2.看看Android自帶顏色插值器ArgbEvaluator核心代碼
@Override ? ? public Object evaluate(float fraction, Object startValue, Object endValue) { ? ? ? ? int startInt = (Integer) startValue; ? ? ? ? float startA = ((startInt >> 24) & 0xff) / 255.0f; ? ? ? ? float startR = ((startInt >> 16) & 0xff) / 255.0f; ? ? ? ? float startG = ((startInt >> ?8) & 0xff) / 255.0f; ? ? ? ? float startB = ( startInt ? ? ? ?& 0xff) / 255.0f; ? ? ? ? int endInt = (Integer) endValue; ? ? ? ? float endA = ((endInt >> 24) & 0xff) / 255.0f; ? ? ? ? float endR = ((endInt >> 16) & 0xff) / 255.0f; ? ? ? ? float endG = ((endInt >> ?8) & 0xff) / 255.0f; ? ? ? ? float endB = ( endInt ? ? ? ?& 0xff) / 255.0f; ? ? ? ? // 將sRGB轉(zhuǎn)化成線性 ? ? ? ? startR = (float) Math.pow(startR, 2.2); ? ? ? ? startG = (float) Math.pow(startG, 2.2); ? ? ? ? startB = (float) Math.pow(startB, 2.2); ? ? ? ? endR = (float) Math.pow(endR, 2.2); ? ? ? ? endG = (float) Math.pow(endG, 2.2); ? ? ? ? endB = (float) Math.pow(endB, 2.2); ? ? ? ? //在線性空間中計(jì)算插值的顏色 ? ? ? ? float a = startA + fraction * (endA - startA); ? ? ? ? float r = startR + fraction * (endR - startR); ? ? ? ? float g = startG + fraction * (endG - startG); ? ? ? ? float b = startB + fraction * (endB - startB); ? ? ? ? //轉(zhuǎn)換回sRGB在[0..255]范圍 ? ? ? ? a = a * 255.0f; ? ? ? ? r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f; ? ? ? ? g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f; ? ? ? ? b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f; ? ? ? ? return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b); ? ? }
3.根據(jù)ArgbEvaluator的實(shí)現(xiàn)來(lái)自定義一個(gè)顏色插值器
public class MyColorEvaluator implements TypeEvaluator
接下來(lái)?yè)Q一種顏色的計(jì)算方式,在本人看相關(guān)api的過(guò)程中,發(fā)現(xiàn)Color中有colorToHSV和HSVToColor的方法,于是在網(wǎng)上找了一個(gè)HVS的計(jì)算方式。(以下代碼來(lái)源于網(wǎng)絡(luò))。
@Override ? ? public Integer evaluate(float fraction, Integer startValue, Integer endValue) { ? ? ? ? Color.colorToHSV(startValue,startHsv); ? ? ? ? Color.colorToHSV(endValue,endHsv); ? ? ? ? int alpha = startValue >> 24 + (int) ((endValue >> 24 - startValue >> 24) * fraction); ? ? ? ? // 計(jì)算當(dāng)前動(dòng)畫完成度(fraction)所對(duì)應(yīng)的顏色值 ? ? ? ? if (endHsv[0] - startHsv[0] > 180) { ? ? ? ? ? ? endHsv[0] -= 360; ? ? ? ? } else if (endHsv[0] - startHsv[0] < -180) { ? ? ? ? ? ? endHsv[0] += 360; ? ? ? ? } ? ? ? ? outHsv[0] = startHsv[0] + (endHsv[0] - startHsv[0]) * fraction; ? ? ? ? if (outHsv[0] > 360) { ? ? ? ? ? ? outHsv[0] -= 360; ? ? ? ? } else if (outHsv[0] < 0) { ? ? ? ? ? ? outHsv[0] += 360; ? ? ? ? } ? ? ? ? outHsv[1]=startHsv[1]+(endHsv[1]-startHsv[1])*fraction; ? ? ? ? outHsv[2]=startHsv[2]+(endHsv[2]-startHsv[2])*fraction; ? ? ? ? return Color.HSVToColor(alpha,outHsv); ? ? }
4.使用自己定義的顏色插值器MyColorEvaluator
ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE); ? ? ? ? colorAnim.setDuration(4000); ? ? ? ? colorAnim.setEvaluator(new MyColorEvaluator()); ? ? ? ? colorAnim.setRepeatCount(ValueAnimator.INFINITE); ? ? ? ? colorAnim.setRepeatMode(ValueAnimator.REVERSE); ? ? ? ? colorAnim.start();
三、源碼
ColorGradient.java:
public class ColorGradient extends View { ? ? public ColorGradient(Context context) { ? ? ? ? super(context); ? ? } ? ? public ColorGradient(Context context, @Nullable AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? ? ? animation(); ? ? } ? ? public ColorGradient(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? } ? ? private void animation(){ ? ? ? ? ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE); ? ? ? ? colorAnim.setDuration(4000); ? ? ? ? colorAnim.setEvaluator(new MyColorEvaluator()); ? ? ? ? colorAnim.setRepeatCount(ValueAnimator.INFINITE); ? ? ? ? colorAnim.setRepeatMode(ValueAnimator.REVERSE); ? ? ? ? colorAnim.start(); ? ? } ? ?? }
MyColorEvaluator.java:
public class MyColorEvaluator implements TypeEvaluator<Integer> { ? ? float[] startHsv=new float[3]; ? ? float[] endHsv=new float[3]; ? ? float[] outHsv=new float[3]; ? ? @Override ? ? public Integer evaluate(float fraction, Integer startValue, Integer endValue) { ? ? ? ? Color.colorToHSV(startValue,startHsv); ? ? ? ? Color.colorToHSV(endValue,endHsv); ? ? ? ? int alpha = startValue >> 24 + (int) ((endValue >> 24 - startValue >> 24) * fraction); ? ? ? ? // 計(jì)算當(dāng)前動(dòng)畫完成度(fraction)所對(duì)應(yīng)的顏色值 ? ? ? ? if (endHsv[0] - startHsv[0] > 180) { ? ? ? ? ? ? endHsv[0] -= 360; ? ? ? ? } else if (endHsv[0] - startHsv[0] < -180) { ? ? ? ? ? ? endHsv[0] += 360; ? ? ? ? } ? ? ? ? outHsv[0] = startHsv[0] + (endHsv[0] - startHsv[0]) * fraction; ? ? ? ? if (outHsv[0] > 360) { ? ? ? ? ? ? outHsv[0] -= 360; ? ? ? ? } else if (outHsv[0] < 0) { ? ? ? ? ? ? outHsv[0] += 360; ? ? ? ? } ? ? ? ? outHsv[1]=startHsv[1]+(endHsv[1]-startHsv[1])*fraction; ? ? ? ? outHsv[2]=startHsv[2]+(endHsv[2]-startHsv[2])*fraction; ? ? ? ? return Color.HSVToColor(alpha,outHsv); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用TransitionDrawable漸變切換多張圖片
- Android?App頁(yè)面滑動(dòng)標(biāo)題欄顏色漸變?cè)斀?/a>
- Android簡(jiǎn)單實(shí)現(xiàn)一個(gè)顏色漸變的ProgressBar的方法
- Android Textview實(shí)現(xiàn)顏色漸變滾動(dòng)效果
- Android 微信6.1 tab欄圖標(biāo)和字體顏色漸變的實(shí)現(xiàn)
- android中實(shí)現(xiàn)背景圖片顏色漸變方法
- Kotlin使用TransitionDrawable實(shí)現(xiàn)顏色漸變效果流程講解
相關(guān)文章
Android沉浸式狀態(tài)欄實(shí)現(xiàn)示例
本篇文章主要介紹了Android沉浸式狀態(tài)欄實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Android開(kāi)發(fā)之菜單(menu)用法實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)之菜單(menu)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android菜單的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-03-03Android實(shí)現(xiàn)歌詞漸變色和進(jìn)度的效果
這篇文章主要介紹了Android實(shí)現(xiàn)歌詞漸變色和進(jìn)度的效果的相關(guān)資料,需要的朋友可以參考下2016-03-03Android控制閃光燈的方法(打開(kāi)與關(guān)閉)
這篇文章主要介紹了Android控制閃光燈的方法,可實(shí)現(xiàn)閃光燈打開(kāi)與關(guān)閉的效果,涉及Android操作Camera拍照閃光燈的相關(guān)技巧,需要的朋友可以參考下2016-01-01Android開(kāi)發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊(cè)界面功能示例
這篇文章主要介紹了Android開(kāi)發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊(cè)界面功能,涉及Android使用intent傳值及界面布局等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Android 應(yīng)用指定瀏覽器開(kāi)發(fā)實(shí)例
這篇文章主要介紹了Android 應(yīng)用指定瀏覽器開(kāi)發(fā)實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互
本篇文章主要介紹了Android中Retrofit 2.0直接使用JSON進(jìn)行數(shù)據(jù)交互,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08