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>