Search code examples
androidxmlobjectanimatoranimator

Android ObjectAnimatorSet XML Convert To JAVA Code


these is my animator set in xml, i want convert to java code, but i don't know how to set the "AnimatorSet" in the "AnimatorSet"?

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
    <set>
        <objectAnimator
            android:duration="50"
            android:propertyName="scaleX"
            android:valueFrom="1.0"
            android:valueTo="0.8" />
        <objectAnimator
            android:duration="50"
            android:propertyName="scaleY"
            android:valueFrom="1.0"
            android:valueTo="0.8" />
    </set>

    <set>
        <objectAnimator
            android:duration="50"
            android:propertyName="scaleX"
            android:valueFrom="0.8"
            android:valueTo="1.1" />
        <objectAnimator
            android:duration="50"
            android:propertyName="scaleY"
            android:valueFrom="0.8"
            android:valueTo="1.1" />
    </set>
</set>

Solution

  • First you have to create an AnimatorSet to play animation together, and then add two ObjectAnimator to AnimatorSet, like this :

    AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(view, "scaleX", 1, 0.8f, 1.1f),
                ObjectAnimator.ofFloat(view, "scaleY", 1, 0.8f, 1.1f)
        );
    animatorSet.setDuration(1000);
    animatorSet.start();