Android 动画 ValueAnimator(三)

2024-01-11 00:08

本文主要是介绍Android 动画 ValueAnimator(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第二篇我们讲了插值器,现在我们来看看另外一个东西,Evaluator。

Evaluator其实就是一个转换器,他能把小数进度转换成对应的数值位置

我们之前可以在监听器中拿到当前的进度值

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator valueAnimator) {int value = (int) valueAnimator.getAnimatedValue();System.out.println("==========value: "+value);}});animator.start();

那么这个值是怎么计算出来的呢?这就要靠Evaluator了,;Evaluator就是将从加速器返回的数字进度转成对应的数字值。
那这时候有朋友会问,之前也没有设置过插值器和Evaluator,那么怎么还能正常拿到结果呢?是因为ofInt和ofFloat都是系统直接提供的函数,所以在使用时都会有默认的插值器和Evaluator来使用的,不指定则使用默认的;对于Evaluator而言,ofInt()的默认Evaluator当然是IntEvaluator;而FloatEvalutar默认的则是FloatEvalutor。

下面我们看看系统的Evaluator是怎么实现的

public class IntEvaluator implements TypeEvaluator<Integer> {/*** This function returns the result of linearly interpolating the start and end values, with* <code>fraction</code> representing the proportion between the start and end values. The* calculation is a simple parametric calculation: <code>result = x0 + t * (v1 - v0)</code>,* where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,* and <code>t</code> is <code>fraction</code>.** @param fraction   The fraction from the starting to the ending values* @param startValue The start value; should be of type <code>int</code> or*                   <code>Integer</code>* @param endValue   The end value; should be of type <code>int</code> or <code>Integer</code>* @return A linear interpolation between the start and end values, given the*         <code>fraction</code> parameter.*/public Integer evaluate(float fraction, Integer startValue, Integer endValue) {int startInt = startValue;
        return (int)(startInt + fraction * (endValue - startInt));}
}

我们重点看evaluate这个方法,第一个参数fraction代表开始到结束之间的百分比,后面两个参数分别代表起始值和结束值。
当前值 = 起始值+百分比*(结束值-起始值)
比如我们定义范围为(0,400),那么
当前值 = 0+百分比*(400),可以看到当前值是不断增大的。意思就是当前位置距离起始位置越来越远。

看懂了Evaluator的实现,我们来自定义一个Evaluator

public class AddEvaluator implements TypeEvaluator<Integer> {@Overridepublic Integer evaluate(float fraction, Integer startValue, Integer endValue) {return (int)(200+startValue+fraction*(endValue - startValue));}
}
  ValueAnimator animator = ValueAnimator.ofInt(0,400);animator.setEvaluator(new AddEvaluator());animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {int value = (int) animation.getAnimatedValue();textView.layout(value,value,value+textView.getWidth(),value+textView.getHeight());}});animator.setDuration(500);animator.start();

我们可以看到,在原先的基础上增加了200.下面我们看一下效果图:

未使用Evaluator:
这里写图片描述
使用以后:
这里写图片描述

我们可以看到,使用自定义的Evaluator以后,textview的位置比原先增加了200.

下面我们再来实现一个之前用插值器实现过的效果
这里写图片描述

当前的值是随着进度的增加而减小的。

public class ReverseEvaluator implements TypeEvaluator<Integer> {@Overridepublic Integer evaluate(float fraction, Integer startValue, Integer endValue) {return (int)(endValue - fraction*(endValue - startValue));}
}

系统除了有IntEvaluator和FloatEvaluator之外,还有一个ArgbEvaluator,接下来我们看看到底是干啥的。

从名字上我们可以看出这个Evaluator和颜色有关,是的,就是用来变换颜色的,我们先看一下如何使用,然后去看源码。

这里写图片描述

从黄色变为蓝色

  ValueAnimator animator = ValueAnimator.ofInt(0xffffff00,0xff0000ff);animator.setEvaluator(new ArgbEvaluator());animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {int value = (int) animation.getAnimatedValue();textView.setBackgroundColor(value);}});animator.setDuration(1500);animator.start();
public class ArgbEvaluator implements TypeEvaluator {private static final ArgbEvaluator sInstance = new ArgbEvaluator();/*** Returns an instance of <code>ArgbEvaluator</code> that may be used in* {@link ValueAnimator#setEvaluator(TypeEvaluator)}. The same instance may* be used in multiple <code>Animator</code>s because it holds no state.* @return An instance of <code>ArgbEvalutor</code>.** @hide*/public static ArgbEvaluator getInstance() {return sInstance;}/*** This function returns the calculated in-between value for a color* given integers that represent the start and end values in the four* bytes of the 32-bit int. Each channel is separately linearly interpolated* and the resulting calculated values are recombined into the return value.** @param fraction The fraction from the starting to the ending values* @param startValue A 32-bit int value representing colors in the* separate bytes of the parameter* @param endValue A 32-bit int value representing colors in the* separate bytes of the parameter* @return A value that is calculated to be the linearly interpolated* result, derived by separating the start and end values into separate* color channels and interpolating each one separately, recombining the* resulting values in the same way.*/public Object evaluate(float fraction, Object startValue, Object endValue) {int startInt = (Integer) startValue;int startA = (startInt >> 24) & 0xff;int startR = (startInt >> 16) & 0xff;int startG = (startInt >> 8) & 0xff;int startB = startInt & 0xff;int endInt = (Integer) endValue;int endA = (endInt >> 24) & 0xff;int endR = (endInt >> 16) & 0xff;int endG = (endInt >> 8) & 0xff;int endB = endInt & 0xff;return (int)((startA + (int)(fraction * (endA - startA))) << 24) |(int)((startR + (int)(fraction * (endR - startR))) << 16) |(int)((startG + (int)(fraction * (endG - startG))) << 8) |(int)((startB + (int)(fraction * (endB - startB))));}
}

因为一共有四个值,ARGB,每个用八位来表示,所以十六进制就是0xffffff00

在evaluate方法里面先拿到A的颜色,然后分别拿到R,G,B的值,同理,拿到结束颜色相应的值,然后根据百分比分别计算出当前变化后的值,最后用或运算拼起来,就得到了一个完整的变化的值。

这篇关于Android 动画 ValueAnimator(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/592522

相关文章

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局