Android创建和配置布局动画


创建一个新工程LearnLayoutAnimation,带有BankActivity,并带上一个Fragment

activity_layout_animation.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:orientation="vertical"     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:animateLayoutChanges="true"     android:id="@+id/myContainer"     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="+增加按钮"         android:onClick="onClickbtnAddbtn"         android:id="@+id/btnAddbtn" />     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="设置Listview动画效果"         android:onClick="onClickSetListViewAnimation"         android:id="@+id/btnSetListViewAnimation" />     <ImageButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/btn_state_pressed_false"         android:id="@+id/imageButton" />     <ListView         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/listView" /> </LinearLayout>

 

LayoutAnimationActivity.java

public class LayoutAnimationActivity extends AppCompatActivity {     LinearLayout myConainer;     private View.OnClickListener btn_OnClickListener=new View.OnClickListener() {         @Override         public void onClick(View v) {             myConainer.removeView( v);         }     };     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_layout_animation);         myConainer= (LinearLayout) findViewById(R.id.myContainer);         ScaleAnimation sa=new ScaleAnimation(0,1,0,1);         sa.setDuration(1000);         LayoutAnimationController lac=new LayoutAnimationController(sa,0.2f);  //第二个参数是动画运行到百分多少时,第二个对象开始动画          lac.setOrder(LayoutAnimationController.ORDER_RANDOM);  //设置动画效果的顺序LayoutAnimationController下提供随机、从上往下、从下往上          myConainer.setLayoutAnimation(lac);     }     public void onClickbtnAddbtn(View view) {         Button btn=new Button(this);         btn.setText("删除我");         myConainer.addView(btn);         //设置动画  //        ScaleAnimation sa=new ScaleAnimation(0,1,0,1); //        sa.setDuration(1000);         RotateAnimation ra=new RotateAnimation(0,360,100,150);//0360读旋转100,50的中心的像素为中心点选择          ra.setDuration(1000);         LayoutAnimationController lac=new LayoutAnimationController(ra,0.2f);  //第二个参数是动画运行到百分多少时,第二个对象开始动画          lac.setOrder(LayoutAnimationController.ORDER_RANDOM);  //设置动画效果的顺序LayoutAnimationController下提供随机、从上往下、从下往上          myConainer.setLayoutAnimation(lac);         btn.setOnClickListener(btn_OnClickListener);     }     public void onClickSetListViewAnimation(View view) {         ArrayAdapter<String> adapter;         ListView listView;         listView= (ListView) findViewById(R.id.listView);         adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"hello caizhigui","你好呀 caizhigui","大家好才是真的好!!!"});         listView.setAdapter(adapter);         ScaleAnimation sa=new ScaleAnimation(0,1,0,1);         sa.setDuration(1000);         LayoutAnimationController lac=new LayoutAnimationController(sa,0.5f);  //第二个参数是动画运行到百分多少时,第二个对象开始动画          lac.setOrder(LayoutAnimationController.ORDER_RANDOM);  //设置动画效果的顺序LayoutAnimationController下提供随机、从上往下、从下往上          listView.setLayoutAnimation(lac);     } }

 




你可能感兴趣的:(android,布局动画)