跳至主要内容

Android 四种特殊Interpolator源码解析 收藏

animation.setInterpolator(new BounceInterpolator());

今天介绍4种动画插值器
  1. OvershootInterpolator: "Overshoot" means 冲过头了,模拟了冲过了头回滚一点的效果
  2. AnticipateInterpolator:“Anticipate” means 抢先,模拟了出发前先后退一步再前冲的动画效果
  3. AnticipateOvershootInterpolator:以上两种的结合
  4. BounceInterpolator:"Bounce" means 弹跳, 就是模拟了自由落地后回弹的效果

OvershootInterpolation


先来看Overshoot的实现的数学公式,mTension默认值为2.0f,有一个构造函数可以进行设置。
public float getInterpolation(float t) {
    t -= 1.0f;
    return t * t * ((mTension + 1) * t + mTension) + 1.0f;
}
如果带入默认值,简化下就是
y=3t^3+2t^2+1  // t:[-1~0]
然后去google搜索一下"3t^3+2t^2+1",就能看到下面这个曲线图
overshoot
我们改下参数mTension,就能发现mTension越大,冲过的距离越大,效果越明显!

AnticipateInterpolation


再看下Anticipate的实现数学公式,mTension同样默认是2.0f:
public float getInterpolation(float t) { 
    return t * t * ((mTension + 1) * t - mTension);
}
于是带上实际参数2.0f,得到
y=3t^3-2t^2 //t:[0~1.0]
anticipate
同样的结论:mTension越大,抢先动画约长,效果越明显!

BounceInterpolator


从源码我们能发现,实现方式是根据时间t,模拟了一次自由落体运动,所以Google这次帮不了我们了。
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;
}

评论

此博客中的热门博文

javascript数组删除指定元素 remove index array specific

定义和用法 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。 注释: 该方法会改变原始数组。 语法 arrayObject.splice(index,howmany,item1,.....,itemX) 参数 描述 index 必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。 howmany 必需。要删除的项目数量。如果设置为 0,则不会删除项目。 item1, ..., itemX 可选。向数组添加的新项目。 返回值 类型 描述 Array 包含被删除项目的新数组,如果有的话。 说明 splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。 如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.