ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

一、简介

二、代码
1.xml
(1)activity_main.xml

 1 <ListView
 2         android:id="@id/android:list"
 3         android:layout_width="wrap_content"
 4         android:layout_height="wrap_content"
 5         android:scrollbars="vertical" 
 6         />
 7         <!-- android:layoutAnimation="@anim/list_anim_layout" -->
 8 
 9        <Button
10         android:id="@+id/buttonId"
11         android:layout_width="fill_parent"
12         android:layout_height="wrap_content"
13         android:text="测试"
14         />

(2)item.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent" android:layout_height="fill_parent"
 4     android:orientation="horizontal" android:paddingLeft="10dip"
 5     android:paddingRight="10dip" android:paddingTop="1dip"
 6     android:paddingBottom="1dip">
 7     
 8     <TextView android:id="@+id/user_name" android:layout_width="180dip"
 9         android:layout_height="30dip" android:textSize="5pt"
10         android:singleLine="true"/>
11     <TextView android:id="@+id/usr_gender" android:layout_width="fill_parent"
12         android:layout_height="fill_parent" android:textSize="5pt" 
13         android:singleLine="true"/>
14 </LinearLayout>

(3)resanimlist_anim.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3     android:interpolator="@android:anim/accelerate_interpolator"
4     android:shareInterpolator="true">
5     
6     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
7 </set>

(3)resanimlist_anim_layout.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
3     android:delay="2"
4     android:animationOrder="normal" 
5     android:animation="@anim/list_anim" />

2.java
(1)MainActivity.java

 1 package com.layoutanimationcontroller;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 
 7 import android.app.ListActivity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.view.animation.Animation;
12 import android.view.animation.AnimationUtils;
13 import android.view.animation.LayoutAnimationController;
14 import android.widget.Button;
15 import android.widget.ListAdapter;
16 import android.widget.ListView;
17 import android.widget.SimpleAdapter;
18 
19 public class MainActivity extends ListActivity {
20     
21     private Button button = null;
22     private ListView listView = null;
23     
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.activity_main);
28         listView = getListView();
29         button = (Button)findViewById(R.id.buttonId);
30         button.setOnClickListener(new ButtonListener());
31     }
32     
33     class ButtonListener implements OnClickListener {
34         @Override
35         public void onClick(View v) {
36             listView.setAdapter(buidListAdapter());
37             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
38             LayoutAnimationController lac = new LayoutAnimationController(animation);
39             lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
40             lac.setDelay(0.5f);
41             listView.setLayoutAnimation(lac);
42         }
43     }
44 
45     private ListAdapter buidListAdapter() {
46         List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
47         HashMap<String, String> m1 = new HashMap<String,String>();
48         m1.put("user_name", "张三");
49         m1.put("user_gender", "女");
50 
51         HashMap<String, String> m2 = new HashMap<String, String>();
52         m2.put("user_name", "李四");
53         m2.put("user_gender", "女");
54 
55         HashMap<String, String> m3 = new HashMap<String, String>();
56         m3.put("user_name", "王五");
57         m3.put("user_gender", "男");
58         
59         list.add(m1);
60         list.add(m2);
61         list.add(m3);
62         
63         SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item, 
64                 new String[] {"user_name", "user_gender"}, 
65                 new int[] {R.id.user_name, R.id.usr_gender});
66         return simpleAdapter;
67     }
68 }

 

 

原文地址:https://www.cnblogs.com/shamgod/p/5201802.html