做过android动画的人对Interpolator应该不会陌生,这个类主要是用来控制android动画的执行速率,一般情况下,如果我们不设置,动画都不是匀速执行的,系统默认是先加速后减速这样一种动画执行速率。

android通过Interpolator类来让我们自己控制动画的执行速率,还记得上一篇博客中我们使用属性动画实现的旋转效果吗?在不设置Interpolator的情况下,这个动画是先加速后减速,我们现在使用android系统提供的类LinearInterpolator来设置动画的执行速率,LinearInterpolator可以让这个动画匀速执行,我们来看一个案例,我们有两个TextView重叠放在一起,点击旋转按钮后这两个TextView同时执行旋转动画,不同的是一个设置了LinearInterpolator,而另外一个什么都没有设置,代码如下:

            LinearInterpolator ll = new LinearInterpolator();
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotation",
0f, 360f);
animator.setInterpolator(ll);
animator.setDuration(5000);
animator.start();
ObjectAnimator animator2 = ObjectAnimator.ofFloat(tv2, "rotation",
0f, 360f);
animator2.setDuration(5000);
animator2.start();

效果图如下:

现在我们可以很清楚的看到这里的差异,一个TextView先加速后减速,一个一直匀速运动。

这就引起了我的好奇,究竟LinearInterpolator做了什么,改变了动画的执行速率。这里我们就要看看源码了。

当我们调用animator.setInterpolator(ll);的时候,调用的是ValueAnimator方法中的setInterpolator方法,源码如下:

    public void setInterpolator(TimeInterpolator value) {
if (value != null) {
mInterpolator = value;
} else {
mInterpolator = new LinearInterpolator();
}
}

我们看到这里有一个mInterpolator变量,如果我们不执行这个方法,那么mInterpolator 的默认值是多少呢?

我们找到了这样两行代码:

    // The time interpolator to be used if none is set on the animation
private static final TimeInterpolator sDefaultInterpolator =
new AccelerateDecelerateInterpolator();
private TimeInterpolator mInterpolator = sDefaultInterpolator;

这下明朗了,如果我们不设置,那么系统默认使用AccelerateDecelerateInterpolator,AccelerateDecelerateInterpolator又是什么呢?继续看源码:

public class AccelerateDecelerateInterpolator extends BaseInterpolator
implements NativeInterpolatorFactory {
public AccelerateDecelerateInterpolator() {
} @SuppressWarnings({"UnusedDeclaration"})
public AccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {
} public float getInterpolation(float input) {
return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
} /** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createAccelerateDecelerateInterpolator();
}
}

这里的一个核心函数就是getInterpolation,使用了反余弦函数,input传入的值在0-1之间,因此这里返回值的变化速率就是先增加后减少,对应的动画执行速率就是先增加后减速。有兴趣的童鞋可以使用MatLab来画一下这个函数的图像。而当我们实现了LinearInterpolator之后,情况发生了变化:

public class LinearInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

    public LinearInterpolator() {
} public LinearInterpolator(Context context, AttributeSet attrs) {
} public float getInterpolation(float input) {
return input;
} /** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactoryHelper.createLinearInterpolator();
}
}

这里干净利落直接返回了input,没有经过任何计算。input返回的值是均匀的,因此动画得以匀速执行。

看到这里,大家应该就明白了,如果我们想要控制动画的执行速率,应该重写getInterpolation方法就能实现。为了证实我们的猜想,我们继续看源码。


大部分时候,我们使用的系统提供的各种各样的**Interpolator,比如上文说的LinearInterpolator,这些类都是继承自Interpolator,而Interpolator则实现了TimeInterpolator接口,我们来看看一个继承结构图:

那么这个终极大Boss TimeInterpolator究竟是什么样子呢?

package android.animation;

/**
* A time interpolator defines the rate of change of an animation. This allows animations
* to have non-linear motion, such as acceleration and deceleration.
*/
public interface TimeInterpolator { /**
* Maps a value representing the elapsed fraction of an animation to a value that represents
* the interpolated fraction. This interpolated value is then multiplied by the change in
* value of an animation to derive the animated value at the current elapsed animation time.
*
* @param input A value between 0 and 1.0 indicating our current point
* in the animation where 0 represents the start and 1.0 represents
* the end
* @return The interpolation value. This value can be more than 1.0 for
* interpolators which overshoot their targets, or less than 0 for
* interpolators that undershoot their targets.
*/
float getInterpolation(float input);
}

源码还是很简单的,只有一个方法,就是getInterpolation,看来没错,就是它了,如果我们想要自定义Interpolator,只需要实现TimeInterpolator接口的getInterpolation方法就可以了,getInterpolation方法接收的参数是动画执行的百分比,这个值是均匀的。

我们来个简单的案例:

public class TanInterpolator implements TimeInterpolator {

    @Override
public float getInterpolation(float t) {
return (float) Math.sin((t / 2) * Math.PI);
}
}

在动画中使用:

            TanInterpolator tl = new TanInterpolator();
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotation",
0f, 360f);
animator.setInterpolator(tl);
animator.setDuration(5000);
animator.start();

咦?这是什么效果?这是一开始速度很大,然后逐渐减小到0的动画效果.

原因如下:

看下图,这是sin函数图象:

x取0-0.5PI,y值则为0-1,这一段曲线的斜率逐渐减小至0,这也是为什么我们的动画一开始执行很快,后来速度逐渐变为0.


好了,看完这些,想必大家已经理解了这个类的使用了吧。

版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。

从源码角度理解android动画Interpolator类的使用的更多相关文章

  1. 从源码角度理解Java设计模式——装饰者模式

    一.饰器者模式介绍 装饰者模式定义:在不改变原有对象的基础上附加功能,相比生成子类更灵活. 适用场景:动态的给一个对象添加或者撤销功能. 优点:可以不改变原有对象的情况下动态扩展功能,可以使扩展的多个 ...

  2. 【原创】源码角度分析Android的消息机制系列(三)——ThreadLocal的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 先看Android源码(API24)中对ThreadLocal的定义: public class ThreadLocal<T> 即 ...

  3. 【原创】源码角度分析Android的消息机制系列(五)——Looper的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. Looper在Android的消息机制中就是用来进行消息循环的.它会不停地循环,去MessageQueue中查看是否有新消息,如果有消息就立刻 ...

  4. 【原创】源码角度分析Android的消息机制系列(四)——MessageQueue的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. MessageQueue,主要包含2个操作:插入和读取.读取操作会伴随着删除操作,插入和读取对应的方法分别为enqueueMessage和ne ...

  5. 【转】java comparator 升序、降序、倒序从源码角度理解

    原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境jdk:1.7+ 前言之前我写过关于comparator的理解,但是都 ...

  6. 【原创】源码角度分析Android的消息机制系列(六)——Handler的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 先看Handler的定义: /** * A Handler allows you to send and process {@link Mes ...

  7. 【原创】源码角度分析Android的消息机制系列(一)——Android消息机制概述

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.为什么需要Android的消息机制 因为Android系统不允许在子线程中去访问UI,即Android系统不允许在子线程中更新UI. 为什 ...

  8. 【原创】源码角度分析Android的消息机制系列(二)——ThreadLocal的工作过程

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一篇文章中,我们已经提到了ThreadLocal,它并非线程,而是在线程中存储数据用的.数据存储以后,只能在指定的线程中获取到数据,对于其 ...

  9. 从源码角度深入理解Toast

    Toast这个东西我们在开发中经常用到,使用也很简单,一行代码就能搞定: 1: Toast.makeText(", Toast.LENGTH_LONG).show(); 但是我们经常会遇到这 ...

随机推荐

  1. Java 构造器 一道构造器调用子类重载方法的题目

    构造器中不能new本类对象,否则进入死循环. 构造器没有返回值,也没有void修饰. 使用关键字super可以调用父类的构造器,而且这一句必须放在第一句的位置,否则无法编译. 题目: 请写出以下程序的 ...

  2. HttpResponseCache 网络缓存使用

    Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. ...

  3. [Mac]使用xcode命令行编译protobuf

    因为不想每次测试与画面显示无关的小功能时把cocos2dx包含进去,所以改用xcode的command line tool直接建立c++项目. 方法: 1.点击file-new-project,选择o ...

  4. JBossESB教程(一)——开发环境的搭建

    前言 上篇对SOA的概述里面,在说SOA构建需要考虑什么时,提到了ESB,它作为SOA的基础设施而存在. 从这篇开始,将对ESB的其中一个实现JBossESB进行一个从头开始的讲解,既然是从头开始,那 ...

  5. C# 读取网络txt文件 并写在本地txt文件中

    public void WriteStream() { try { stirng ImagesPath = ImagesPath + "\\2013-09-27.txt"; Htt ...

  6. ExecutorService生命周期

    ExecutorService接口继承了Executor接口,定义了一些生命周期的方法 public interface ExecutorService extends Executor { void ...

  7. eclipse android重新安装遇到各种问题

    1.JAVA_HOME环境变量失效的解决办法 原文网址:http://www.cnblogs.com/yjmyzz/p/3521554.html 晚上把oracle自带的weblogic给卸载了,然后 ...

  8. Python的高级特性之切片、迭代、列表生成式、生成器

    切片 切片就是获取一个list.tuple.字符串等的部分元素 l = range(100) #取[0,5)元素 print(l[:5]) #[0, 1, 2, 3, 4] #在[0,99]中每隔10 ...

  9. PC-修改IE 与 禁止修改IE

    1.注册表编辑器1.1在Windows启动后1.2点击“开始”1.3“运行”菜单项1.4在“打开”栏中键入“regedit”1.5然后按“确定”键 2.首页项2.1展开注册表到“HKEY_LOCAL_ ...

  10. 1116 HTML CSS

    1. JPEG和GIF在使用时的注意事项: JPEG可以连续调次(复制品中有中间层次,如照片)图像中获得最好效果:JPEG可以用1600万色显示图像,是有损格式,不支持透明. GIF适用于几种纯色组成 ...