概述:将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 主程式
點選(此處)折疊或打開
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);
default:
}
}
class animationListener implements AnimationListener
public void onAnimationEnd(Animation animation)
//移除控件
viewGroup.removeView(listView);
public void onAnimationRepeat(Animation animation)
public void onAnimationStart(Animation animation)
@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;
}
list_animation_layout.xml animation設定檔案
?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"
/>
list_animation.xml animation動畫檔案
set
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: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:text="addButton"
android:id="@+id/removeBtn"
android:text="removeButton"
/LinearLayout>
item.xml list控件布局檔案
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"
android:id="@+id/user_sex"
android:layout_width="wrap_content"
android:gravity="right"