如果要求自己的Activity整齐划一,可以考虑使用布局动画LayoutAnimation,话不多说,先演示在代码中使用,然后是xml。
1.in code
他山之石,可以攻玉,摘抄网友的代码:
/**
* Layout动画
*
* @return
*/
protected LayoutAnimationController getAnimationController() {
int duration=300;
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(duration);
set.addAnimation(animation);
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(duration);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); //注意这个地方是以秒为单位,是浮点型数据,所以要加f
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
return controller;
}
接下来在代码中调用该方法即可:
listView = (ListView) findViewById(R.id.listView);
listView.setLayoutAnimation(getAnimationController());
adapter = new ListViewAdapter(stores);
listView.setAdapter(adapter);
2.in xml
首先在res/anim目录下新建一个动画xml文件push_right_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
接下来在res/animator目录下新建一个布局动画文件layout-anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"//注意此处单位为s
android:animationOrder="random"
android:animation="@anim/push_right_in"
/>
然后在需要动画的Layout下加入下面一条语句即可:
android:layoutAnimation="@animator/layout-anim"