Android中為ListView的Item選項設定出場動畫
LayoutAnimation用來給ViewGroup的子View新增動畫。
1.在res/anim資料夾建立檔案anim/layout_animation_item.xml,作用於ItemView的動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="50"
android:fillAfter="false">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.2" />
<scale
android:fromYScale="1"
android:toYScale="1.5"
android:pivotX="0.5"
android:pivotY="0.5"/>
<scale
android:fromYScale="1.5"
android:toYScale="1.0"
android:pivotX="0.5"
android:pivotY="0.5"/>
<alpha
android:fromAlpha="0.2"
android:toAlpha="0.1"/>
</set>
2.在res/anim資料夾建立檔案anim/layout_animation.xml,作用於ListView
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animationOrder="reverse"
android:delay="10"
android:interpolator="@android:interpolator/linear"
android:animation="@anim/layout_animation_item"/>
<!--
android:animationOrder="reverse"表示子Item反向出現
android:delay="10" 延時10毫秒
android:interpolator 設定線性插值器
android:animation="@anim/layout_animation_item" 為item引用View動畫
-->
3.在res/layout新建檔案layout/layout_animation_activity.xml,引用第二步的layout_animation
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--為ListView設定LayoutAnimation android:layoutAnimation="@anim/layout_animation"-->
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layout_animation"/>
</RelativeLayout>
4.在Activity中載入ListView.java
public class LayoutAnimationActivity extends Activity {
private ListView ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_animation_activity);
ListView=(ListView)findViewById(R.id.listView);
//建立分享選項
final List<ShareEntity> list=new ArrayList<ShareEntity>();
ShareEntity entity1=new ShareEntity();
entity1.setTvNameStr("支付寶好友");
entity1.setIvIconId(R.drawable.alipay);
list.add(entity1);
ShareEntity entity2=new ShareEntity();
entity2.setTvNameStr("微信好友");
entity2.setIvIconId(R.drawable.weixin);
list.add(entity2);
ShareEntity entity3=new ShareEntity();
entity3.setTvNameStr("朋友圈");
entity3.setIvIconId(R.drawable.weixin_friend);
list.add(entity3);
ShareEntity entity4=new ShareEntity();
entity4.setTvNameStr("新浪微博");
entity4.setIvIconId(R.drawable.sina_weibo);
list.add(entity4);
ShareEntity entity5=new ShareEntity();
entity5.setTvNameStr("支付寶好友");
entity5.setIvIconId(R.drawable.alipay);
list.add(entity5);
ShareEntity entity6=new ShareEntity();
entity6.setTvNameStr("微信好友");
entity6.setIvIconId(R.drawable.weixin);
list.add(entity6);
ShareEntity entity7=new ShareEntity();
entity7.setTvNameStr("朋友圈");
entity7.setIvIconId(R.drawable.weixin_friend);
list.add(entity7);
ShareEntity entity8=new ShareEntity();
entity8.setTvNameStr("新浪微博");
entity8.setIvIconId(R.drawable.sina_weibo);
list.add(entity8);
ShareEntity entity9=new ShareEntity();
entity9.setTvNameStr("支付寶好友");
entity9.setIvIconId(R.drawable.alipay);
list.add(entity9);
ShareEntity entity10=new ShareEntity();
entity10.setTvNameStr("微信好友");
entity10.setIvIconId(R.drawable.weixin);
list.add(entity10);
ShareEntity entity11=new ShareEntity();
entity11.setTvNameStr("朋友圈");
entity11.setIvIconId(R.drawable.weixin_friend);
list.add(entity11);
ShareEntity entity12=new ShareEntity();
entity12.setTvNameStr("新浪微博");
entity12.setIvIconId(R.drawable.sina_weibo);
list.add(entity12);
ShareEntity entity13=new ShareEntity();
entity13.setTvNameStr("支付寶好友");
entity13.setIvIconId(R.drawable.alipay);
list.add(entity13);
ShareEntity entity14=new ShareEntity();
entity14.setTvNameStr("微信好友");
entity14.setIvIconId(R.drawable.weixin);
list.add(entity14);
ShareEntity entity15=new ShareEntity();
entity15.setTvNameStr("朋友圈");
entity15.setIvIconId(R.drawable.weixin_friend);
list.add(entity15);
ShareEntity entity16=new ShareEntity();
entity16.setTvNameStr("新浪微博");
entity16.setIvIconId(R.drawable.sina_weibo);
list.add(entity16);
//設定介面卡
WindowUtilAdapter adapter=new WindowUtilAdapter(list,this);
ListView.setAdapter(adapter);
}
}
5.新建類WindowUtilAdapter.java
public class WindowUtilAdapter extends BaseAdapter {
private List<ShareEntity> list;
private Context context;
public WindowUtilAdapter(List<ShareEntity> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView= LayoutInflater.from(context).inflate(R.layout.window_util_item,parent,false);
holder.tvName=(TextView)convertView.findViewById(R.id.tvName);
holder.ivIcon=(ImageView)convertView.findViewById(R.id.ivIcon);
convertView.setTag(holder);
}else {
holder=(ViewHolder)convertView.getTag();
}
holder.tvName.setText(list.get(position).getTvNameStr());
holder.ivIcon.setBackgroundResource(list.get(position).getIvIconId());
return convertView;
}
static class ViewHolder{
TextView tvName;
ImageView ivIcon;
}
}
6.window_util_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="120dp">
<ImageView
android:id="@+id/ivIcon"
android:layout_width="45dp"
android:layout_height="45dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center|left"
android:layout_toRightOf="@+id/ivIcon"
android:textColor="@color/blue"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
7.演示效果