Search code examples
androidandroid-animationobjectanimator

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!


Solution

  • 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