The following code shows how to Create Layout Changes Animation.
Main layout xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" > <Button android:id="@+id/button_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onAddClick" android:text="Click To Add Item" /> <LinearLayout android:id="@+id/verticalContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" android:orientation="vertical" /> </LinearLayout>
Main Activity Java code
import android.animation.Animator; import android.animation.LayoutTransition; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; //w ww . jav a 2s . c o m public class MainActivity extends Activity { LinearLayout mContainer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContainer = (LinearLayout) findViewById(R.id.verticalContainer); LayoutTransition transition = new LayoutTransition(); mContainer.setLayoutTransition(transition); Animator appearAnim = ObjectAnimator.ofFloat(null, "rotationY", 90f, 0f) .setDuration(transition.getDuration(LayoutTransition.APPEARING)); transition.setAnimator(LayoutTransition.APPEARING, appearAnim); Animator disappearAnim = ObjectAnimator.ofFloat(null, "rotationX", 0f, 90f) .setDuration(transition.getDuration(LayoutTransition.DISAPPEARING)); transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnim); PropertyValuesHolder pvhSlide = PropertyValuesHolder.ofFloat("y", 0, 1); PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.4f, 1f); PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.6f, 1f); Animator changingAppearingAnim = ObjectAnimator.ofPropertyValuesHolder(this, pvhSlide, pvhScaleY, pvhScaleX) .setDuration(transition.getDuration(LayoutTransition.CHANGE_DISAPPEARING)); transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changingAppearingAnim); } public void onAddClick(View v) { Button button = new Button(this); button.setText("Click To Remove"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mContainer.removeView(v); } }); mContainer.addView(button, new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); } }