文章概览
本系列将介绍以下内容:
1 插值器Interpolator
1.1 Interpolator概述
有关动画的变化速率的问题是由Interpolator接口决定的,它是用来指定动画如何变化的变量,即用来控制动画的区间值是如何被计算出来的。
哪些动画可以设置插值器呢?
可以:Tween Animation、ValueAnimator、ObjectAnimator、ViewPropertyAnimator
不可以:Frame Animation
即只有逐帧动画不能设置插值器Interpolator。
View Animation中,补间动画的父类Animation中有具体方法setInterpolator(Interpolator i),而逐帧动画的实现类AnimationDrawable没有相关方法,即补间动画可以设置插值器,而逐帧动画不能。
Property Animation均可设置插值器,因为ValueAnimator和ObjectAnimator的父类Animator中有抽象方法setInterpolator(TimeInterpolator value),而ViewPropertyAnimator中单独定义了非抽象方法setInterpolator(TimeInterpolator interpolator)。
Interpolator接口的父接口是TimeInterpolator,而常用的插值器实现类全部继承自抽象类BaseInterpolator。
TimeInterpolator位于包android.animation中,Interpolator、BaseInterpolator及其十个子类全部位于包android.view.animation中。
关于Interpolator接口的UML图(类图Class Diagram)如图所示:
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);
}
package android.view.animation;
import android.animation.TimeInterpolator;
/**
* An interpolator defines the rate of change of an animation. This allows
* the basic animation effects (alpha, scale, translate, rotate) to be
* accelerated, decelerated, repeated, etc.
*/
public interface Interpolator extends TimeInterpolator {
// A new interface, TimeInterpolator, was introduced for the new android.animation
// package. This older Interpolator interface extends TimeInterpolator so that users of
// the new Animator-based animations can use either the old Interpolator implementations or
// new classes that implement TimeInterpolator directly.
}
package android.view.animation;
import android.content.pm.ActivityInfo.Config;
/**
* An abstract class which is extended by default interpolators.
*/
abstract public class BaseInterpolator implements Interpolator {
private @Config int mChangingConfiguration;
/**
* @hide
*/
public @Config int getChangingConfiguration() {
return mChangingConfiguration;
}
/**
* @hide
*/
void setChangingConfiguration(@Config int changingConfiguration) {
mChangingConfiguration = changingConfiguration;
}
}
1.2 常用插值器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
常用的Interpolator实现类有十个,全部继承自抽象类BaseInterpolator,且均位于包android.view.animation中:
package android.view.animation;
import android.content.Context;
import android.graphics.animation.HasNativeInterpolator;
import android.graphics.animation.NativeInterpolator;
import android.graphics.animation.NativeInterpolatorFactory;
import android.util.AttributeSet;
/**
* An interpolator where the rate of change starts and ends slowly but
* accelerates through the middle.
*/
@HasNativeInterpolator
public class AccelerateDecelerateInterpolator extends BaseInterpolator
implements NativeInterpolator {
public AccelerateDecelerateInterpolator() {
}
@SuppressWarnings({
"UnusedDeclaration"})
public AccelerateDecelerateInterpolator(Context context, AttributeSet attrs)