当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(127):Views->Layout Animation->2. List Cascade

本例使用TranslateAnimation 给一个ListView的列表项向下逐步展开(Cascade)的动画效果,各种基本Animation可以参见Android ApiDemos示例解析(3): App->Activity->Animation。

本例使用代码为ListView添加LayoutAnimation.

[java] 
AnimationSet set = new AnimationSet(true); 
 
Animation animation = new AlphaAnimation(0.0f, 1.0f); 
animation.setDuration(50); 
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(2000); 
set.addAnimation(animation); 
 
LayoutAnimationController controller 
 = new LayoutAnimationController(set, 0.5f); 
ListView listView = getListView(); 
listView.setLayoutAnimation(controller); 

AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
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(2000);
set.addAnimation(animation);

LayoutAnimationController controller
 = new LayoutAnimationController(set, 0.5f);
ListView listView = getListView();
listView.setLayoutAnimation(controller);
本例Cascade效果是通过TranslateAnimation实现的,本例使用的构造函数:

public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

fromXType: 指出fromXValue的模式,本例使用RELATIVE_TO_SELF,表示使用本View的宽度或是高度。RELATIVE_TO_PARENT为父View的宽度或是高度。ABSOLUTE 表示fromXValue使用绝对值。
fromXValue: 起始X坐标或当fromXType为RELATIVE_TO_PARENT或是RELATIVE_TO_SELF时表示相对于本View或父View的系数。本例向下展开,在X方向无动画,因此设为0.
toXType: 指出toXValue的模式,本例使用RELATIVE_TO_SELF,表示使用本View的宽度或是高度。RELATIVE_TO_PARENT为父View的宽度或是高度。ABSOLUTE 表示fromXValue使用绝对值。
toXValue: 结束X坐标或当fromXType为RELATIVE_TO_PARENT或是RELATIVE_TO_SELF时表示相对于本View或父View的系数。本例向下展开,在X方向无动画,因此设为0.
fromYType: 指出fromYValue的模式,本例使用RELATIVE_TO_SELF,表示使用本View的宽度或是高度。RELATIVE_TO_PARENT为父View的宽度或是高度。ABSOLUTE 表示fromYValue使用绝对值。
fromYValue: 起始Y坐标或当fromYType为RELATIVE_TO_PARENT或是RELATIVE_TO_SELF时表示相对于本View或父View的系数。
toYType: 指出toYValue的模式,本例使用RELATIVE_TO_SELF,表示使用本View的宽度或是高度。RELATIVE_TO_PARENT为父View的宽度或是高度。ABSOLUTE 表示fromXValue使用绝对值。
toYValue: 结束Y坐标或当fromYType为RELATIVE_TO_PARENT或是RELATIVE_TO_SELF时表示相对于本View或父View的系数。本例向下展开,值为1表示向下移动子View的高度。
将动画时间从100改成2000可以较慢的速度显示动画效果。

 \
 


 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,