天天看點

android layoutanimation 實作listview /gradview 的動畫效果

package com.example.scrool;


 import android.app.Activity;

 import android.os.Bundle;

 import android.view.animation.AlphaAnimation;

 import android.view.animation.Animation;

 import android.view.animation.AnimationSet;

 import android.view.animation.LayoutAnimationController;

 import android.view.animation.RotateAnimation;

 import android.view.animation.ScaleAnimation;

 import android.view.animation.TranslateAnimation;

 import android.widget.ArrayAdapter;

 import android.widget.ListView;


 public class MainActivity extends Activity {


     private ListView listView;//滾動


     @Override

     protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

         String[] arraycontent= { "Afghanistan", "Albania", "Algeria", 

                 "American Samoa", "Andorra", "Angola", "Anguilla", 

                 "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", 

                 "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", 

                 "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", 

                 "Benin", "Bermuda", "Bhutan", "Bolivia", 

                 "Bosnia and Herzegovina", "Botswana", "Bouvet Island" };

          ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( 

          MainActivity.this, android.R.layout.simple_list_item_1, 

          arraycontent); 

         listView = (ListView) findViewById(R.id.lv_mylist);

         listView.setAdapter(arrayAdapter);

         Animation();

         

     

     }


     private void Animation() {

         AnimationSet set = new AnimationSet(false);

         Animation animation = new AlphaAnimation(0,1);   //AlphaAnimation 控制漸變透明的動畫效果

         animation.setDuration(500);     //動畫時間毫秒數

         set.addAnimation(animation);    //加入動畫集合


         animation = new TranslateAnimation(1, 13, 10, 50);  //ScaleAnimation 控制尺寸伸縮的動畫效果

         animation.setDuration(300);

         set.addAnimation(animation);


         animation = new RotateAnimation(30,10);    //TranslateAnimation  控制畫面平移的動畫效果

         animation.setDuration(300);

         set.addAnimation(animation);


         animation = new ScaleAnimation(5,0,2,0);    //RotateAnimation  控制畫面角度變化的動畫效果

         animation.setDuration(300);

         set.addAnimation(animation);


         LayoutAnimationController controller = new LayoutAnimationController(set, 2);



         /*GridView gridView = (GridView) this.findViewById(R.id.gridview);

         gridView .setLayoutAnimation(controller);  //GridView 設定動畫效果

 */

         ListView listview= (ListView)this.findViewById(R.id.lv_mylist);

         listview.setLayoutAnimation(controller);   //ListView 設定動畫效果

         

     }

     

     


 }