I wrote about a new approach to animate multiple objects in Android one after another. For example, 6 ImageButtons and 6 TextViews. I decided to duplicate this solution in my blog.
My approach to reach this goal can be useful when you need to animate a lot of views one after another. You can use setStartOffset
method to set a delay before animation begins. So, if you know, how much time will take for your first animation to end, you can set this as a delay for your second animation. This is an example where I animated six ImageButtons
and six TextViews
below them one after another:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public void animateButtons() { // An array of buttons int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton}; // Array of textViews int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView}; int i = 1; for (int viewId : imageButtonIds) { ImageButton imageButton = (ImageButton) findViewById(viewId); // Animation from a file fade.xml in folder res/anim Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade); // Delay for each animation is 100 ms bigger than for previous one fadeAnimation.setStartOffset(i * 100); imageButton.startAnimation(fadeAnimation); // The same animation is for textViews int textViewId = textViewIds[i-1]; TextView textView = (TextView) findViewById(textViewId); textView.startAnimation(fadeAnimation); i ++; } } |
In my res/anim
folder I have a file, called fade.xml
with these contents:
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <!-- Fade animation with 500 ms duration --> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /> |
Here http://stackoverflow.com/questions/3519682/android-animation-one-after-other is this post and two other approaches – using AnimationSet and using listeners.