Skip to content

Instantly share code, notes, and snippets.

@arpit
Created July 13, 2012 22:27
Show Gist options
  • Save arpit/3107951 to your computer and use it in GitHub Desktop.
Save arpit/3107951 to your computer and use it in GitHub Desktop.
Stagger LayoutTransitions in Android
package com.arpit.animatortest;
import android.animation.AnimatorSet;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AnimationTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_test);
LinearLayout l = (LinearLayout)findViewById(R.id.ll);
LayoutTransition transition = new LayoutTransition();
ObjectAnimator a1 = ObjectAnimator.ofFloat(null, View.SCALE_X, 0, 1);
ObjectAnimator a2 = ObjectAnimator.ofFloat(null, View.SCALE_Y, 0, 1);
ObjectAnimator a3 = ObjectAnimator.ofFloat(null, View.ALPHA, 0, 1);
AnimatorSet animator= new AnimatorSet();
animator.setStartDelay(0);
animator.playTogether(a1, a2, a3);
transition.setAnimator(LayoutTransition.APPEARING, animator);
transition.setStagger(LayoutTransition.CHANGE_APPEARING, 1000);
transition.setStagger(LayoutTransition.CHANGE_DISAPPEARING, 1000);
transition.setStagger(LayoutTransition.APPEARING, 1000);
transition.setStagger(LayoutTransition.DISAPPEARING, 1000);
transition.setDuration(5000);
l.setLayoutTransition(transition);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
for(int i=0; i<5; i++){
TextView t = new TextView(this);
t.setBackgroundColor(Color.RED);
t.setText("Hello "+i);
t.setLayoutParams(params);
l.addView(t);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_animation_test, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment