Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫入門:四種基本動畫與 Animation 的用法

編輯:關於 Android 程式設計

概述

Android 裡最基礎的幾種動畫效果無非四類:平移、透明度漸變、旋轉、縮放。實現方式則有兩條路:一是在 Java 代碼裡動態創建,二是寫在 XML 中靜態聲明,再於運行時加載。

一、動態實現

/* 透明度漸變:從 1 到 0 */
AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
alphaAnimation.setDuration(1000);   // 動畫時長
alphaAnimation.setStartOffset(200); // 延遲多久開始
mImageViewAnim.startAnimation(alphaAnimation);
// 注意:ImageView 的圖片要放在 src 中,或通過 background 加載

/* 平移:從一個坐標移動到另一個坐標
   四個參數依次是 起點橫坐標、起點縱坐標、終點橫坐標、終點縱坐標 */
TranslateAnimation translateAnimation =
        new TranslateAnimation(-mImageViewAnim.getMeasuredWidth(), 0, 0, 0);
translateAnimation.setDuration(1000);
translateAnimation.setStartOffset(200);
mImageViewAnim.startAnimation(translateAnimation);

/* 旋轉:默認繞左上角旋轉 */
RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
rotateAnimation.setDuration(1000);
rotateAnimation.setStartOffset(200);
mImageViewAnim.startAnimation(rotateAnimation);

/* 縮放:分別指定水平與豎直方向的尺寸變化 */
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f);
scaleAnimation.setDuration(1000);
scaleAnimation.setStartOffset(200);
mImageViewAnim.startAnimation(scaleAnimation);

二、組合動畫:AnimationSet

要把多個動畫疊在一起播放,需要一個 AnimationSet 把它們裝起來:

/**
 * AnimationSet 是存放 animation 的集合
 * 構造參數 false:set 中每個 animation 各自使用自己的 interpolator
 * 構造參數 true:所有 animation 共用同一個 interpolator
 */
AnimationSet animationSet = new AnimationSet(false);

AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
TranslateAnimation translateAnimation = new TranslateAnimation(
        0, 0,
        mImageViewAnim.getMeasuredWidth(),
        mImageViewAnim.getMeasuredHeight());
// RELATIVE_TO_SELF 表示相對於自身的位置
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f);

alphaAnimation.setDuration(1000);
translateAnimation.setDuration(1000);
rotateAnimation.setDuration(1000);
scaleAnimation.setDuration(1000);

animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);

mImageViewAnim.startAnimation(animationSet);

運行起來,看到的就是幾種效果疊加在一起的綜合呈現。

三、靜態實現:XML 描述

先在 res 下新建 anim 目錄,在其中放入資源文件(示例名為 rotation.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/anticipate_interpolator">

    <!-- 常用 interpolator:
         cycle_interpolator               —— 來回車輪效果
         accelerate_decelerate_interpolator —— 先加速後減速
         bounce_interpolator              —— 反覆彈起(適合平移、下落)
         anticipate_interpolator          —— 預備動作效果 -->

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        android:repeatCount="0"
        android:startOffset="100" />
    <!-- 從 0 度轉到 360 度,繞自身中心;延遲 100ms 啟動,每次持續 2000ms -->

    <scale
        android:fromXScale="1"
        android:toXScale="2"
        android:fromYScale="1"
        android:toYScale="2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000" />
    <!-- 水平與豎直都從 1 倍放大到 2 倍,以自身中心為軸,歷時 2000ms -->

</set>

四、加載 XML 動畫

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
mImageViewAnim.startAnimation(animation);

運行效果同样是 XML 中幾種動畫的合成結果。對比兩種方式:動態寫法靈活、可運行時調整參數;XML 寫法結構清晰、便於復用與交付給設計實現。項目中通常兩者混用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved