在ListView组件中配置动画效果,首先xml配置和上一节课GridView中一模一样,同样需要
anim/anim_set.xml anim/layout_animation.xml
这里不再做过多阐述,读者可翻阅上一篇博客内容
ListView相信大家已经很熟悉了,定义info.xml模板
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
定义主布局函数xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".List" >
<ListView
android:id="@+id/mylistView"
android:layoutAnimation="@anim/layout_animation"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这里注意到我们的layoutAnimation已经添加动画效果的那行代码了
然后主要就是Activity函数
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListAnimation extends Activity {
private String data[]=new String[]{"杨某某","张依依",
"王某某","李某某","陈某某","周某某","周某某"};
private String[] age=new String[]{"13","12",
"45","56","34","37","2"};
ListView listview;
SimpleAdapter simpleadapter=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listview=(ListView)super.findViewById(R.id.mylistView);
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map = null;
for(int i=0;i<data.length;i++){
map=new HashMap<String,Object>();
map.put("name", data[i]);
map.put("age", age[i]);
list.add(map);
}
this.simpleadapter=new SimpleAdapter(this,list,R.layout.info,
new String[]{"name","age"},
new int[]{R.id.name,R.id.age});
this.listview.setAdapter(simpleadapter);
// //读取动画配置文件
// Animation anim=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_set);
// LayoutAnimationController control=new LayoutAnimationController(anim);
// control.setDelay(0.5f);
// control.setOrder(LayoutAnimationController.ORDER_NORMAL);
// this.listview.setLayoutAnimation(control);
}
}
与ListView用法一模一样,注释掉的代码暂且不管,实现效果,就是每一行字逐个平移,有点弹幕特效
实现order 有 normal,random,reverse,逐个,随机,逆序
读者可以自行设置
实现效果如下:
同样,listview动画效果不知可以使用配置文件,也可以使用代码完成,所使用的代码就是之前注释掉的那里,这时候主配置文件xml需要取消掉layoutAnimation这一行配置,实现效果是一样的。配置文件动画是通过已经封装好的LayoutAnimationController动画,而我们用代码写的时候要了解这个类的配置,同之前AnimationSet设置差不多。
代码主要这几行
Animation anim=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_set);
LayoutAnimationController control=new LayoutAnimationController(anim);
control.setDelay(0.5f);
control.setOrder(LayoutAnimationController.ORDER_NORMAL);
this.listview.setLayoutAnimation(control);