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

Change multiple properties with single 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 中?

谢谢!

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

view.animate().translationX(value_x).translationY(value_y).setDuration(duration).start();
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

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

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

(在此代码段中使用了 ViewPropertyAnimator)。

我认为您应该使用 AnimationSet。 它实际上可以做你想做的,而且是做这件事的好方法,因为当我想到 PropertyValuesHolder 时,我没有想到 Keyframes.

是的:

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

我用来向对象的动画添加多个属性的另一种方法是混合使用代码和 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();