android 布局动画 android动画-布局动画(3)
这一篇我们来学点新的东西。做项目的时候应该碰到这种问题:根据不同条件显示或者隐藏一个控件或者布局,我们能想到的第一个方法就是 调用View.setVisibility()方法。虽然实现了显示隐藏效果,但是总感觉这样的显示隐藏过程很僵硬,让人不是很舒服,那么有没有办法能让这种显示隐藏有个过渡的动画效果呢?答案是肯定的,不言而喻就是LayoutTransition类了。
上效果图
实现上面的效果只需要在布局容器中添加Android:animateLayoutChanges=”true”属性就ok了。不信你看
布局文件如下:
view sourceprint? 01.http://schemas.android.com/apk/res/android" 03.android:layout_width="match_parent" 04.android:layout_height="match_parent" 05.android:orientation="vertical"> 06. 07.
08.android:layout_width="match_parent" 09.android:layout_height="wrap_content" 10.android:orientation="horizontal"> 11. 12.24. 25. 26.
27.android:id="@+id/parent" 28.android:layout_width="match_parent" 29.android:layout_height="wrap_content" 30.android:animateLayoutChanges="true" 31.android:orientation="vertical"> 32. 33.如果布局容器中为添加android:animateLayoutChanges=”true”默认是么有动画效果的。该属性只能用在ViewGroup控件里,表示容器里面布局改变时有默认的动画效果,比如说添加控件,删除控件的时候就是默认的动画效果。
以上动画调用代码如下:
view sourceprint? 01.http://schemas.android.com/apk/res/android" 03.android:layout_width="match_parent" 04.android:layout_height="match_parent" 05.android:orientation="vertical"> 06. 07. 08.android:layout_width="match_parent" 09.android:layout_height="wrap_content" 10.android:orientation="horizontal"> 11. 12. 13.android:layout_width="wrap_content" 14.android:layout_height="wrap_content" 15.android:onClick="buttonClick" 16.android:text="添加控件"/> 17. 18. 19.android:layout_width="wrap_content" 20.android:layout_height="wrap_content" 21.android:onClick="buttonClick1" 22.android:text="移除控件"/> 23. 24. 25. 26. 27.android:id="@+id/parent" 28.android:layout_width="match_parent" 29.android:layout_height="wrap_content" 30.android:animateLayoutChanges="true" 31.android:orientation="vertical"> 32. 33.最后来一张效果图:
布局动画之layoutAnimation很多时候我们想要在第一次加载ListView或者GridView的时候能有个动画效果来达到一个很好的过度效果。比如下面的效果
实现这种效果只需要在布局中添加android:layoutAnimation=”@anim/layout”属性就好了。接下来我们来看看layout.xml动画怎么实现的?在res/anim目录下新建layout.xml文件,代码如下:
view sourceprint? 1.http://schemas.android.com/apk/res/android" 3.android:animation="@anim/left" 4.android:animationOrder="normal" 5.android:delay="30%">android:delay 子类动画时间间隔 (延迟) 70% 也可以是一个浮点数 如“1.2”等
android:animationOrder=”random” 子类的显示方式 random表示随机
android:animationOrder 的取值有
normal 0 默认
reverse 1 倒序
random 2 随机
android:animation=”@anim/left” 表示孩子显示时的具体动画是什么代
在res/anim目录下新建left.xml文件,码如下:
http://schemas.android.com/apk/res/android"> 03. 04.android:duration="500" 05.android:fromXDelta="100%" 06.android:fromYDelta="0" 07.android:toXDelta="0" 08.android:toYDelta="0"/> 09. 10.android:duration="500" 11.android:fromAlpha="0" 12.android:toAlpha="1"/> 13.这样就实现了如上ListView的动画效果了。
当然我们也可以在代码中实现这种动画效果
就爱阅读www.92to.com网友整理上传,为您提供最全的知识大全,期待您的分享,转载请注明出处。
来源: