網站首頁 編程語言 正文
本文實例為大家分享了Android顏色漸變動畫效果的實現代碼,供大家參考,具體內容如下
前言
案例效果的實現比較簡單,利用Android自帶的顏色插值器ArgbEvaluator()進行計算即可,而本文的重點就是講講插值器。
效果圖:
一、Android中插值器TypeEvaluator
TypeEvaluator是一個接口,在開發中可以自定義該接口實例,利用ValueAnimator的setEvaluator(TypeEvaluator)方法來控制動畫的更新計算表達式。在日常開發中,不可能只是需要操縱單一數值的變化,如果需要同時操縱對象的多個屬性,如定義動畫的x,y移動的坐標等,那就需要對TypeEvaluator有所了解了。
二、案例效果實現
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轉化成線性
? ? ? ? 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);
? ? ? ? //在線性空間中計算插值的顏色
? ? ? ? float a = startA + fraction * (endA - startA);
? ? ? ? float r = startR + fraction * (endR - startR);
? ? ? ? float g = startG + fraction * (endG - startG);
? ? ? ? float b = startB + fraction * (endB - startB);
? ? ? ? //轉換回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.根據ArgbEvaluator的實現來自定義一個顏色插值器
public class MyColorEvaluator implements TypeEvaluator
接下來換一種顏色的計算方式,在本人看相關api的過程中,發現Color中有colorToHSV和HSVToColor的方法,于是在網上找了一個HVS的計算方式。(以下代碼來源于網絡)。
@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);
? ? ? ? // 計算當前動畫完成度(fraction)所對應的顏色值
? ? ? ? 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);
? ? ? ? // 計算當前動畫完成度(fraction)所對應的顏色值
? ? ? ? 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);
? ? }
}
原文鏈接:https://blog.csdn.net/qq_42761395/article/details/119918232
相關推薦
- 2022-05-06 教你使用zabbix?api批量添加數百臺監控主機的方法_zabbix
- 2023-01-08 基于C#實現屏幕取色器的示例詳解_C#教程
- 2022-09-10 pycharm下載包的時候出現?no?information?available的解決_python
- 2022-08-07 Android?AccessibilityService?事件分發原理分析總結_Android
- 2022-08-21 Caffe卷積神經網絡solver及其配置詳解_python
- 2022-07-23 .Net創建型設計模式之簡單工廠模式(Simple?Factory)_基礎應用
- 2022-10-03 pytest文檔內置fixture的request詳情_python
- 2022-06-01 Python使用list列表和tuple元組的方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支