Android的Animation之LayoutAnimation使用方法



有两种用法,我的通常写在代码中,像下面这样: 
Java代码  收藏代码
  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.     }  


应用的时候只需这样: 
Java代码  收藏代码
  1. listView = (ListView) findViewById(R.id.listView);  
  2. listView.setLayoutAnimation(getAnimationController());  
  3. adapter = new ListViewAdapter(stores);  
  4. listView.setAdapter(adapter);  


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

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

你想设置更炫的动画吗?LayoutAnimation通常是Item一个一个的出现,有某种规律的。想让每个Item都有自己的动画吗?那就继续看下去。 
Java代码  收藏代码
  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 
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 
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 
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 
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从上下左右飞入 
Java代码  收藏代码
  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. }  
第 1 章 Lin u x 安 全 渗 透 简 介 1.1 什 么 是 安 全 渗 透 1.2 安 全 渗 透 所 需 的 工 具 1.3 K ali Lin u x 简 介 1.4 安 装 K ali Lin u x 1.5 K ali更 新 与 升 级 1.6 基 本 设 置 第 2 章 配 置 K ali Lin u x 2.1 准 备 内 核 头 文 件 2.2 安 装 并 配 置 N VIDIA 显 卡 驱 动 2.3 应 用 更 新 和 配 置 额 外 安 全 工 具 2.4 设 置 P r o x y C h ain s 2.5 目 录 加 密 第 3 章 高 级 测 试 实 验 室 3.1 使 用 V M w a r e W o r k s t a tio n 3.2 攻 击 W o r d P r e s s 和 其 他 应 用 程 序 第 4 章 信 息 收 集 4.1 枚 举 服 务 4.2 测 试 网 络 范 围 4.3 识 别 活 跃 的 主 机 4.4 查 看 打 开 的 端 口 4.5 系 统 指 纹 识 别 4.6 服 务 的 指 纹 识 别 4.7 其 他 信 息 收 集 手 段 4.8 使 用 M alt e g o 收 集 信 息 4.9 绘 制 网 络 结 构 图 第 5 章 漏 洞 扫 描 5.1 使 用 N e s s u s 5.2 使 用 O p e n VA S 第 6 章 漏 洞 利 用 6.1 M e t a s ploit a ble 操 作 系 统 6.2 M e t a s ploit 基 础 6.3 控 制 M e t e r p r e t e r 6.4 渗 透 攻 击 应 用 大 学 霸 K ali Lin u x 安 全 渗 透 教 程 2 6.5 7 7.1 7.2 7.3 7.4 8 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 6.5 免杀Payload生成工具Veil 第7章 权限提升 7.1 使用假冒令牌 7.2 本地权限提升 7.3 使用社会工程学工具包(SET) 7.4 使用SET实施攻击 第8章 密码攻击 8.1 密码在线破解 8.2 分析密码 8.3 破解LM Hashes密码 8.4 绕过Utilman登录 8.5 破解纯文本密码工具mimikatz 8.6 破解操作系统用户密码 8.7 创建密码字典 8.8 使用NVIDIA计算机统一设备架构(CUDA) 8.9 物理访问攻击 第9章 无线网络渗透测试 9.1 无线网络嗅探工具Kismet 9.2 使用Aircrack-ng工具破解无线网络 9.3 Gerix Wifi Cracker破解无线网络 9.4 使用Wifite破解无线网络 9.5 使用Easy-Creds工具攻击无线网络 9.6 在树莓派上破解无线网络 9.7 攻击路由器 9.8 Arpspoof工具
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值