Android 属性动画

属性动画实(Property Animation)在 Android 3.0 中引入的,在补间动画中改变的只是 View 的绘制效果,而 View 的真实属性是没有变化的,而属性动画则可以直接改变 View 对象的属性值,属性动画几乎可以对任何对象执行动画,而不是局限在 View 对象上,从某种意义上讲,属性动画可以说是增强版的补间动画。

动画属性 说明
动画持续时间 默认值是 300 ms,在资源文件中通过 android:duration 指定
动画插值方式 在资源文件中通过 android:interpolator 指定
动画重复次数 指定动画重复的次数,在资源文件中通过 android:repeatCount 指定
动画重复模式 指定一次动画播放结束后,结束下次动画时,是从开始帧再次播放到结束帧,还是从结束帧反向播放到开始帧,在资源文件中通过 android:repeatMode 指定
帧刷新频率 指定间隔多长时间播放一帧,默认值是 10 ms
动画集合 通过动画集合将多个属性动画组合起来,通过指定 android:ordering 属性控制这租动画是按次序播放还是同时播放,在资源文件中通过 <set></set> 来表示

属性动画的基类是 Animator,是一个抽象类,所以不会直接使用这个类,通常通过继承它并重写其中的方法,Android SDK 默认提供了几个子类,大多数情况使用这些子类就能完成日常开发。

Evaluator

Evaluator 是用来控制属性动画如何计算属性值。它的接口定义是 TypeEvaluator,其中定义了 evaluate 方法,供不同类型的子类实现。

public interface TypeEvaluator<T> {
    T evaluate(float var1, T var2, T var3);
}

常见的实现类有 IntEvaluator、FloatEvaluator、ArgbEvaluator 等。

AnimatorSet

AnimatorSet 是 Animator 的子类,用来组合多个 Animator,并指定这些 Animator 是顺序播放还是同时播放。

ValueAnimator

ValueAnimator 是属性动画最重要的一个类,继承自 Animator。定义了属性动画大部分的核心功能,包括计算各个帧的属性值、处理更新事件、按照属性值的类型控制计算规则等。
一个完整的属性动画由以下两部分组成。

  • 计算动画各个帧的相关属性值。
  • 将这些属性值设置给指定的对象。

ValueAnimator 实现了第一部分的功能。ValueAnimator的构造函数是空实现,一般都是使用静态工厂方法来实现实例化。


    /**
     * Creates a new ValueAnimator object. This default constructor is primarily for
     * use internally; the factory methods which take parameters are more generally
     * useful.
     */
    public ValueAnimator() {
    }

    /**
     * Constructs and returns a ValueAnimator that animates between int values. A single
     * value implies that that value is the one being animated to. However, this is not typically
     * useful in a ValueAnimator object because there is no way for the object to determine the
     * starting value for the animation (unlike ObjectAnimator, which can derive that value
     * from the target object and property being animated). Therefore, there should typically
     * be two or more values.
     *
     * @param values A set of values that the animation will animate between over time.
     * @return A ValueAnimator object that is set up to animate between the given values.
     */
    public static ValueAnimator ofInt(int... values) {
        ValueAnimator anim = new ValueAnimator();
        anim.setIntValues(values);
        return anim;
    }

    /**
     * Constructs and returns a ValueAnimator that animates between color values. A single
     * value implies that that value is the one being animated to. However, this is not typically
     * useful in a ValueAnimator object because there is no way for the object to determine the
     * starting value for the animation (unlike ObjectAnimator, which can derive that value
     * from the target object and property being animated). Therefore, there should typically
     * be two or more values.
     *
     * @param values A set of values that the animation will animate between over time.
     * @return A ValueAnimator object that is set up to animate between the given values.
     */
    public static ValueAnimator ofArgb(int... values) {
        ValueAnimator anim = new ValueAnimator();
        anim.setIntValues(values);
        anim.setEvaluator(ArgbEvaluator.getInstance());
        return anim;
    }

    /**
     * Constructs and returns a ValueAnimator that animates between float values. A single
     * value implies that that value is the one being animated to. However, this is not typically
     * useful in a ValueAnimator object because there is no way for the object to determine the
     * starting value for the animation (unlike ObjectAnimator, which can derive that value
     * from the target object and property being animated). Therefore, there should typically
     * be two or more values.
     *
     * @param values A set of values that the animation will animate between over time.
     * @return A ValueAnimator object that is set up to animate between the given values.
     */
    public static ValueAnimator ofFloat(float... values) {
        ValueAnimator anim = new ValueAnimator();
        anim.setFloatValues(values);
        return anim;
    }

    /**
     * Constructs and returns a ValueAnimator that animates between the values
     * specified in the PropertyValuesHolder objects.
     *
     * @param values A set of PropertyValuesHolder objects whose values will be animated
     * between over time.
     * @return A ValueAnimator object that is set up to animate between the given values.
     */
    public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
        ValueAnimator anim = new ValueAnimator();
        anim.setValues(values);
        return anim;
    }
    /**
     * Constructs and returns a ValueAnimator that animates between Object values. A single
     * value implies that that value is the one being animated to. However, this is not typically
     * useful in a ValueAnimator object because there is no way for the object to determine the
     * starting value for the animation (unlike ObjectAnimator, which can derive that value
     * from the target object and property being animated). Therefore, there should typically
     * be two or more values.
     *
     * <p><strong>Note:</strong> The Object values are stored as references to the original
     * objects, which means that changes to those objects after this method is called will
     * affect the values on the animator. If the objects will be mutated externally after
     * this method is called, callers should pass a copy of those objects instead.
     *
     * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this
     * factory method also takes a TypeEvaluator object that the ValueAnimator will use
     * to perform that interpolation.
     *
     * @param evaluator A TypeEvaluator that will be called on each animation frame to
     * provide the ncessry interpolation between the Object values to derive the animated
     * value.
     * @param values A set of values that the animation will animate between over time.
     * @return A ValueAnimator object that is set up to animate between the given values.
     */
    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
        ValueAnimator anim = new ValueAnimator();
        anim.setObjectValues(values);
        anim.setEvaluator(evaluator);
        return anim;
    }

获得实例之后,接着需要设置动画持续时间、插值方式、重复次数等属性值,然后启动动画,最后需要为 ValueAnimator 注册 AnimatiorUpdateListener 监听器,并在监听器的 onAnimationUpdate 方法中将计算出来的属性值设置给指定对象。

ObjectAnimator

ObjectAnimator 是 ValueAnimator 的子类,封装实现了属性动画的第二部分的功能。
实际开发中用得最多的就是 ObjectAnimator,只有在 ObjectAnimator 实现不了的场景下,才考虑使用 ValueAnimator。ObjectAnimator 和 ValueAnimator 在构造实例时最大的不同是需要指定动画作用的具体对象和对象的属性名,而且一般不需要注册 AnimatiorUpdateListener 监听器。

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "progress", progress);
objectAnimator.setDuration(300);
objectAnimator.start();

使用 ObjectAnimator 有以下几点需要注意。

  • 需要为对象对应的属性提供 setter 方法。
  • 动画的对象是 View 的话,为了显示动画效果,在某些情况下,需要注册 AnimatiorUpdateListener 监听器,在其回调方法 onAnimationUpdate 中调用 View 的 invalidate 方法来刷新 View 的显示。

XML 资源文件方式

属性动画 XML 方式实现。
在项目中的 res/animator 目录中存放这个属性动画的 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">
    <objectAnimator
        android:duration="2000"
        android:propertyName="scaleX"
        android:valueFrom="1"
        android:valueTo="0.4"
        android:valueType="floatType" />

    <objectAnimator
        android:duration="2000"
        android:propertyName="scaleY"
        android:valueFrom="1"
        android:valueTo="0.4"
        android:valueType="floatType" />
</set>

使用代码加载。

ivFrameXML.setImageResource(R.drawable.animation);
AnimationDrawable anim = (AnimationDrawable) ivFrameXML.getDrawable();
anim.start();

代码方式

AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "scaleX", 1F, 0.4F);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(this, "scaleY", 1F, 0.4F);

animatorSet.playTogether(objectAnimator, objectAnimator1);
animatorSet.setDuration(2000);
animatorSet.setTarget(ivPropertyCode);
animatorSet.start();

实现效果

实现效果.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353