No ViewGroup Animation

I am trying to make an easy translation animation in Android. The following does not 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 add more views that need to be animated at the same time, and I would like to use TranslateAnimationinside AnimationSet.

+4
source share
2 answers

Well, after doing some research on both animation methods, 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:

  • TranslateAnimation , Android. , . , (, ListView) , .
  • Honeycomb (Android 3.0), , , . ObjectAnimator. 3.1 , ObjectAnimator : ViewPropertyAnimator. , , ViewPropertyAnimator.
  • , - , . , ViewPropertyAnimator . ObjectAnimator
  • , ObjectAnimator , Y- Y.

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

0

, , ViewPropertyAnimator . , , ( ):

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

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

, . , , , -. . , , ms startDelay , , . , .

0

All Articles