Chinaunix首页 | 论坛 | 博客
  • 博客访问: 305132
  • 博文数量: 65
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-21 15:10
文章分类
文章存档

2016年(3)

2015年(51)

2014年(8)

2013年(3)

分类: Android平台

2015-04-13 17:11:39

概述:将Animation应用于activity里的每一个控件,实现漂亮的动画效果
一、什么是LayoutAnimationController

    1、LayoutAnimationController用于为layout里面的每一个空间或者ViewGroup里面的控件设置动画效果
    2、每一个空间都有相同的动画效果
    3、这些动画效果在不同的时间显示出来
    4、LayoutAnimationController可以再xml文件中实现,也可以在代码中实现
    5、在xml文件中使用LayoutAnimationController
    (1)在res/anim文件中创造一个xml文件list_anim_layout.xml
                    xmlns:android=""
           //设置动作时间
           android:delay = "1"
           //设置每个控件出现的顺序
           android:animationOrder = "random"
           //设置动画配置文件
           android:animation = "@anim/list_anim"
           />
    (2)在布局文件中为控件添加配置
        //设置配置文件
        android:layoutAnimation="@anim/list_anim_layout"
    6、在代码中使用LayoutAnimationController
    (1)创造一个Animation对象
        通过装载xml文件创造,或者通过构造函数创造
    (2)使用下列代码创造LayoutAnimationController
        LayoutAnimationController lac = new LayoutAnimationController(animation)
    (3)设置控件的显示顺序
        lac.setOrder(LayoutAnimationController.ORDER_RANDOM)
    (4)为控件设置LayoutAnimationController属性
        listview.setLayoutAnimation(lac)
二、什么是AnimationListener
    1、AnimationListener是一个监听器
    2、这个监听器在动画执行的各个阶段都会得到通知,从而调用相应的方法
    3、主要包含下面2个方法
        onAnimationEnd(animation)       //动画效果结束时执行
        onAnimationRepeat(animation) //动画效果重复时执行
        onAnimationStart(animation) //动画效果开始时执行
三、源代码  
    MainAcvitity.java    主程序   

点击(此处)折叠或打开

  1. public class MainActivity extends ListActivity
  2. {
  3.     private Button addBtn = null;
  4.     private Button removeBtn = null;
  5.     private ListView listView = null;
  6.     private ViewGroup viewGroup = null;
  7.     protected void onCreate(Bundle savedInstanceState)
  8.     {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.         
  12.         addBtn = (Button)findViewById(R.id.addBtn);
  13.         removeBtn = (Button)findViewById(R.id.removeBtn);
  14.         listView = getListView();
  15.         viewGroup = (ViewGroup)findViewById(R.id.layoutId);
  16.         addBtn.setOnClickListener(new btnListener());
  17.         removeBtn.setOnClickListener(new btnListener());
  18.     }
  19.     
  20.     private ListAdapter listAdapter()
  21.     {
  22.         //创造一个list
  23.         List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
  24.         //生成HashMap
  25.         HashMap<String, String> map1 = new HashMap<String, String>();
  26.         HashMap<String, String> map2 = new HashMap<String, String>();
  27.         HashMap<String, String> map3 = new HashMap<String, String>();
  28.         //为每一个HashMap添加键值对
  29.         map1.put("user_name", "张三");
  30.         map1.put("user_sex", "男");
  31.         map2.put("user_name", "李四");
  32.         map2.put("user_sex", "男");
  33.         map3.put("user_name", "王五");
  34.         map3.put("user_sex", "女");
  35.         //将HashMap添加到list
  36.         list.add(map1);
  37.         list.add(map2);
  38.         list.add(map3);
  39.         //生成adapter
  40.         SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item,
  41.                 new String[]{"user_name", "user_sex"}, new int[]{R.id.user_name, R.id.user_sex});
  42.         return simpleAdapter;
  43.     }
  44.     
  45.     
  46.     class btnListener implements OnClickListener
  47.     {
  48.         @Override
  49.         public void onClick(View v)
  50.         {
  51.             switch(v.getId())
  52.             {
  53.             // TODO Auto-generated method stub
  54.                 case R.id.addBtn:
  55.                     listView.setAdapter(listAdapter());
  56.                     //创造一个Animation对象
  57.                     Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
  58.                     //使用下列代码创造LayoutAnimationController
  59.                     LayoutAnimationController lac = new LayoutAnimationController(animation);
  60.                     //设置控件的显示顺序
  61.                     lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
  62.                     //为listview设置LayoutAnimationController属性
  63.                     listView.setLayoutAnimation(lac);
  64.                     break;
  65.                 case R.id.removeBtn:
  66.                     //设置淡出效果
  67.                     AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
  68.                     //持续时间3s
  69.                     alphaAnimation.setDuration(3000);
  70.                     //为Animation设置监听器
  71.                     alphaAnimation.setAnimationListener(new animationListener());
  72.                     //启动效果
  73.                     listView.startAnimation(alphaAnimation);
  74.                     break;
  75.                 default:
  76.                     break;
  77.             }
  78.         }
  79.         
  80.     }
  81.     
  82.     class animationListener implements AnimationListener
  83.     {

  84.         @Override
  85.         public void onAnimationEnd(Animation animation)
  86.         {
  87.             // TODO Auto-generated method stub
  88.             //移除控件
  89.             viewGroup.removeView(listView);
  90.         }
  91.         @Override
  92.         public void onAnimationRepeat(Animation animation)
  93.         {
  94.             // TODO Auto-generated method stub
  95.             
  96.         }

  97.         @Override
  98.         public void onAnimationStart(Animation animation)
  99.         {
  100.             // TODO Auto-generated method stub
  101.             
  102.         }
  103.     }

  104.     @Override
  105.     public boolean onCreateOptionsMenu(Menu menu)
  106.     {
  107.         // Inflate the menu; this adds items to the action bar if it is present.
  108.         getMenuInflater().inflate(R.menu.main, menu);
  109.         return true;
  110.     }

  111. }
    list_animation_layout.xml    animation设置文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layoutAnimation
  3.     xmlns:android=""
  4.     android:delay = "1"
  5.     android:animationOrder = "random"
  6.     android:animation = "@anim/list_anim"
  7.     />
    list_animation.xml              animation动画文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3.     xmlns:android=""
  4.     android:interpolator="@android:anim/accelerate_interpolator">
  5.  
  6.     <alpha
  7.         android:fromAlpha="0.1"
  8.         android:toAlpha="1.0"
  9.         android:duration="3000"
  10.         android:startOffset="500"
  11.         />
  12. </set>
四、布局文件
    activity_main.xml                activity布局文件

点击(此处)折叠或打开

  1. <LinearLayout
  2.     xmlns:android=""
  3.     xmlns:tools=""
  4.     android:id="@+id/layoutId"
  5.     android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent"
  7.      android:orientation="vertical"
  8.     >

  9.     <ListView
  10.      android:id="@id/android:list"
  11.      android:layout_width="fill_parent"
  12.      android:layout_height="wrap_content"
  13.      android:scrollbars="vertical"
  14.      android:layoutAnimation="@anim/list_anim_layout"
  15.      />
  16.     <Button
  17.      android:id="@+id/addBtn"
  18.      android:layout_width="fill_parent"
  19.      android:layout_height="wrap_content"
  20.      android:text="addButton"
  21.      />
  22.     <Button
  23.      android:id="@+id/removeBtn"
  24.      android:layout_width="fill_parent"
  25.      android:layout_height="wrap_content"
  26.      android:text="removeButton"
  27.      />
  28. </LinearLayout>
    item.xml                            list控件布局文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="horizontal"
  6.     android:padding="10dip" >
  7.     <TextView
  8.         android:id="@+id/user_name"
  9.         android:layout_width="30dip"
  10.         android:layout_height="wrap_content"
  11.         android:singleLine="true"
  12.         />
  13.     <TextView
  14.         android:id="@+id/user_sex"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:gravity="right"
  18.         />
  19.     

  20. </LinearLayout>



阅读(2580) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~