LayoutAnimationController的作用
1.为一个layout里面的控件,或ViewGroup里面的控件设置动画效果的类
2.每个控件设置动画一样
3.指定时间显示一帧
4.既可以在xml文件中配置,也可以在代码中实现
下面演示使用该对象实现一个2s淡入的动画效果。
具体使用步骤:
1)xml文件中配置LayoutAnimationController
res/anim/layout_anim.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/alpha" />
2)还需要个动画配置文件
res/anim/alpha.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. 布局文件中使用ListView
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> <Button android:id="@+id/btn_show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示列表" /> </LinearLayout>
4.配置列表的样式文件:
layout/other.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/name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="5pt" android:singleLine="true" /> <TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="5pt" android:singleLine="true"/> </LinearLayout>
5.activity中的代码:
package com.example.layoutanimationctrl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.ListActivity; import android.os.Bundle; 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; public class MainActivity extends ListActivity { private Button btn_show = null; private ListView list_view = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_show = (Button)findViewById(R.id.btn_show); list_view = getListView(); btn_show.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { System.out.println("you click button!"); list_view.setAdapter(buildAdapter()); // 这段代码演示没有layout_anim_ctl.xml文件中,在代码中使用LayoutAnimationControl播放动画效果 /* Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); LayoutAnimationController lac = new LayoutAnimationController(animation); list_view.setLayoutAnimation(lac);*/ } }); } // 用于填充listView的数据 private ListAdapter buildAdapter(){ List<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>(); HashMap<String,String> stu1 = new HashMap<String, String>(); stu1.put("name", "张三"); stu1.put("age", "23"); HashMap<String,String> stu2 = new HashMap<String, String>(); stu2.put("name", "张4"); stu2.put("age", "24"); list.add(stu1); list.add(stu2); SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.other, new String[]{"name","age"}, new int[]{R.id.name, R.id.age}); return adapter; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }