使用单个ObjectAnimator更改多个属性?

时间:2015-02-05 19:25:26

标签: android android-animation objectanimator

我有一个非常复杂的动画我需要编码,而且我使用了一堆ObjectAnimators,如下所示:

ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view, TRANSLATION_X, value).setDuration(BASE_DURATION * 2);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, TRANSLATION_Y, value).setDuration(BASE_DURATION * 2);

是否可以将X和Y翻译分组到同一个ObjectAnimator中,而不是创建一堆然后将它们全部添加到AnimatorSet中?

谢谢!

5 个答案:

答案 0 :(得分:46)

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(TRANSLATION_X, value);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(TRANSLATION_Y, value);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY);
animator.setDuration(BASE_DURATION * 2);
animator.start();

http://developer.android.com/guide/topics/graphics/prop-animation.html#views  一个ObjectAnimator

答案 1 :(得分:14)

我认为你应该使用 AnimationSet 。 它几乎可以做你想要的,也是很好的方式,因为当我想到 PropertyValuesHolder 时,我不会想到关键帧

所以是的:

ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view, TRANSLATION_X, value);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, TRANSLATION_Y, value);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1, objectAnimator2);
animatorSet.setDuration(BASE_DURATION * 2);

animatorSet.start();

您可以根据需要添加任意数量的 ObjectAnimators

答案 2 :(得分:3)

如果您要为视图设置动画(如您的示例所示),则可以使用ViewPropertyAnimator:

view.animate().translationX(value_x).translationY(value_y).setDuration(duration).start();

答案 3 :(得分:2)

动画视图可以更轻松地完成:

v.animate().x(valueX).y(valueY).setDuration(500).start();

(在此片段中使用了ViewPropertyAnimator)。

答案 4 :(得分:2)

我用来向对象的动画添加多个属性的另一种方法是使用混合的代码和XML来定义动画。这基于此documentation

例如,在XML中,我可以为具有静态值的单个对象设置AnimatorSets和ObjectAnimators,并定义一系列连续的更改(res / animator / moveout.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">
    <set>
        <objectAnimator
            android:propertyName="scaleX"
            android:duration="2000"
            android:valueTo="0.8"
            android:interpolator="@android:anim/decelerate_interpolator"/>
        <objectAnimator
            android:propertyName="scaleY"
            android:duration="2000"
            android:valueTo="0.8"
            android:interpolator="@android:anim/decelerate_interpolator"/>
        <objectAnimator
            android:propertyName="alpha"
            android:duration="2000"
            android:valueTo="0"
            android:interpolator="@android:anim/decelerate_interpolator"/>
    </set>
    <set>
        <objectAnimator
            android:propertyName="scaleX"
            android:duration="2000"
            android:valueTo="1.2"
            android:interpolator="@android:anim/accelerate_interpolator"/>
        <objectAnimator
            android:propertyName="scaleY"
            android:duration="2000"
            android:valueTo="1.2"
            android:interpolator="@android:anim/accelerate_interpolator"/>
        <objectAnimator
            android:propertyName="alpha"
            android:duration="2000"
            android:valueTo="1"
            android:interpolator="@android:anim/accelerate_interpolator"/>
    </set>
</set>

然后我可以在运行时加载这些AnimatorSets / ObjectAnimators并使用动态生成的值修改它们的值:

    AnimatorSet firstSet = (AnimatorSet) AnimatorInflater.loadAnimator(this,
            R.animator.moveout);

    AnimatorSet secondSet = firstSet.clone();
    firstSet.setTarget(button);
    secondSet.setTarget(anotherButton);

    // Choreograph the animations
    // Change the duration of all child elements in the set
    firstSet.setDuration(1000);
    secondSet.setDuration(200);

    // Set start delay so second set plays after the first set
    secondSet.setStartDelay(2000);
    AnimatorSet anim = new AnimatorSet();
    anim.playTogether(firstSet,secondSet);
    anim.start();