Android动画(三):Interpolator和Evaluator

本系列将介绍以下内容:
在这里插入图片描述

Android动画

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)如图所示:
Interpolator类图

Interpolator类图
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 Class
Resource ID
AccelerateDecelerateInterpolator
@android:anim/accelerate_decelerate_interpolator
AccelerateInterpolator
@android:anim/accelerate_interpolator
AnticipateInterpolator
@android:anim/anticipate_interpolator
AnticipateOvershootInterpolator
@android:anim/anticipate_overshoot_interpolator
BounceInterpolator
@android:anim/bounce_interpolator
CycleInterpolator
@android:anim/cycle_interpolator
DecelerateInterpolator
@android:anim/decelerate_interpolator
LinearInterpolator
@android:anim/linear_interpolato
OvershootInterpolator
@android:anim/overshoot_interpolator
PathInterpolator
无此用法

常用的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) 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值