Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android ImageView 動畫效果實作

編輯:Android 開發實例

前言

Android 的 ImageView 可透過 AnimationDrawable 實現逐格動畫。做法是先在 drawable 目錄建立 animation-list,再於程式碼中啟動動畫。

建立動畫資源

animation-list 中可設定 oneshot 是否只播放一次,並以多個 item 指定每張圖與持續時間。每個 item 的 duration 單位為毫秒。

啟動動畫

取得 ImageView 背景後,轉型為 AnimationDrawable,再呼叫 start。若 ImageView 本身已有圖片,可先設定空 Drawable,避免背景動畫被覆蓋。

AnimationDrawable animator = (AnimationDrawable) imageView.getBackground();
imageView.setImageDrawable(null);
animator.start();

停止動畫

停止時可呼叫 stop,並將 ImageView 重設為靜態圖片。

AnimationDrawable animator = (AnimationDrawable) imageView.getBackground();
animator.stop();
imageView.setImageResource(R.drawable.icon);

這種方式適合簡單的載入動畫、狀態提示或逐格圖片效果。

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