ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

时间:2022-11-19 14:56:40

一、简介

ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

ANDROID_MARS学习笔记_S02_011_ANIMATION_LayoutAnimationController

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

 <ListView
android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
<!-- android:layoutAnimation="@anim/list_anim_layout" --> <Button
android:id="@+id/buttonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试"
/>

(2)item.xml

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

(3)res\anim\list_anim.xml

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

(3)res\anim\list_anim_layout.xml

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

2.java
(1)MainActivity.java

 package com.layoutanimationcontroller;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import android.app.ListActivity;
import android.os.Bundle;
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; public class MainActivity extends ListActivity { private Button button = null;
private ListView listView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = getListView();
button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new ButtonListener());
} class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
listView.setAdapter(buidListAdapter());
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
LayoutAnimationController lac = new LayoutAnimationController(animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
lac.setDelay(0.5f);
listView.setLayoutAnimation(lac);
}
} private ListAdapter buidListAdapter() {
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> m1 = new HashMap<String,String>();
m1.put("user_name", "张三");
m1.put("user_gender", "女"); HashMap<String, String> m2 = new HashMap<String, String>();
m2.put("user_name", "李四");
m2.put("user_gender", "女"); HashMap<String, String> m3 = new HashMap<String, String>();
m3.put("user_name", "王五");
m3.put("user_gender", "男"); list.add(m1);
list.add(m2);
list.add(m3); SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item,
new String[] {"user_name", "user_gender"},
new int[] {R.id.user_name, R.id.usr_gender});
return simpleAdapter;
}
}