在上一篇View动画里,我们知道了View动画以及帧动画的简单使用,而这一篇主要是讲View动画的特殊使用场景,比如:
- 在ViewGroup中可以控制子元素的出场效果
- 在Activity中可以实现不同Activity之间的切换效果,
关于Activity切换这点,这篇帖子就不细说了无非就是overridePendingTransition的使用,本文主要要说的是LayoutAnimation
LayoutAnimatioon
LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样他的子元素出场时都会具有这种动画
LayoutAnimatioon中的属性:
- android:delay="0.5"
表示子元素开始动画的延迟时间,比如子元素入场动画的时间周期为300ms,那么0.5表示每个子元素都需要延迟150ms才能播放入场动画,也就是说:第一个子元素延迟150ms,第二个子元素延迟300ms。 - android:animationOrder="normal"
表示元素动画的顺序,有三种选项分别是:
A:nromal--表示顺序显示
B:reverse--表示逆向显示
C:random--表示随机显示 - android:animation="@anim/anim_item"
为子元素指定具体的入场动画
LayoutAnimatioon的使用遵循以下几个步骤:
- 定义LayoutAnimation
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/anim_item"
android:animationOrder="normal"
android:delay="0.5">
</layoutAnimation>
- 为子元素指定具体的入场动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="600"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0"
android:toAlpha="1.0" />
<translate
android:fromXDelta="500"
android:toXDelta="0" />
</set>
- 为ViewGroup指定android:layoutAnimation属性:
在xml布局文件中指定android:layoutAnimation属性:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff4f7f9"
android:cacheColorHint="#00000000"
android:divider="#dddbdb"
android:dividerHeight="1.0px"
android:layoutAnimation="@anim/layout_animation"
android:listSelector="@android:color/transparent"
/>
或者,可以在java代码中通过LayoutAnimationController来指定:
ListView listview = (ListView) findViewById(R.id.listview);
Animation animation = AnimationUtils.loadAnimation(TestAnimActivity.this, R.anim.anim_item);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listview.setLayoutAnimation(controller);
- 然后正常使用ViewGroup(比如ListView)即可
怎么样,如此简单就能做出一个炫酷的ListView特效,so easy!
================================================
更多内容请关注 我的专题
转载请注明 原文链接:
http://www.jianshu.com/users/c1b4a5542220/latest_articles
网友评论