【转】ListView,GridView之LayoutAnimation特殊动画的实现 ps:需要学习的是在getView中添加动画的思想

LayoutAnimation干嘛用的?不知道的话网上搜一下。 
Android的Animation之LayoutAnimation使用方法 

有两种用法,我的通常写在代码中,像下面这样: 

 1 /**
 2      * Layout动画
 3      * 
 4      * @return
 5      */
 6     protected LayoutAnimationController getAnimationController() {
 7         int duration=300;
 8         AnimationSet set = new AnimationSet(true);
 9 
10         Animation animation = new AlphaAnimation(0.0f, 1.0f);
11         animation.setDuration(duration);
12         set.addAnimation(animation);
13 
14         animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
15                 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
16                 -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
17         animation.setDuration(duration);
18         set.addAnimation(animation);
19 
20         LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
21         controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
22         return controller;
23     }

应用的时候只需这样:

1 listView = (ListView) findViewById(R.id.listView);
2 listView.setLayoutAnimation(getAnimationController());
3 adapter = new ListViewAdapter(stores);
4 listView.setAdapter(adapter);

这样一个简单的LayoutAnimation就完成了。 

别看到这里就以为文章就完了,以上都是些小玩意。呵呵,还有更厉害的! 

你想设置更炫的动画吗?LayoutAnimation通常是Item一个一个的出现,有某种规律的。想让每个Item都有自己的动画吗?那就继续看下去。 

 1 .......
 2 private int duration=1000;
 3         private Animation push_left_in,push_right_in;
 4         private Animation slide_top_to_bottom,slide_bottom_to_top;
 5         public ListViewAdapter(ArrayList<Store> list) {
 6             this.list = list;
 7             push_left_in=AnimationUtils.loadAnimation(context, R.anim.push_left_in);
 8             push_right_in=AnimationUtils.loadAnimation(context, R.anim.push_right_in);
 9             slide_top_to_bottom=AnimationUtils.loadAnimation(context, R.anim.slide_top_to_bottom);
10             slide_bottom_to_top=AnimationUtils.loadAnimation(context, R.anim.slide_bottom_to_top);
11         }
12 ........
13 
14 @Override
15         public View getView(int position, View convertView, ViewGroup parent) {
16             // TODO Auto-generated method stub
17             ViewHodler hodler;
18             if (convertView == null) {
19                 hodler = new ViewHodler();
20                 convertView = LayoutInflater.from(context).inflate(
21                         R.layout.simple_item_7_for_main, null);
22                 ........
23                 
24                 
25                 convertView.setTag(hodler);
26                 
27                 if (position % 2 == 0) {
28                     push_left_in.setDuration(duration);
29                     convertView.setAnimation(push_left_in);
30                 } else {
31                     push_right_in.setDuration(duration);
32                     convertView.setAnimation(push_right_in);
33                 }
34                 
35                 /*if(position==0){
36                     slide_bottom_to_top.setDuration(duration);
37                     convertView.setAnimation(slide_bottom_to_top);
38                 }
39                 else{
40                     slide_top_to_bottom.setDuration(duration);
41                     convertView.setAnimation(slide_top_to_bottom);
42                 }*/
43                 
44             }else{
45                 hodler = (ViewHodler) convertView.getTag();
46             }
47 ........
48             
49             
50             return convertView;
51         }

看见上面的动画设置了吗?将动画写在getView()中,这样可以设置很多不同的动画。其实这不属于LayoutAnimation的范畴了。 
你可以试一下,如果设计好的话,可以有比LayoutAnimation更酷的效果。 
我这里只试了两种效果。 
下面是我的动画文件,共四个: 
第一种效果:item分别从左右两侧滑入效果。 
push_left_in.xml 

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="300"/>
4     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
5 </set>

push_right_in.xml 

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
4     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
5 </set>

 

第2种效果:第一个item从下往上滑入,其他Item从上往下滑入效果,这个效果如果单个Item比较高(height)的话效果非常酷(卡牛的老版本好像用的就是这种效果)。 
slide_bottom_to_top.xml 

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
3     <translate android:fromYDelta="100%" android:toXDelta="0" android:duration="300" />
4     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
5 </set>

slide_top_to_bottom.xml 

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
3     <translate android:fromYDelta="-100%" android:toXDelta="0" android:duration="300" />
4     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
5 </set>

另外一篇: 
这个不是我写的。 
GridView的item从上下左右飞入 

  1 import java.util.Random;
  2 import android.app.Activity;
  3 import android.content.Context;
  4 import android.os.Bundle;
  5 import android.view.LayoutInflater;
  6 import android.view.View;
  7 import android.view.View.OnClickListener;
  8 import android.view.ViewGroup;
  9 import android.view.animation.Animation;
 10 import android.view.animation.TranslateAnimation;
 11 import android.widget.BaseAdapter;
 12 import android.widget.Button;
 13 import android.widget.GridView;
 14 import android.widget.ImageView;
 15 public class ZdemoActivity extends Activity {
 16  
 17  private GridView gv;
 18  private Button btn;
 19  private TranslateAnimation taLeft, taRight, taTop, taBlow;
 20  private int[] imgList = new int[15];
 21  private MyAdapter mAdapter;
 22  private LayoutInflater mInflater;
 23  @Override
 24  public void onCreate(Bundle savedInstanceState) {
 25   super.onCreate(savedInstanceState);
 26   setContentView(R.layout.main);
 27   this.InitView();
 28   this.InitAnima();
 29   this.InitData();
 30  }
 31  private void InitAnima() {
 32   // TODO Auto-generated method stub
 33   taLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f,
 34     Animation.RELATIVE_TO_PARENT, 0.0f,
 35     Animation.RELATIVE_TO_PARENT, 0.0f,
 36     Animation.RELATIVE_TO_PARENT, 0.0f);
 37   taRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f,
 38     Animation.RELATIVE_TO_PARENT, 0.0f,
 39     Animation.RELATIVE_TO_PARENT, 0.0f,
 40     Animation.RELATIVE_TO_PARENT, 0.0f);
 41   taTop = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
 42     Animation.RELATIVE_TO_PARENT, 0.0f,
 43     Animation.RELATIVE_TO_PARENT, 1.0f,
 44     Animation.RELATIVE_TO_PARENT, 0.0f);
 45   taBlow = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
 46     Animation.RELATIVE_TO_PARENT, 0.0f,
 47     Animation.RELATIVE_TO_PARENT, -1.0f,
 48     Animation.RELATIVE_TO_PARENT, 0.0f);
 49   taLeft.setDuration(1000);
 50   taRight.setDuration(1000);
 51   taTop.setDuration(1000);
 52   taBlow.setDuration(1000);
 53  }
 54  private void InitData() {
 55   // TODO Auto-generated method stub
 56   for (int i = 0; i < 15; i++) {
 57    imgList[i] = R.drawable.ic_launcher;
 58   }
 59   mAdapter = new MyAdapter();
 60   gv.setAdapter(mAdapter);
 61  }
 62  private void InitView() {
 63   // TODO Auto-generated method stub
 64   gv = (GridView) findViewById(R.id.gridView1);
 65   btn = (Button) findViewById(R.id.button1);
 66   btn.setOnClickListener(new OnClickListener() {
 67    @Override
 68    public void onClick(View v) {
 69     // TODO Auto-generated method stub
 70     mAdapter = null;
 71     mAdapter = new MyAdapter();
 72     gv.setAdapter(mAdapter);
 73     mAdapter.notifyDataSetChanged();
 74    }
 75   });
 76   mInflater = (LayoutInflater) this
 77     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 78  }
 79  private class MyAdapter extends BaseAdapter {
 80   @Override
 81   public int getCount() {
 82    // TODO Auto-generated method stub
 83    return imgList.length;
 84   }
 85   @Override
 86   public Object getItem(int position) {
 87    // TODO Auto-generated method stub
 88    return imgList[position];
 89   }
 90   @Override
 91   public long getItemId(int position) {
 92    // TODO Auto-generated method stub
 93    return position;
 94   }
 95   @Override
 96   public View getView(int position, View convertView, ViewGroup parent) {
 97    // TODO Auto-generated method stub
 98    ViewHolder holder = null;
 99    if (convertView == null) {
100     convertView = mInflater.inflate(R.layout.item, null);
101     holder = new ViewHolder();
102     holder.image = (ImageView) convertView
103       .findViewById(R.id.imageView1);
104     convertView.setTag(holder);
105    } else {
106     holder = (ViewHolder) convertView.getTag();
107    }
108    int imgID = imgList[position];
109    holder.image.setImageResource(imgID);
110    Random ran = new Random();
111    int rand = ran.nextInt(4);
112    switch (rand) {
113    case 0:
114     convertView.startAnimation(taLeft);
115     break;
116    case 1:
117     convertView.startAnimation(taRight);
118     break;
119    case 2:
120     convertView.startAnimation(taTop);
121     break;
122    case 3:
123     convertView.startAnimation(taBlow);
124     break;
125    }
126    return convertView;
127   }
128   class ViewHolder {
129    public ImageView image;
130   }
131  }
132 }

原文地址:http://gundumw100.iteye.com/blog/1874545

 

另外附上android animation学习的博文:

http://blog.csdn.net/tianjf0514/article/details/7566304

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=564

posted @ 2014-05-22 11:27  沙发土豆  阅读(395)  评论(0编辑  收藏  举报