以编程方式更改 AnimatorSet 的 ObjectAnimator 中的值

Change Value in ObjectAnimator of AnimatorSet programmatically

我想更改 ObjectAnimator 的值,它是 AnimatorSet 的一部分,它在 animator 目录的指定文件中编码为 Xml。

我不想用 Java 代码替换 Xml 文件,我也不想拆分它。

可以吗?

示例代码:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:duration="0" />

    <objectAnimator
        android:valueFrom="-170"  <!-- I want to change this value on run time -->
        android:valueTo="0"
        android:propertyName="rotationY"
        android:duration="@integer/anim_length" />

    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="alpha"
        android:startOffset="@integer/anim_length_half"
        android:duration="0" />

</set>

有可能。

    AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_yours);

    List<Animator> animators = animatorSet.getChildAnimations();
    for (int i = 0; i < animators.size(); i++) {
        Animator animator = animators.get(i);
        if (animator instanceof ObjectAnimator) {
            ObjectAnimator objectAnimator = (ObjectAnimator) animator;
            if ("rotationY".equals(objectAnimator.getPropertyName())) {
                float fromValue = -100;
                float toValue = 0;
                objectAnimator.setFloatValues(fromValue, toValue);
            }
        }
    }
    // and you use changed AnimoatorSet..