概述:将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="http://schemas.android.com/apk/res/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 主程序
list_animation_layout.xml animation设置文件
list_animation.xml animation动画文件
四、布局文件
activity_main.xml activity布局文件
item.xml list控件布局文件
一、什么是LayoutAnimationController
1、LayoutAnimationController用于为layout里面的每一个空间或者ViewGroup里面的控件设置动画效果
2、每一个空间都有相同的动画效果
3、这些动画效果在不同的时间显示出来
4、LayoutAnimationController可以再xml文件中实现,也可以在代码中实现
5、在xml文件中使用LayoutAnimationController
(1)在res/anim文件中创造一个xml文件list_anim_layout.xml
xmlns:android="http://schemas.android.com/apk/res/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 主程序
点击(此处)折叠或打开
- public class MainActivity extends ListActivity
- {
- private Button addBtn = null;
- private Button removeBtn = null;
- private ListView listView = null;
- private ViewGroup viewGroup = null;
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- addBtn = (Button)findViewById(R.id.addBtn);
- removeBtn = (Button)findViewById(R.id.removeBtn);
- listView = getListView();
- viewGroup = (ViewGroup)findViewById(R.id.layoutId);
- addBtn.setOnClickListener(new btnListener());
- removeBtn.setOnClickListener(new btnListener());
- }
-
- private ListAdapter listAdapter()
- {
- //创造一个list
- ListHashMapString, String>> list = new ArrayListHashMapString, String>>();
- //生成HashMap
- HashMapString, String> map1 = new HashMapString, String>();
- HashMapString, String> map2 = new HashMapString, String>();
- HashMapString, String> map3 = new HashMapString, String>();
- //为每一个HashMap添加键值对
- map1.put("user_name", "张三");
- map1.put("user_sex", "男");
- map2.put("user_name", "李四");
- map2.put("user_sex", "男");
- map3.put("user_name", "王五");
- map3.put("user_sex", "女");
- //将HashMap添加到list
- list.add(map1);
- list.add(map2);
- list.add(map3);
- //生成adapter
- SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item,
- new String[]{"user_name", "user_sex"}, new int[]{R.id.user_name, R.id.user_sex});
- return simpleAdapter;
- }
-
-
- class btnListener implements OnClickListener
- {
- @Override
- public void onClick(View v)
- {
- switch(v.getId())
- {
- // TODO Auto-generated method stub
- case R.id.addBtn:
- listView.setAdapter(listAdapter());
- //创造一个Animation对象
- Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
- //使用下列代码创造LayoutAnimationController
- LayoutAnimationController lac = new LayoutAnimationController(animation);
- //设置控件的显示顺序
- lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
- //为listview设置LayoutAnimationController属性
- listView.setLayoutAnimation(lac);
- break;
- case R.id.removeBtn:
- //设置淡出效果
- AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
- //持续时间3s
- alphaAnimation.setDuration(3000);
- //为Animation设置监听器
- alphaAnimation.setAnimationListener(new animationListener());
- //启动效果
- listView.startAnimation(alphaAnimation);
- break;
- default:
- break;
- }
- }
-
- }
-
- class animationListener implements AnimationListener
- {
-
- @Override
- public void onAnimationEnd(Animation animation)
- {
- // TODO Auto-generated method stub
- //移除控件
- viewGroup.removeView(listView);
- }
- @Override
- public void onAnimationRepeat(Animation animation)
- {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void onAnimationStart(Animation animation)
- {
- // TODO Auto-generated method stub
-
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }
点击(此处)折叠或打开
- ?xml version="1.0" encoding="utf-8"?>
- layoutAnimation
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:delay = "1"
- android:animationOrder = "random"
- android:animation = "@anim/list_anim"
- />
点击(此处)折叠或打开
- ?xml version="1.0" encoding="utf-8"?>
- set
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- alpha
- android:fromAlpha="0.1"
- android:toAlpha="1.0"
- android:duration="3000"
- android:startOffset="500"
- />
- /set>
activity_main.xml activity布局文件
点击(此处)折叠或打开
- LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/layoutId"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
-
- ListView
- android:id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:scrollbars="vertical"
- android:layoutAnimation="@anim/list_anim_layout"
- />
- Button
- android:id="@+id/addBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="addButton"
- />
- Button
- android:id="@+id/removeBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="removeButton"
- />
- /LinearLayout>
点击(此处)折叠或打开
- ?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="match_parent"
- android:orientation="horizontal"
- android:padding="10dip" >
- TextView
- android:id="@+id/user_name"
- android:layout_width="30dip"
- android:layout_height="wrap_content"
- android:singleLine="true"
- />
- TextView
- android:id="@+id/user_sex"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="right"
- />
-
-
- /LinearLayout>