Android Interpolator插值器详解

2023-12-24 22:59

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

动画的基本原理是从开始时间到结束时间一帧一帧的播放静态图像。Interpolator插值器来指定动画如何变化的东东。Interpolator本质上讲是一种数学函数,参数是0.0到1.0之间的浮点数,输出值也是0.0到1.0的浮点数,曲线的斜率是速度。

Android系统的插值器有9种:

在这里插入图片描述
Interpolators的使用方式有两种:一种是通过xml文件来设置,另一种在代码中设置
xml中的使用

<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator       xmlns:android="http://schemas.android.com/apk/res/android"android:factor="2"/>
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:fromAlpha="0.0"android:toAlpha="1.0"android:interpolator="@anim/linearpolator_alpha"/>

1.LinearInterpolator 线性插值器

资源ID:@android:anim/linear_interpolator
xml标签:linearInterpolator
函数公式:y = t
坐标系解释:x轴表示的是动画的进度,取值范围是[0.0,1.0],y轴表示的得到的值,取值可以为正可以为负,1.0是他的目标值,下面的坐标图表示的含义都相同

在这里插入图片描述在这里插入图片描述

2.AccelerateInterpolator加速插值器

资源ID: @android:anim/accelerate_interpolator
XML标记: accelerateInterpolator
函数公式: y=t^(2f)
有参构造函数:public AccelerateInterpolator(float factor) xml中属性:android:factor
描述:起始速度是0,速度越来越快,加速运动。factor的大小对曲线的影响看图吧。

在这里插入图片描述在这里插入图片描述
关键源码:

 public AccelerateInterpolator(float factor) {mFactor = factor;mDoubleFactor = 2 * mFactor;}public float getInterpolation(float input) {if (mFactor == 1.0f) {return input * input;} else {return (float)Math.pow(input, mDoubleFactor);//幂函数y = input^mDoubleFactor  }}

3.DecelerateInterpolator减速插值器

资源ID: @android:anim/decelerate_interpolator
XML标记: decelerateInterpolator
函数公式: y=1-(1-t)^(2f)
有参构造函数:public DecelerateInterpolator(float factor) xml中属性:android:factor
描述:起始速度不为0,速度越来越慢,最后速度为0,减速运动。factor的大小对曲线的影响看图吧。

在这里插入图片描述在这里插入图片描述

关键源码:

 public float getInterpolation(float input) {float result;if (mFactor == 1.0f) {result = (float)(1.0f - (1.0f - input) * (1.0f - input));} else {result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));}return result;}

4.AccelerateDecelerateInterpolator先加速后减速插值器

资源ID: @android:anim/accelerate_decelerate_interpolator
XML标记: accelerateDecelerateInterpolator
函数公式: y=cos((t+1)π)/2+0.5
描述:速度从0开始,先加速后加速,最后速度为0

在这里插入图片描述在这里插入图片描述

关键源码:

public float getInterpolation(float input) {return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;}

5.AnticipateInterpolator

资源ID: @android:anim/anticipate_interpolator
XML标记: anticipateInterpolator
函数公式: y=(T+1)×t3–T×t2
有参的构造函数:public AnticipateInterpolator(float tension) xml中的属性:android:tension
描述:第一步运动,反方向运动,先加速后减速至0,此时运动到的位置为负值,第二步运动,正方向加速运动,速度越来越快。就像跳远一样,先向后退几步,增大助跑距离。tension大小对曲线的影响请看图

在这里插入图片描述在这里插入图片描述

关键源码:

 public float getInterpolation(float t) {return t * t * ((mTension + 1) * t - mTension);}

6.OvershootInterpolator

资源ID: @android:anim/overshoot_interpolator
XML标记: overshootInterpolator
函数公式: y=(T+1)x(t1)3+T×(t1)2 +1
有参的构造函数:public OvershootInterpolator(float tension) xml中的属性:android:tension
描述:第一步正方向运动,速度从0开始,先加速后减速至0,此时y的值大于1了,第二步反方向运动至y值为1。就像跑步速度过快一样,没有留住,跑过终点了,然后再往回跑,回到终点。tension对曲线的影响看图。

在这里插入图片描述在这里插入图片描述

关键源码:

 public float getInterpolation(float t) {      t -= 1.0f;return t * t * ((mTension + 1) * t + mTension) + 1.0f;}

7.AnticipateOvershootInterpolator

资源ID: @android:anim/anticipate_overshoot_interpolator
XML标记: anticipateOvershootInterpolator
在这里插入图片描述
函数公式: 
有参的构造函数:
public AnticipateOvershootInterpolator(float tension)
public AnticipateOvershootInterpolator(float tension, float extraTension)
描述:效果是AnticipateInterpolator 和OvershootInterpolator两个加起来的效果,T的值为tension*extraTension

在这里插入图片描述在这里插入图片描述

关键源码:

 public AnticipateOvershootInterpolator(float tension, float extraTension) {mTension = tension * extraTension;}public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {TypedArray a;....mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);...}private static float a(float t, float s) {return t * t * ((s + 1) * t - s);}private static float o(float t, float s) {return t * t * ((s + 1) * t + s);}public float getInterpolation(float t) {        if (t < 0.5f) {return 0.5f * a(t * 2.0f, mTension);}else {return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);}}

8.BounceInterpolator弹跳插值器

资源ID: @android:anim/bounce_interpolator
XML标记: bounceInterpolator
在这里插入图片描述
函数公式:  这里写图片描述
描述:就像跳跳球掉落在地面一样的效果

在这里插入图片描述在这里插入图片描述

关键源码:

  private static float bounce(float t) {return t * t * 8.0f;}public float getInterpolation(float t) {        t *= 1.1226f;if (t < 0.3535f) return bounce(t);else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;else return bounce(t - 1.0435f) + 0.95f;}

9.CycleInterpolator周期插值器

资源ID: @android:anim/cycle_interpolator
XML标记: cycleInterpolator
函数公式: y=sin(2π×C×t)
有参构造函数: public CycleInterpolator(float cycles) xml中属性:android:cycles
描述:cycles是周期值,默认为1,cycles=2表示动画会执行两次

在这里插入图片描述在这里插入图片描述

关键源码:

  public float getInterpolation(float input) {return (float)(Math.sin(2 * mCycles * Math.PI * input));}

10.自定义插值器

想要实现的函数公式: 
在这里插入图片描述

public class MyInterpolator implements Interpolator {@Overridepublic float getInterpolation(float input) {float x=2.0f*input-1.0f;return 0.5f*(x*x*x + 1.0f);}
}

在这里插入图片描述在这里插入图片描述

自定义插值器要实现Interpolator ,Interpolator直接继承TimeInterpolator(注意源码版本有些改动,但是实现思路是没变的)
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);
}

参数 input:input 参数是一个 float 类型,它取值范围是 0 到 1,表示当前动画的进度,取 0 时表示动画刚开始,取 1 时表示动画结束,取 0.5 时表示动画中间的位置,其它类推。 返回值:表示当前实际想要显示的进度。取值可以超过 1 也可以小于 0,超过 1 表示已经超过目标值,小于 0 表示小于开始位置。 对于 input 参数,它表示的是当前动画的进度,匀速增加的。什么叫动画的进度,动画的进度就是动画在时间上的进度,与我们的任何设置无关,随着时间的增长,动画的进度自然的增加,从 0 到 1;input 参数相当于时间的概念,我们通过 setDuration()指定了动画的时长,在这个时间范围内,动画进度肯定是一点点增加的;
————————————————
版权声明:本文为CSDN博主「钉某人」的原创文章

这篇关于Android Interpolator插值器详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1