javaandroidobjectanimatoranimatorset

Change Value in ObjectAnimator of AnimatorSet programmatically


I'd like to change a value of an ObjectAnimator which is part of an AnimatorSet, which is coded in Xml in a designated file in the animator directory.

I don't want to replace the Xml file with Java code, neither do I want to split it up.

Is it possible?

example code:

<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>

Solution

  • It's possible.

        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..