Android中ListView添加動畫

Android開發中最常用的ListView以及GridView可以實現多種的動畫,爲我們的應用增加視覺的體驗。比如攜程機票搜索的列表頁、卡牛等。今天咱們就看看如何實現炫酷的列表動畫效果。

首先如果只是對列表中的沒一個item使用同一個動畫效果,我們可以使用LayoutAnimation,顧名思義,是用來設置給viewgroup類型的animation,是子view來執行的。可以兩種實現方式

第一種方式:
    XML文件中使用::android:layoutAnimation="@anim/popinlayout"
第二種方式:
    java代碼中動態設置::viewGrop.setLayoutAnimation(LayoutAnimationController);

和Animation類似,Layout Animation也支持AnimationListener,在動畫開始或者結束做一些處理,具體的就不多說了。layoutanimation會在View Group第一次進行佈局的時候執行一次。
具體來說,layoutAnimation支持三個參數,

  • anim就是要執行的動畫比如平移、旋轉、透明度以及縮放
  • animationOrder,這個是說子view按照什麼順序來執行anim,可以使normal(正常,0-n),reverse(反序,n-0),random爲隨機。一般不要太亂的還是normal
  • delay,用於延遲的每個子view的動畫開始動畫持續時間的浮點數。越大間隔越長。0.3或者30%的字樣

另外:LayoutAnimationController包含一個內部類,LayoutAnimationController.AnimationParameters,這個類主要包括了count和index兩個參數。這些參數用於計算每個單獨的視圖動畫的開始時間。ViewGroup.LayoutParams這個類大家都一定很熟悉的,主要是height和width。

在XMl使用動畫

使用xml文件創建動畫


> 在res/anim文件夾先新建一個xml文件,list_anim_layout.xml


    <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"  
        android:delay="30%"  
        android:animationOrder="normal"  
        android:animation="@anim/slide_bottom" /> 



> 同時創建slide_bottom.xml動畫文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="10%p"
        android:toYDelta="0"
        android:duration="300"
        android:startOffset="100"/>
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="300"
        android:startOffset="100"/>
</set>

ListView佈局中使用設置的動畫文件即可

<ListView  
        android:id="@+id/listview"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:persistentDrawingCache="animation|scrolling"   
        android:layoutAnimation="@anim/list_anim_layout" 
        />  
    -

第二種設置方法:java代碼中動態設置

//通過加載XML動畫設置文件來創建一個Animation對象;
       Animation animation=AnimationUtils.loadAnimation(this, R.anim.slide_bottom);
       //得到一個LayoutAnimationController對象;
       LayoutAnimationController lac=new LayoutAnimationController(animation);
       //設置控件顯示的順序;
       lac.setOrder(LayoutAnimationController.ORDER_REVERSE);
       //設置控件顯示間隔時間;
       lac.setDelay(1);
       //爲ListView設置LayoutAnimationController屬性;
       datalist.setLayoutAnimation(lac);

有時候我們可以在代碼中一般爲工具類中,創建一個加載動畫的方法 比如:

    /** 
     * Layout動畫 
     *  
     * @return 
     */  
    public static LayoutAnimationController getAnimationController() {  
        int duration=300;  
        AnimationSet set = new AnimationSet(true);  

        Animation animation = new AlphaAnimation(0.0f, 1.0f);  
        animation.setDuration(duration);  
        set.addAnimation(animation);  

        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,  
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,  
                -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);  
        animation.setDuration(duration);  
        set.addAnimation(animation);  

        LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);  
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
        return controller;  
    }

使用的時候既可以直接調用該工具類的方法即可

listView = (ListView) findViewById(R.id.listView);  
listView.setLayoutAnimation(AnimUtil.getAnimationController());  
adapter = new ListViewAdapter(stores);  
listView.setAdapter(adapter);  

~~到這裏你已經可以爲你的ListView或者GridView設置比較炫的動畫了,,,不過還可以設置更加炫酷的不一樣的動畫!!!

  • 爲每個Item設置不同效果的動畫,通過Adapter中的getView方法爲特定的Item設置動畫
        private int duration=1000;  
        private Animation push_left_in,push_right_in;  
        private Animation slide_top_to_bottom,slide_bottom_to_top;  
        public ListViewAdapter(ArrayList<Store> list) {  
            this.list = list;  
            push_left_in=AnimationUtils.loadAnimation(context, R.anim.push_left_in);  
            push_right_in=AnimationUtils.loadAnimation(context, R.anim.push_right_in);  
            slide_top_to_bottom=AnimationUtils.loadAnimation(context, R.anim.slide_top_to_bottom);  
            slide_bottom_to_top=AnimationUtils.loadAnimation(context, R.anim.slide_bottom_to_top);  
        }  
        ........  

        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
            ViewHodler hodler;  
            if (convertView == null) {  
                hodler = new ViewHodler();  
                convertView = LayoutInflater.from(context).inflate(  
                        R.layout.simple_item_7_for_main, null);  
                ........    
                convertView.setTag(hodler);  

                if (position % 2 == 0) { //具體根據應用需求判斷
                    push_left_in.setDuration(duration);  
                    convertView.setAnimation(push_left_in);  
                } else {  
                    push_right_in.setDuration(duration);  
                    convertView.setAnimation(push_right_in);  
                }  

                /*if(position==0){ 
                    slide_bottom_to_top.setDuration(duration); 
                    convertView.setAnimation(slide_bottom_to_top); 
                } 
                else{ 
                    slide_top_to_bottom.setDuration(duration); 
                    convertView.setAnimation(slide_top_to_bottom); 
                }*/  

            }else{  
                hodler = (ViewHodler) convertView.getTag();  
            }  
            ........  
            return convertView;  
        }  

push_left_in,push_right_in,slide_top_to_bottom,slide_bottom_to_top; 爲四個不同的動畫。。。。。GridView和ListView一樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章