[b]如何使用layoutAnimation[/b]
首先: 需要创建一个layoutAnimation的配置文件, 定义动画的步骤和引用动画
然后: 在需要动画效果的控件中加入[color=red]android:layoutAnimation="@anim/list_anim_layout"[/color].
[b]一个完成的例子:[/b]
[b]首先/res/anim/alpha.xml[/b]
[b]新建一个/res/anim/list_anim_layout.xml[/b]
[b]这个/res/layout/activity.xml是ListView用的[/b]
[b]/res/layout/main.xml主文件[/b]
[b]Animation3Activity.java文件[/b]
首先: 需要创建一个layoutAnimation的配置文件, 定义动画的步骤和引用动画
然后: 在需要动画效果的控件中加入[color=red]android:layoutAnimation="@anim/list_anim_layout"[/color].
[b]一个完成的例子:[/b]
[b]首先/res/anim/alpha.xml[/b]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000"/>
</set>
[b]新建一个/res/anim/list_anim_layout.xml[/b]
<?xml version="1.0" encoding="utf-8"?><!-- animationOrder: random, normal -->
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="2" android:animationOrder="normal" android:animation="@anim/alpha"></layoutAnimation>
[b]这个/res/layout/activity.xml是ListView用的[/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50px">
<TextView android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical"/>
<TextView android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/>
</LinearLayout>
[b]/res/layout/main.xml主文件[/b]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layoutAnimation="@anim/list_anim_layout"></ListView>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="setAlphaClick"></Button>
</LinearLayout>
[b]Animation3Activity.java文件[/b]
package com.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Animation3Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void createListView(){
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
map = new HashMap<String,String>();
map.put("姓名", "小王");
map.put("年龄", "12");
list.add(map);
ListView view = (ListView)findViewById(R.id.listView1);
view.setAdapter(new SimpleAdapter(this, list, R.layout.activity, new String[]{"姓名","年龄"}, new int[]{R.id.view1,R.id.view2}));
// setContentView(view);
}
public void setAlphaClick(View v){
createListView();
}
}