標籤:
Drawable animation可以載入Drawable資源實現幀動畫。AnimationDrawable是實現Drawable animations的基本類。
這裡用AnimationDrawable 簡單類比動態圖的實現。
fragment_main 布局檔案 ---- 只需要放一個 ImageView即可
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.yztc.frameanimation.MainActivity" > 6 7 <ImageView 8 android:id="@+id/iv_frame" 9 android:layout_width="match_parent"10 android:layout_height="200dp"11 android:background="@drawable/girl_and_boy" />12 13 </RelativeLayout>
fragment_main
girl_and_boy 布局檔案 ---- 實現動畫
推薦用XML檔案的方法實現Drawable動畫,不推薦在代碼中實現。這種XML檔案存放在工程中res/drawable/目錄下。XML檔案的指令(即屬性)為動畫播放的順序和時間間隔。
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 <!-- onshot 屬性工作表示動畫只執行一次 --> 4 5 <!-- duration 表示期間 --> 6 <item 7 android:drawable="@drawable/girl_1" 8 android:duration="200"> 9 </item>10 <item11 android:drawable="@drawable/girl_2"12 android:duration="200">13 </item>14 <item15 android:drawable="@drawable/girl_3"16 android:duration="200">17 </item>18 <item19 android:drawable="@drawable/girl_4"20 android:duration="200">21 </item>22 <item23 android:drawable="@drawable/girl_5"24 android:duration="300">25 </item>26 <item27 android:drawable="@drawable/girl_6"28 android:duration="400">29 </item>30 <item31 android:drawable="@drawable/girl_7"32 android:duration="500">33 </item>34 <item35 android:drawable="@drawable/girl_8"36 android:duration="400">37 </item>38 <item39 android:drawable="@drawable/girl_9"40 android:duration="300">41 </item>42 <item43 android:drawable="@drawable/girl_10"44 android:duration="200">45 </item>46 <item47 android:drawable="@drawable/girl_11"48 android:duration="200">49 </item>50 51 </animation-list>
girl_and_boy
MainActivity
1 package com.dragon.android.initgif; 2 3 import android.app.Activity; 4 import android.graphics.drawable.AnimationDrawable; 5 import android.os.Bundle; 6 import android.widget.ImageView; 7 8 public class MainActivity extends Activity { 9 10 @Override11 protected void onCreate(Bundle savedInstanceState) {12 super.onCreate(savedInstanceState);13 setContentView(R.layout.fragment_main);14 15 ImageView ivFrame = (ImageView) findViewById(R.id.iv_frame);16 // 得到一個動畫圖片17 AnimationDrawable background = (AnimationDrawable) ivFrame18 .getBackground();19 // 開始播放20 background.start();21 // 停止方法.22 // background.stop();23 }24 25 }
圖片素材
girl_1.gif girl_2.gif girl_3.gif
girl_4.gif girl_5.gif girl_6.gif
girl_7.gif girl_8.gif girl_9.gif
girl_10.gif girl_11.gif
Android之AnimationDrawable初識