1 import android.app.Activity; 2 import android.app.Fragment; 3 import android.os.Bundle; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.view.animation.LayoutAnimationController; 8 import android.view.animation.ScaleAnimation; 9 import android.widget.LinearLayout; 10 11 public class MainActivity extends Activity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 if (savedInstanceState == null) { 19 getFragmentManager().beginTransaction() 20 .add(R.id.container, new PlaceholderFragment()).commit(); 21 } 22 } 23 25 public static class PlaceholderFragment extends Fragment { 26 27 public PlaceholderFragment() { 28 } 29 30 @Override 31 public View onCreateView(LayoutInflater inflater, ViewGroup container, 32 Bundle savedInstanceState) { 33 34 final LinearLayout rootView = (LinearLayout) inflater.inflate( 35 R.layout.fragment_main, container, false); 36 37 /** 38 * *** ScaleAnimation 缩放动画效果 *** 39 * float fromX 动画起始时 X坐标上的伸缩尺寸。 40 * float toX 动画结束时 X坐标上的伸缩尺寸。 41 * float fromY 动画起始时Y坐标上的伸缩尺寸。 42 * float toY 动画结束时Y坐标上的伸缩尺寸。 43 */ 44 ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1); 45 sa.setDuration(5000); // 5秒 设置持续时间 46 47 /** 48 * LayoutAnimationController 控制一组控件按照规定显示. 49 * delay 推迟。 50 */ 51 LayoutAnimationController lac = new LayoutAnimationController(sa, 52 0.5f); 53 54 /** 55 * LayoutAnimationController.ORDER_NORMAL 顺序显示 56 * LayoutAnimationController.ORDER_REVERSE 反显示(从下往上) 57 * LayoutAnimationController.ORDER_RANDOM 随机显示 58 */ 59 lac.setOrder(LayoutAnimationController.ORDER_REVERSE); 60 61 rootView.setLayoutAnimation(lac); 62 63 return rootView; 64 } 65 } 66 67 }
activity_main.xml
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" />
fragment_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.jikexueyuan.layoutanimationinlinearlayout.MainActivity$PlaceholderFragment" > 7 8 <Button 9 android:id="@+id/Button05" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="Button" /> 13 14 <Button 15 android:id="@+id/Button04" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="Button" /> 19 20 <Button 21 android:id="@+id/Button03" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="Button" /> 25 26 <Button 27 android:id="@+id/Button02" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="Button" /> 31 32 <Button 33 android:id="@+id/Button01" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:text="Button" /> 37 38 <Button 39 android:id="@+id/button1" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="Button" /> 43 44 </LinearLayout>