0%

Android 动画深入分析(二):View 动画的特殊使用场景

1. View 动画的特殊使用场景

  • ViewGroup 中控制子元素的出场效果
  • Activity 中实现不同 Activity 之间的切换效果

2. LayoutAnimation 概述

  • LayoutAnimation 也是一个 View 动画,在 XML 中对应 <layoutAnimation> 标签,在代码中对应 LayoutAnimationController

  • LayoutAnimation 作用于 ViewGroup,为 ViewGroup 指定一个动画,这样它的子元素出场时都会具有这种出场动画效果

  • LayoutAnimation 示例及语法

    1. 定义 LayoutAnimation:

      1
      2
      3
      4
      5
      6
      // 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 才能播放入场动画
      android:animationOrder 表示子元素动画的顺序,有三个选项:normalreverserandom。normal 表示顺序显示,即排在前面的子元素先开始播放入场动画;reverse 表示逆序显示,即排在后面的子元素先开始播放入场动画;random 表示随机播放入场动画
      android:animation 为子元素指定具体的入场动画
    2. 为子元素指定具体的入场动画:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      // 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" >

      <alpha
      andorid: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:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      <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" />
    4. 除了在 XML 中指定 LayoutAnimation 外,还可以在代码中通过 LayoutAnimationController 来实现:

      1
      2
      3
      4
      5
      6
      ListView listView = (ListView) layout.findViewById(R.id.listview);
      Animation animation = AnimationUtils.loadAnimation(this, R.anim_item);
      LayoutAnimationController controller = new LayoutAnimationController(animation);
      controller.setDelay(0.5f);
      controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
      listView.setLayoutAnimation(controller);

3. Activity 的转场动画

  • Activity 默认的切换效果是从右往左进入、从左往右退出,符合人类交互习惯,但这个效果是可以自定义的,方法很多包括:Windows、Activity、Theme、Style、overridePendingTransition(int enterAnim, int exitAnim)Android 5.0(API21) 提供的 transition API 等

  • Fragment 也可以添加切换动画,由于 Fragment 是在 API 11 中新引入的类,因此为了兼容性需要使用 support-v4 这个兼容包。此时,可以通过 FragmenTransaction 中的 setCustomAnimation() 方法来添加切换动画(需要是 View 动画,不能是属性动画)

  • Demo:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // 当启动一个 Activity 时
    Intent intent = new Intent(this, TestActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim); // 该方法必须位于 startActivity() 后面,否则动画效果不起作用

    // 当 Activity 退出时
    @Override
    public void finish() {
    super.finish();
    overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim); // 该方法必须位于 finish() 后面,否则动画效果不起作用
    }
-------------------- 本文结束感谢您的阅读 --------------------