欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

LayoutAnimation给ListView中的item设置动态出场效果(实例)

时间:2021-05-23 08:06:26|栏目:Android代码|点击:

LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,当它的子元素出场时都按照这个动画出场。

LayoutAnimation作用于viewgroup有两种方式:

1. 静态的使用xml文件实现。

2. 在代码中动态实现。

下面用ListView中的item设置动态出场效果来分别介绍两种方式:

静态的使用xml文件实现,分为三步

1. 在res的anim目录(res的文件夹下没有anim文件夹自己新建一个)下定义LayoutAnimation命名为anim_layout如下:

version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
  android:delay="0.5"
  android:animation="@anim/anim_item"
  android:animationOrder="normal"
  >

其中的delay=“0.5”是指后一个item出场时间比前一个item的出场时间多0.5倍。

animationOrder指的是item的出场顺序是正常。

anim_item是指item出场的动画效果。

2. 在res的anim目录下定义LayoutAnimation命名为anim_item如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="200"
  >
<alpha
  android:fromAlpha="0.1"
  android:toAlpha="1"
  />
  <translate
    android:fromXDelta="500"
    android:toXDelta="0"/>
</set>

1.在listview的布局中加入layoutAnimation。

<ListView
    android:id="@+id/mylistView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutAnimation="@anim/anim_layout"
    >
</ListView>

在代码中动态的实现,分为以下几步:

Animation animation= AnimationUtils.loadAnimation(this,R.anim.anim_item);
LayoutAnimationController controller=new LayoutAnimationController(animation);
controller.setDelay(0.5f);
listView.setLayoutAnimation(controller);

上一篇:Android 蓝牙连接 ESC/POS 热敏打印机打印实例(蓝牙连接篇)

栏    目:Android代码

下一篇:Android应用App更新实例详解

本文标题:LayoutAnimation给ListView中的item设置动态出场效果(实例)

本文地址:http://www.codeinn.net/misctech/127140.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有