【Android学习】Android动画效果--Animations(3)
LayoutAnimationController:
1、LayoutAnimationController用于为一个Layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果
2、每个控件都有相同的动画效果
3、这些控件的动画效果在不同的时间显示出来
4、LayoutAnimationController可以在XML文件中设置,也可以在代码中设置
------>在XML当中使用LayoutAnimationController
1、在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、在布局文件中为listview添加以下配置:
android:layoutAnimation="@anim/list_anim_layout"
----->在代码中使用LayoutAnimationController
1、创建一个Animation对象
2、使用如下代码创建LayoutAnimationController对象:
LayoutAnimationController lac = new LayoutAnimationController(animation);
3、设置空间显示的顺序
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
4、为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(lac);
具体实现如下:
MainActivity.java:
public class MainActivity extends ListActivity { private Button button = null; private ListView listView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = getListView(); button = (Button)findViewById(R.id.buttonId); button.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("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.user_gender }); return simpleAdapter; } private class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { listView.setAdapter(buildListAdapter()); Animation 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); } } }
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/buttonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试" /> </LinearLayout>
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>
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/user_gender" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="5pt" android:singleLine="true"/> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。