最近项目要用到很多动画,涵盖item删除、listView item滑动、listView item替换等效果,在gitHub上找到了一个ListViewAnimation的开源项目,但是该项目要依赖一些jar和库,会导致项目过大,经过不懈地努力,在google找到了一个比较轻量级的listView item删除动画,现在给大家分享下,代码我就直接贴上了。
1 java代码
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.ScaleAnimation; import android.view.animation.Transformation; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { static final int ANIMATION_DURATION = 200; private static List<MyCell> mAnimList = new ArrayList<MyCell>(); private MyAnimListAdapter mMyAnimListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i=0;i<50;i++) { MyCell cell = new MyCell(); cell.name = "Cell No."+Integer.toString(i); mAnimList.add(cell); } mMyAnimListAdapter = new MyAnimListAdapter(this, R.layout.chain_cell, mAnimList); ListView myListView = (ListView) findViewById(R.id.chainListView); myListView.setAdapter(mMyAnimListAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private void deleteCell(final View v, final int index) { AnimationListener al = new AnimationListener() { @Override public void onAnimationEnd(Animation arg0) { mAnimList.remove(index); ViewHolder vh = (ViewHolder)v.getTag(); vh.needInflate = true; mMyAnimListAdapter.notifyDataSetChanged(); } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) {} }; collapse(v, al); } private void collapse(final View v, AnimationListener al) { final int initialHeight = v.getMeasuredHeight(); Animation anim = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { if (interpolatedTime == 1) { v.setVisibility(View.GONE); } else { v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime); v.requestLayout(); } } @Override public boolean willChangeBounds() { return true; } }; if (al!=null) { anim.setAnimationListener(al); } anim.setDuration(ANIMATION_DURATION); v.startAnimation(anim); } private class MyCell { public String name; } private class ViewHolder { public boolean needInflate; public TextView text; ImageButton imageButton; } public class MyAnimListAdapter extends ArrayAdapter<MyCell> { private LayoutInflater mInflater; private int resId; public MyAnimListAdapter(Context context, int textViewResourceId, List<MyCell> objects) { super(context, textViewResourceId, objects); this.resId = textViewResourceId; this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(final int position, View convertView, ViewGroup parent) { final View view; ViewHolder vh; MyCell cell = (MyCell)getItem(position); if (convertView==null) { view = mInflater.inflate(R.layout.chain_cell, parent, false); setViewHolder(view); } else if (((ViewHolder)convertView.getTag()).needInflate) { view = mInflater.inflate(R.layout.chain_cell, parent, false); setViewHolder(view); } else { view = convertView; } vh = (ViewHolder)view.getTag(); vh.text.setText(cell.name); vh.imageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { deleteCell(view, position); } }); return view; } private void setViewHolder(View view) { ViewHolder vh = new ViewHolder(); vh.text = (TextView)view.findViewById(R.id.cell_name_textview); vh.imageButton = (ImageButton) view.findViewById(R.id.cell_trash_button); vh.needInflate = false; view.setTag(vh); } } }
2 activity_main.xml配置:
<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" tools:context=".MainActivity" > <ListView android:id="@+id/chainListView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
3 chain_cell.xml配置
<?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="wrap_content" android:orientation="horizontal" > <ImageButton android:id="@+id/cell_trash_button" android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/trash_can" android:scaleType="fitXY" /> <TextView android:id="@+id/cell_name_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|left" android:layout_marginLeft="16dp" android:layout_weight="1" android:text="cell name" /> </LinearLayout>
4 效果