0

我想更改作为 anObjectAnimator一部分的 an的值,该值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>
4

1 回答 1

1

这是可能的。

    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..
于 2018-05-21T02:55:26.730 回答