美文网首页
android LayoutAnimation的基本用法和说明

android LayoutAnimation的基本用法和说明

作者: 雨田Android开发 | 来源:发表于2021-04-01 09:41 被阅读0次

LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子元素 出场时都会具有这种动画效果。这种效果常常被用在ListView上,我们时常会看到一种特 殊的ListView,它的每个item都以一定的动画的形式出现,其实这并非什么高深的技术, 它使用的就是LayoutAnimation。LayoutAnimation也是一个View动画,为了给ViewGroup 的子元素加上出场效果,遵循如下几个步骤。
(1)定义LayoutAnimation,如下所示。

// res/anim/anim_layout.xml 
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
android:delay="0.5" 
android:animationOrder="normal" 
android:animation="@anim/anim_item"/>

它的属性的含义如下所示。
android:delay 表示子元素开始动画的时间延迟,比如子元素入场动画的时间周期为300ms,那么0.5 表示每个子元素都需要延迟150ms才能播放入场动画。总体来说,第一个子元素延迟 150ms开始播放入场动画,第2个子元素延迟300ms开始播放入场动画,依次类推。
android:animationOrder 表示子元素动画的顺序,有三种选项:normal、reverse和random,其中normal表示顺
序显示,即排在前面的子元素先开始播放入场动画;reverse表示逆向显示,即排在后面的 子元素先开始播放入场动画;random则是随机播放入场动画。
android:animation 为子元素指定具体的入场动画。
(2)为子元素指定具体的入场动画,如下所示。

// res/anim/anim_item.xml 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
  android:duration="300" 
  android:interpolator="@android:anim/accelerate_interpolator" 
  android:shareInterpolator="true" >
 <alphaandroid:fromAlpha="0.0" android:toAlpha="1.0" />
 <translate android:fromXDelta="500"
   android:toXDelta="0" />
 </set>

(3)为ViewGroup指定android:layoutAnimation属性:android:layoutAnimation= "@anim/ anim_layout"。对于ListView来说,这样ListView的item就具有出场动画了,这种 方式适用于所有的ViewGroup,如下所示。

<ListView android:id="@+id/list" 
  android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layoutAnimation="@anim/anim_layout"
 android:background="#fff4f7f9"
 android:cacheColorHint="#00000000"
 android:divider="#dddbdb"
 android:dividerHeight="1.0px"
android:listSelector="@android:color/transparent" />

除了在XML中指定LayoutAnimation外,还可以通过LayoutAnimationController来实 现,具体代码如下所示。

ListView listView = (ListView) layout.findViewById(R.id.list); 
Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_ item);
 LayoutAnimationController controller = new LayoutAnimationController (animation); controller.setDelay(0.5f);
 controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
 listView.setLayoutAnimation(controller);

相关文章

网友评论

      本文标题:android LayoutAnimation的基本用法和说明

      本文链接:https://www.haomeiwen.com/subject/fbozhltx.html