Is there any way to animate multiple views at the same time?
What I want to do is translate animations:
I have 5 TextViews and 4 coloured strips (plain RelativeLayouts with a background). At the start of the animations, the stips are stacked with the TextViews in a horizontal row. At the end I want all the TextViews stacked between the strips:
This is a very simple drawing, but it demonstrates what I want to do. Is there any way of doing this with animations, or do I have to use canvas animations.
Create your animation objects, then use
startAnimation
collectively on all views at the same time. So it would be something like this:Just note that the more animations you have going on at once, the slower it's going to be.
You can use the ObjectAnimator for animating the multiple view as follows:
I've managed to share a single Animation instance among several Views. At least with an AlphaAnimation. I had a ListView and an animation that should be applied to a particular child of all list item views. In my case it should have been possible for the views to 'join' and 'leave' the shared animation at any time and it should not affect other animated views in any way or interfere with already running animation. To achieve this I had to make an adjusted copy of android's stock AlphaAnimation. My use case is rather special, but let it be here just in case someone has to deal with similar goal with ListView.
To set this animation to a view:
And to start an animation (previously set by setAnimation) TWO things must be done:
And after that you must manually call invalidate() on every view that is affected by the animation.
The usual startAnimation() does invalidate() for you, but setAnimation doesn't. (read the comment on View.setAnimation() method in android's sources).
You can use AnimationSet