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一样

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