本例使用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可以较慢的速度显示动画效果。


更多相关文章

  1. Android(安卓)在OnCreate获取需要控件的高度,宽度,textview的行数
  2. Android(安卓)自定义侧边栏列表
  3. 上中下布局,上下高度指定,中间自适应(左中右同理)
  4. android PinYinIME横屏模式禁止全屏输入 & 调整横屏模式下键盘高
  5. 关于ListView嵌套的问题
  6. Android(安卓)RecyclerView实现图片自适应高度的瀑布流
  7. android 获取字符串在屏幕中所占像素宽度和高度
  8. Android(安卓)获得软键盘状态
  9. Flutter 新闻客户端 - 09 详情页展示、分享、远程真机调试

随机推荐

  1. Android 打包
  2. android 开发 经典网站
  3. android中activity全屏的方法
  4. 【读书笔记《Android游戏编程之从零开始
  5. android TabHost导航切换实现方式
  6. android:maxHeight,android:maxWidth失效
  7. android中view的一些属性
  8. Android ListView 去除边缘阴影、选中色
  9. Android解析XML三种方式(PULL、SAX、DOM)
  10. Android 系统UI设计规则