在xml中使用LayoutAnimationController

来源:互联网 发布:软件测试过程管理 编辑:程序博客网 时间:2024/06/10 04:24

什么是LayoutAnimationController?

答:它是用于一个布局或者ViewGroup的动画控制器,它能够让每个子View在不同的时间点分别执行相同的动画。


案例:

点击按钮前

【中间有动画效果】

点击按钮后


步骤如下:

1.现在res/anim文件夹下创一个list_anim_layout.xml,该文件的作用是【为整个layout设置效果】

<?xml version="1.0" encoding="utf-8"?><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"    android:animationOrder="normal"    android:delay="0.5"     android:animation="@anim/list_anim"></layoutAnimation><!-- 专门为整个layout设置效果 -->

2.在res/anim文件夹下创建一个list_anim.xml,该文件的作用是【设置单个控件的效果】

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="2000"        android:fromAlpha="0.0"        android:toAlpha="1.0" /></set>

3.mainActivity.xml,在这个文件中一定别忘了【android:layoutAnimation="@anim/list_anim_layout"】

<LinearLayout 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:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="LayoutAnimationController " />        <ListView                 android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:scrollbars="vertical"        android:layoutAnimation="@anim/list_anim_layout"        />                    <Button         android:id="@+id/button"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="测试"/></LinearLayout>

4.item.xml

<LinearLayout 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:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/userName"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <TextView        android:id="@+id/userAge"        android:layout_width="wrap_content"        android:layout_height="wrap_content"            />                   </LinearLayout>

5.MainActivity.java

package com.fzq.layoutanimation;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.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {private Button buttonTest;private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);buttonTest=(Button)findViewById(R.id.button);listView=(ListView)findViewById(R.id.listView);buttonTest.setOnClickListener(new ButtonListener());}private ListAdapter buildListAdapter(){List<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();HashMap<String, String> m1=new HashMap<String, String>();m1.put("userName", "秋词");m1.put("userAge", "17");HashMap<String, String> m2=new HashMap<String, String>();m2.put("userName", "瀚声");m2.put("userAge", "19");HashMap<String, String> m3=new HashMap<String, String>();m3.put("userName", "履善");m3.put("userAge", "18");list.add(m1);list.add(m2);list.add(m3);SimpleAdapter simpleAdapter=new SimpleAdapter(this, list, R.layout.item, new String[] {"userName","userAge"}, new int[]{R.id.userName,R.id.userAge});return simpleAdapter;}class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stublistView.setAdapter(buildListAdapter());}}}