Android(安卓)ApiDemos示例解析(127):Views->Layout Animation->2. List Cascade
16lz
2021-01-26
本例使用TranslateAnimation 给一个ListView的列表项向下逐步展开(Cascade)的动画效果,各种基本Animation可以参见Android ApiDemos示例解析(3): App->Activity->Animation。
本例使用代码为ListView添加LayoutAnimation.
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(安卓)在OnCreate获取需要控件的高度,宽度,textview的行数
- Android(安卓)自定义侧边栏列表
- 上中下布局,上下高度指定,中间自适应(左中右同理)
- android PinYinIME横屏模式禁止全屏输入 & 调整横屏模式下键盘高
- 关于ListView嵌套的问题
- Android(安卓)RecyclerView实现图片自适应高度的瀑布流
- android 获取字符串在屏幕中所占像素宽度和高度
- Android(安卓)获得软键盘状态
- Flutter 新闻客户端 - 09 详情页展示、分享、远程真机调试