Drawable animation可以加载Drawable资源实现帧动画。AnimationDrawable是实现Drawable animations的基本类。
这里用AnimationDrawable 简单模拟动态图的实现。
fragment_main 布局文件 ---- 只需要放一个 ImageView即可
images/loading.gif' data-original="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" />
1 <RelativeLayout ="http://schemas.android.com/apk/res/android" 2 ="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 布局文件 ---- 实现动画
推荐用
1 <??> 2 <animation-list ="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初识
关键词:Android