4

I'm trying to do an easy translation animation in Android. The following doesn't work:

public class MyView extends ViewGroup {
    ...
    TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -500);
    animation.setDuration(300);
    startAnimation(animation);
    ...
}

However, this works:

public class MyView extends ViewGroup {
    ...
    animate().setDuration(300).translationYBy(-500);
    ...
}

I need the top version to work because I'm adding in more views that need to be animated at the same time and I'd like to use TranslateAnimations inside an AnimationSet.

MrGrinst
  • 970
  • 3
  • 9
  • 20

2 Answers2

0

Well after doing some research into both methods of animation I came up with the following:

List<Animator> animators = new ArrayList<>();
for (int i = 0; i < view.getChildCount(); i++) {
    View child = view.getChildAt(i);
    ObjectAnimator va = ObjectAnimator.ofFloat(child, View.TRANSLATION_Y, child.getY() - 500);
    va.setDuration(300);
    animators.add(va);
}

Some explanation:

  1. TranslateAnimations are part of the older animation library included by Android. They don't actually change the underlying property values of the object as they animate it. I noticed if I performed other actions on the screen (like scrolling a ListView) as the animation performed, flashes of the animation were visible.
  2. As of Honeycomb (Android 3.0), there is a newer way of animating things that changes the underlying property values themselves as it animates. It's called ObjectAnimator. Then in version 3.1, they introduced another class to help make ObjectAnimator even easier: ViewPropertyAnimator. The second method I was using is actually a shortcut to use ViewPropertyAnimator.
  3. As I said in the question, I needed whatever solution I found to animate multiple things at once. Unfortunately ViewPropertyAnimator isn't capable of doing multiple animations at once. So I ended up using ObjectAnimator
  4. Finally, because ObjectAnimator doesn't support translation by a certain value, I had to calculate the final Y positions of the views using their current Y values.

source: http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html

MrGrinst
  • 970
  • 3
  • 9
  • 20
  • What do you mean with ViewPropertyAnimator is not capable of doing multiple Animations at once? – A Honey Bustard Jun 06 '15 at 14:06
  • Maybe this answer and the links to the Videos can help you understand the different animation APIs better : http://stackoverflow.com/questions/29175429/objectanimator-vs-translateanimation/29187285#29187285 – A Honey Bustard Jun 06 '15 at 14:08
  • I was unable to find any methods on ViewPropertyAnimator that allowed me to give a list of views and different values and animate all at once. – MrGrinst Jun 06 '15 at 14:21
0

This is not an answer to the actual question, more a suggestion of how to use ViewPropertyAnimator simultaneously with different Views and values. Write your own method and pass in the values you need as parameters for example (min implementation) :

private void animateView(View view, float transX, float transY, int duration) {

     view.animate().
         .translationXBy(transX)
         .translationYBy(transY)
         .duration(duration);
}

Add more parameters if you like. Interpolator, Scale, Rotate or Alpha values for example. Then use a loop or write another method to animate your views simultaneously. To be sure they animate all at the same time you could also add a small ms startDelay to the parameters to make sure all Animations are ready loaded before starting. Hope that helps.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38