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.
TranslateAnimation
AnimationSet
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:
ObjectAnimator
ViewPropertyAnimator
: http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html
, , ViewPropertyAnimator . , , ( ):
private void animateView(View view, float transX, float transY, int duration) { view.animate(). .translationXBy(transX) .translationYBy(transY) .duration(duration); }
, . , , , -. . , , ms startDelay , , . , .