標籤:animation android控制項開發 layout動畫效果 動畫
android開發之Animations的使用(四)
本博文主要講述的是,animation在layout中的使用。本文是用ListView控制項為例子
實現在layout中的使用有兩種方法,
第一是直接使用xml檔案中的layoutAnimation標籤
第二是使用代碼實現,使用layoutAnimationController對象完成,
詳細代碼如下:
MainActivity.java:
package com.example.animationtest4;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/*
*本類主要實現的是Animation的動畫效果在layout控制項中的使用
*本例將anim_list動畫效果應用於listview 控制項中
*/
public class MainActivity extends Activity {
private Button testButton = null;
private ListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testButton = (Button)findViewById(R.id.myButton);
listView = (ListView)findViewById(R.id.myList);
testButton.setOnClickListener(new buttonListener());
}
class buttonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//使用布局檔案完成對Layout控制項的動畫效果的設定
//查看layout_anim_list.xml檔案
//listView.setAdapter(buildListAdapter());
//使用xml檔案實現,需要在相應的layout控制項中添加如下語句:
android:layoutAnimation="@anim/layout_anim_list"
//使用代碼實現layout控制項的動畫效果設定
listView.setAdapter(buildListAdapter());
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_list);
//建立LayoutAnimationController對象,且設定延時為0.5s
LayoutAnimationController lac = new LayoutAnimationController(animation, 0.5f);
//設定顯示的順序是normal
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);
}
}
// 建立一個listAdapter對象
private ListAdapter buildListAdapter(){
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
map1.put("user", "張三");
map1.put("sex", "男");
map2.put("user", "李四");
map2.put("sex", "女");
map3.put("user", "王五");
map3.put("sex", "男");
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.item, new String[]{"user","sex"}, new int[]{R.id.user,R.id.sex});
return adapter;
}
@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;
}
}
動畫效果布局檔案anim_list.xml:
<?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:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
layout控制項實現動畫效果的布局檔案layout_anim_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/anim_list">
</layoutAnimation>
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/myList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myText"
//android:layoutAnimation="@anim/layout_anim_list"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/myList"
android:text="測試" />
</RelativeLayout>
listview條目的布局檔案item.xml:
<?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" >
<TextView
android:id="@+id/user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
android開發之Animations的使用(四)