从布局动画引入ListView滑动时,每一Item项的显示动画

来源:互联网 发布:php继承一个普通类 编辑:程序博客网 时间:2024/06/11 04:11

 1、LayoutAnimation

     所谓的布局动画,其实就是为ViewGroup添加显示动画效果,主要用过LayoutAnimationController来控制实现。LayoutAnimationController用于为一个Layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果,可以在XML文件中设置,亦可以在Java代码中设置。

 1.1 在XML文件中设置布局动画

  首先,我们在res/anim文件夹下建立一个list_anim_layout.xml文件,该文件就是布局动画控制器。

<<span id="21_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="21_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=layout&k0=layout&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="21" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">layout</span></a></span>Animation xmlns:android="http://schemas.android.com/apk/res/android"    android:delay="30%"    android:animationOrder="random"    android:animation="@anim/slide_right" />
android:delay  子类动画时间间隔 (延迟)   70% 也可以是一个浮点数 如“1.2”等。
android:animationOrder="random"   子类的显示方式 random表示随机。
android:animationOrder 的取值有 : normal 0    默认,  reverse 1   倒序,  random 2   随机

android:animation="@anim/slide_right"表示列表项显示时的具体动画是什么!
下面,我们定义每一个列表项显示时的动画效果吧,及slide_right.xml:

<?<span id="19_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="19_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=xml&k0=xml&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="19" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">xml</span></a></span> version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/<span id="20_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="20_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=res&k0=res&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="20" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">res</span></a></span>/android"    android:interpolator="@android:anim/accelerate_interpolator">    <translate        android:duration="3000"        android:fromXDelta="100%p"        android:toXDelta="0%p" /></set>
显示的效果为ListView第一次出现的时候为 item随机出现 每个Item都是从右边的区域向左滑动到显示的地方。
接下来,你只需要把这个布局动画,指定到ViewGroup上就好了:

<ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutAnimation="@anim/list_anim_layout" >
</ListView>

1.2 在Java代码中实现布局动画

    在Java代码中实现布局动画并无难度,只要熟悉几个API的使用即可。 关于动画的定义和上文一致,只是,你不需要再在把控制动画应用到listview中了,即android:layoutAnimation="@anim/list_anim_layout"这行代码可以删除。

    接下来,需要在Java代码中进行配置:

   private void startLayoutAnim() {        //通过加载XML动画设置文件来创建一个Animation对象;        Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_right);        //得到一个LayoutAnimationController对象;        LayoutAnimationController lac = new LayoutAnimationController(animation);        //设置<span id="3_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=%BF%D8%BC%FE&k0=%BF%D8%BC%FE&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="3" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">控件</span></a></span>显示的顺序;        lac.setOrder(LayoutAnimationController.ORDER_REVERSE);        //设置控件显示间隔时间;        lac.setDelay(1);        //为ListView设置LayoutAnimationController属性;        listView.setLayoutAnimation(lac);    }

2、仿窝牛装修List列表的动画效果

    首先,我们定义一个动画资源,该动画即是列表项显示时的动画:woniu_list_item.xml

<?<span id="16_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="16_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=xml&k0=xml&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="16" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">xml</span></a></span> version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/<span id="17_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="17_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=res&k0=res&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="17" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">res</span></a></span>/android"    android:interpolator="@android:anim/accelerate_interpolator"    >    <!--    woniu list item animation    -->    <translate        android:duration="500"        android:fromXDelta="0"        android:fromYDelta="100"        android:toXDelta="0"        android:toYDelta="0" /></set>
    该平移动画表示,从下往上,垂直平移100px,时间为500毫秒。

    接下来,我们需要在BaseAdapter的getView()方法里,去使用该动画:

import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import <span id="13_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=java&k0=java&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="13" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">java</span></a></span>.util.List;public class WoniuListAdapter extends BaseAdapter {    private Context mContext;    private LayoutInflater mInflater;    private List<WoniuSimple> mDatas;    private Animation animation;    public WoniuListAdapter(Context context, List<WoniuSimple> datas) {        mContext = context;        mInflater = LayoutInflater.from(mContext);        mDatas = datas;        animation = AnimationUtils.loadAnimation(mContext, R.anim.woniu_list_item);    }    @Override    public int getCount() {        return (mDatas != null ? mDatas.size() : 0);    }    @Override    public Object getItem(int position) {        return (mDatas != null ? mDatas.get(position) : null);    }    @Override    public <span id="14_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=long&k0=long&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="14" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">long</span></a></span> getItemId(int position) {        return position;    }    @Override    public View getView(final int position, View convertView, ViewGroup parent) {        ViewHolder holder = null;        int type = getItemViewType(position);        if (convertView == null) {            // 下拉项布局            convertView = mInflater.inflate(R.<span id="15_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="15_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=layout&k0=layout&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="15" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">layout</span></a></span>.list_item_woniu, null);            holder = new ViewHolder();            holder.tem_img = (ImageView) convertView.findViewById(R.id.tem_img);            holder.text_name = (TextView) convertView.findViewById(R.id.text_name);            holder.text_name = (TextView) convertView.findViewById(R.id.text_name);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        convertView.startAnimation(animation);        final WoniuSimple materialSimple = mDatas.get(position);        if (materialSimple != null) {//            holder.tem_img.setImageResource(R.mipmap.assist_default_img);//            holder.text_name.setText(materialSimple.name);//            holder.text_mobile.setText(materialSimple.mobile);        }        return convertView;    }    class ViewHolder {        ImageView tem_img;        TextView text_name;        TextView text_mobile;    }}
   我们来简要分析应用动画的地方: 1、我们Adapter的构造方法里加载了之前定义的动画,活的Animation对象。 2、 我们在getView方法里,为convertView设置并启动Animation,即convertView.startAnimation(animation)。

    够简单吧,只是这么两行代码,就可以实现在加载每一个View Item时启动动画效果。

    可是,我们发现,这并不是非常完美的实现,为啥这么说呢?

    因为你此刻往上滑动列表,会发现,已经加载过的Item的动画还会再次启动执行一次。这个体验太糟糕了。为啥会出现这种情况啊?

    因为getVIew方法的调用时机会对动画产生影响。Adapter中的getView方法,会在每一个item处于可见状态时调用,所以无论你上滑还是下滑,都会重复调用getView方法(这也是ListView为啥在使用时要进行优化的地方)。


    所以,为了解决刚刚发生的问题,我们可以设置标识,进行判断,已经加载过的view的动画不再进行启动加载。

    完整的代码如下:

package com.lnyp.<span id="9_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=layout&k0=layout&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="9" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">layout</span></a></span>animation;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import <span id="10_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=java&k0=java&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="10" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">java</span></a></span>.util.HashMap;import java.util.List;import java.util.Map;public class WoniuListAdapter extends BaseAdapter {    private Context mContext;    private LayoutInflater mInflater;    private List<WoniuSimple> mDatas;    private Animation animation;    private Map<Integer, Boolean> isFrist;    public WoniuListAdapter(Context context, List<WoniuSimple> datas) {        mContext = context;        mInflater = LayoutInflater.from(mContext);        mDatas = datas;        animation = AnimationUtils.loadAnimation(mContext, R.anim.woniu_list_item);        isFrist = new HashMap<Integer, Boolean>();    }    @Override    public int getCount() {        return (mDatas != null ? mDatas.size() : 0);    }    @Override    public Object getItem(int position) {        return (mDatas != null ? mDatas.get(position) : null);    }    @Override    public <span id="11_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=long&k0=long&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="11" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">long</span></a></span> getItemId(int position) {        return position;    }    @Override    public View getView(final int position, View convertView, ViewGroup parent) {        ViewHolder holder = null;        int type = getItemViewType(position);        if (convertView == null) {            // 下拉项布局            convertView = mInflater.inflate(R.<span id="12_nwp" style="padding: 0px; width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=1045885ee9635bb3&k=layout&k0=layout&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=65035100_cpr&rb=0&rs=1&seller_id=1&sid=b35b63e95e884510&ssp2=1&stid=9&t=tpclicked3_hc&td=1836545&tu=u1836545&u=http%3A%2F%2Fwww%2Ebubuko%2Ecom%2Finfodetail%2D1061853%2Ehtml&urlid=0" target="_blank" mpid="12" style="padding: 0px; color: rgb(51, 51, 51); text-decoration: none;"><span style="padding: 0px; color: rgb(0, 0, 255); width: auto; height: auto;">layout</span></a></span>.list_item_woniu, null);            holder = new ViewHolder();            holder.tem_img = (ImageView) convertView.findViewById(R.id.tem_img);            holder.text_name = (TextView) convertView.findViewById(R.id.text_name);            holder.text_name = (TextView) convertView.findViewById(R.id.text_name);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        // 如果是第一次加载该view,则使用动画        if (isFrist.get(position) == null || isFrist.get(position)) {            convertView.startAnimation(animation);            isFrist.put(position, false);        }        final WoniuSimple materialSimple = mDatas.get(position);        if (materialSimple != null) {//            holder.tem_img.setImageResource(R.mipmap.assist_default_img);//            holder.text_name.setText(materialSimple.name);//            holder.text_mobile.setText(materialSimple.mobile);        }        return convertView;    }    class ViewHolder {        ImageView tem_img;        TextView text_name;        TextView text_mobile;    }}




0 0