Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change multiple properties with single ObjectAnimator?

I have a pretty complex animation I need to code and I'm using a bunch of ObjectAnimators like the following:

ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view, TRANSLATION_X, value).setDuration(BASE_DURATION * 2);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, TRANSLATION_Y, value).setDuration(BASE_DURATION * 2);

Is it possible to group the X and Y translations into the same ObjectAnimator rather than creating a bunch of them then adding them all into an AnimatorSet?

Thanks!

like image 446
The Hungry Androider Avatar asked Feb 05 '15 19:02

The Hungry Androider


4 Answers

I think you should be using an AnimationSet. It practically does what you want and is the nice way of doing it because when I think of PropertyValuesHolder I don't think of Keyframes.

So yeah:

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();

You can add as many ObjectAnimators as you want.

like image 34
creativecreatorormaybenot Avatar answered Oct 18 '22 20:10

creativecreatorormaybenot


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

like image 116
Hugo Avatar answered Oct 18 '22 18:10

Hugo


If you are animating a view (like your example suggests), you could use a ViewPropertyAnimator:

view.animate().translationX(value_x).translationY(value_y).setDuration(duration).start();
like image 5
Quanturium Avatar answered Oct 18 '22 19:10

Quanturium


An alternative approach I've used to add multiple properties to an object's animation is to use a mix of code and XML to define the animation. This is based on this documentation

For example, in XML I can setup AnimatorSets and ObjectAnimators for a single object with static values, and define a sequential sequence of changes (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>

Then I can load these AnimatorSets/ObjectAnimators at runtime and modify their values with dynamically generated values:

    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();
like image 5
brif Avatar answered Oct 18 '22 19:10

brif