[Android Pro] ListView,GridView之LayoutAnimation特殊动画的实现

转载自:http://gundumw100.iteye.com/blog/1874545

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

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

    /**
         * Layout动画
         *
         * @return
         */
        protected 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(getAnimationController());
adapter = new ListViewAdapter(stores);
listView.setAdapter(adapter); 

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

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

你想设置更炫的动画吗?LayoutAnimation通常是Item一个一个的出现,有某种规律的。想让每个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;
        }

看见上面的动画设置了吗?将动画写在getView()中,这样可以设置很多不同的动画。其实这不属于LayoutAnimation的范畴了。
你可以试一下,如果设计好的话,可以有比LayoutAnimation更酷的效果。

我这里只试了两种效果。

下面是我的动画文件,共四个:

第一种效果:item分别从左右两侧滑入效果。

push_left_in.xml

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

push_right_in.xml

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

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

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

slide_top_to_bottom.xml

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

另外一篇:
这个不是我写的。

GridView的item从上下左右飞入

import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class ZdemoActivity extends Activity {

 private GridView gv;
 private Button btn;
 private TranslateAnimation taLeft, taRight, taTop, taBlow;
 private int[] imgList = new int[15];
 private MyAdapter mAdapter;
 private LayoutInflater mInflater;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  this.InitView();
  this.InitAnima();
  this.InitData();
 }
 private void InitAnima() {
  // TODO Auto-generated method stub
  taLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
  taRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
  taTop = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
  taBlow = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, -1.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
  taLeft.setDuration(1000);
  taRight.setDuration(1000);
  taTop.setDuration(1000);
  taBlow.setDuration(1000);
 }
 private void InitData() {
  // TODO Auto-generated method stub
  for (int i = 0; i < 15; i++) {
   imgList[i] = R.drawable.ic_launcher;
  }
  mAdapter = new MyAdapter();
  gv.setAdapter(mAdapter);
 }
 private void InitView() {
  // TODO Auto-generated method stub
  gv = (GridView) findViewById(R.id.gridView1);
  btn = (Button) findViewById(R.id.button1);
  btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mAdapter = null;
    mAdapter = new MyAdapter();
    gv.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();
   }
  });
  mInflater = (LayoutInflater) this
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }
 private class MyAdapter extends BaseAdapter {
  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return imgList.length;
  }
  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return imgList[position];
  }
  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   ViewHolder holder = null;
   if (convertView == null) {
    convertView = mInflater.inflate(R.layout.item, null);
    holder = new ViewHolder();
    holder.image = (ImageView) convertView
      .findViewById(R.id.imageView1);
    convertView.setTag(holder);
   } else {
    holder = (ViewHolder) convertView.getTag();
   }
   int imgID = imgList[position];
   holder.image.setImageResource(imgID);
   Random ran = new Random();
   int rand = ran.nextInt(4);
   switch (rand) {
   case 0:
    convertView.startAnimation(taLeft);
    break;
   case 1:
    convertView.startAnimation(taRight);
    break;
   case 2:
    convertView.startAnimation(taTop);
    break;
   case 3:
    convertView.startAnimation(taBlow);
    break;
   }
   return convertView;
  }
  class ViewHolder {
   public ImageView image;
  }
 }
}

[Android Pro] ListView,GridView之LayoutAnimation特殊动画的实现

时间: 2025-01-05 07:13:25

[Android Pro] ListView,GridView之LayoutAnimation特殊动画的实现的相关文章

【转】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 Animatio

Android之ListView/GridView 优化

一.效率最低的getView实现 我们知道,ListView和GridView的显示都是通过Adapter的getView实现的. ListView/GridView数据量较小时,我们的处理方式一般是这样的(效率最低的一种方式) 1 public View getView(int position, View convertView, ViewGroup parent) { 2 View item = mInflater.inflate(R.layout.list_item_icon_text,

Android实现ListView或GridView首行/尾行距离屏幕边缘距离

Android上ListView&GridView默认行都是置顶的,这样会很丑. 一般为了解决这个问题都会在首行或尾行加上一个隐藏的View,那样实在是太麻烦了.在网上看博客的时候突然看到这个属性真的很有用! 直接上关键属性: 设置ListView或GridView的android:clipToPadding = true, 然后通过paddingTop和paddingBottom设置距离就好了. 博客原文: http://www.cnblogs.com/xitang/p/3606578.htm

Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

Android PullToRefresh (ListView GridView 下拉刷新) 使用详解 标签: Android下拉刷新pullToRefreshListViewGridView 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-r

Android研究之手PullToRefresh(ListView GridView 下拉刷新)使用详解

 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,例子中的功能极其强大,支持很多控件.本篇博客详细给大家介绍下ListView和GridView利用pull-to-rerfesh 实现下拉刷新和上拉加载更多.对布局不清楚的可以看Android研究自定义ViewGroup实现FlowLayout 详解. 1.ListView下拉刷新快速入门 pull-to-refresh对ListView进行了封装,叫做:PullToRefreshList

Android PullToRefresh (ListView GridView 下拉刷新) 使用详解 (转载)

最近项目用到下拉刷新,上来加载更多,这里对PullToRefresh这控件进行了解和使用. 以下内容转载自:http://blog.csdn.net/lmj623565791/article/details/38238749 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,例子中的功能极其强大,支持很多控件.本篇博客详细给大家介绍下ListView和GridView利用pull-to-rerfesh 实现下拉刷新和上拉加载更多. 1.List

Android ListView,GridView,ScrollView,ProgressBar,SeekBar,RelativeLayout,EditText常用属性

ListView的一些特殊属性: 1.ListView的XML属性 [java] view plaincopy android:divider//在列表条目之间显示的drawable或color android:dividerHeight//用来指定divider的高度 android:entries//构成ListView的数组资源的引用.对于某些固定的资源,这个属性提供了比在程序中添加资源更加简便的方式 android:footerDividersEnabled//当设为false时,Lis

【转载】 Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

Android下拉刷新pullToRefreshListViewGridView 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,例子中的功能极其强大,支持很多控件.本篇博客详细给大家介绍下ListView和GridView利用pull-to-rerfesh 实现下拉刷新和上拉加载更

Android 快速开发系列 打造万能的ListView GridView 适配器

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写得最多的就是ListView,GridView的适配器吧,记得以前开发一同事开发项目,一个项目下来基本就一直在写ListView的Adapter都快吐了~~~对于Adapter一般都继承BaseAdapter复写几个方法,getView里面使用ViewHolder模式,其实大部分的代码基本都是类似的