我们会见到动画播放由快到慢播放、匀速播放、先加速后减速播放。这些效果都是通过插值器来实现的。
Interpolator 简介
1、作用
可以控制动画的变化速率
2、须知
1、Interpolator并不是属性动画中新增的技术,实际上从Android 1.0版本开始就一直存在Interpolator接口
2、补间动画也是支持这个功能的
3、属性动画中新增了一个TimeInterpolator接口,这个接口是兼容之前的Interpolator的。
3、TimeInterpolator常见的实现类(系统实现好的)
1、AccelerateDecelerateInterpolator 先加速后减速
2、AccelerateInterpolator 加速
3、DecelerateInterpolator 减速
4、BounceInterpolator 反弹(乒乓球垂直落地在反弹、反弹、效果)
5、等等
anim.setInterpolator(new AccelerateInterpolator(2f))
4、自定义属性动画的Interpolator
由于属性动画提供了TimeInterpolator的兼容接口。我们实现即可。
(1)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);
}
1、函数接收一个参数 input
2、参数随着动画的运行而不断变化
3、参数变化有规律,他根据动画的时长从0到1匀速增加。(LinearInterpolator的默认实现 直接返回的input值)
(2)input和fraction的关系
input 决定 fraction的值(不同的插值器实现类的getInterpolation()方法实现不同)
LinearInterpolator的实现
y=kx 式的函数 匀速动画
AccelerateDecelerateInterpolator的实现
y=cos((x+1)*π/2)+0.5 (0<x<1) 取值范围内 先加速后减速
函数图:(来自网络)
(3)自定义简单实现
/**
* Created by sunnyDay on 2019/6/11 20:08
*/
public class CustomInterpolator implements TimeInterpolator {
@Override
public float getInterpolation(float input) {
return (float) Math.sin(Math.PI*input);
}
}
ViewPropertyAnimator
属性动画的机制已经不是再针对于View而进行设计的了,而是一种不断地对值进行操作的机制,它可以将值赋值到指定对象的指定属性上。但是,在绝大多数情况下,我相信大家主要都还是对View进行动画操作的。Android开发团队也是意识到了这一点,没有为View的动画操作提供一种更加便捷的用法确实是有点太不人性化了,于是在Android 3.1系统当中补充了ViewPropertyAnimator这个机制。
1、栗子
button = findViewById(R.id.btn);
button.animate() // 链式调用 返回ViewPropertyAnimator 对象
.alpha(1)
.alpha(0)
.alpha(1)
.rotationX(360)// 沿着X轴旋转360度
.setDuration(5000);
1、整个ViewPropertyAnimator的功能都是建立在View类新增的animate()方法之上的
2、链式调用
3、无需开启动画即可(默认会开启)我们也可手动开启。
end
参考:
https://blog.csdn.net/guolin_blog/article/details/44171115
安卓开发艺术探索
同时感谢郭神分享!!!