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 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo