工作点滴,汇聚成雨
(LayoutAnimation的简单使用)
简单的说,LayoutAnimation就是帮父类管理子类集体动画的这么一个东东,例如listview里面有多个item,就可以按顺序的孩子设置统一的有顺序的动画效果(无序也可以),废话少说,操作最重要:
设置方法之一:xml
1.
res/layout/test.xml(为控件添加动画属性)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/list_anim_layout"
android:numColumns="5" >
</GridView>
</LinearLayout>
重要的只有一句话: android:layoutAnimation="@anim/list_anim_layout",即开启动画,并指明那个动画,好了,来创建list_anim_layout.xml吧
2.
res/anim/list_anim_layout.xml(为集体动画做常规设置)
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
<!-- 下一个item出现的时间,可以是百分百,可以是1 -->
android:delay="0.1"
<!-- item出现的顺序,normal是有序的,random是无序,还有其他-->
android:animationOrder="normal"
<!-- 这句又是重点,指定动画的效果-->
android:animation="@anim/push_right_in"/>
重要的还是只要一句话:android:animation="@anim/push_right_in,为每个item指定动画的效果,好了,又来个push_right_in.xml
3.
res/anim/push_right_in.xml(具体动画效果)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>
kekeke,搞定
设置方法之二:代码动态设置
1.为控件添加动画属性
gv= (GridView) findViewById(R.id.gv);
gv.setLayoutAnimation(getAnimationController()); //这句是重点
adapter = new ListViewAdapter(xxx);
gv.setAdapter(adapter);
2.为集体动画做常规设置(LayoutAnimation是开启集体动画功能,LayoutAnimationController 是控制集体动画,
protected LayoutAnimationController getController() {
// getAnimation()返回具体动画集,0.1f是下一个item出现的时机
LayoutAnimationController controller = new LayoutAnimationController(getAnimation(), 0.1f);
//ORDER_NORMAL是动画执行的顺序
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
return controller;
}
3.具体动画效果(详细动画介绍http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml)
public AnimationSet getAnimation(){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);
return set;
}
kekeke,又搞定