Android AnimationDrawable

AnimationDrawable 可以用于把一系列的 Drawable,按照一定得顺序一帧帧地播放

AnimationDrawable 使用 <animation-list> 作为根节点

属性

属性 说明
android:oneshot 设置是否循环播放,false 为循环播放
android:duration 帧间隔时间,通常我们会设置为 300 毫秒

<animation-list> 可以包含一个或多个 <item>

<item> 的属性如下

属性 说明
android:drawable 设置位图资源
android:duration 帧间隔时间,通常我们会设置为 100 毫秒

方法

获得 AniamtionDrawable 实例后,需要调用它的 start() 方法播放动画

注意

在 onCreate() 方法中调用的话,是没有任何效果的,因为 View 还没完成初始化
可以用简单的 handler 来延迟播放动画

更多方法可以参考 Android AnimationDrawable 运行的几种方式

范例

使用 AnimationDrawable 来实现帧动画真的是非常方便的


  1. 创建一个 空的 Android 项目 cn.twle.android.AnimationDrawable

  2. 下载 /static/i/android/tuzi.zip 解压并把所有的图片拖到 res/drawable 目录

  3. 定义一个 AnimationDrawable 的 xml 资源文件

    res/drawable/anim_tuzi.xml

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
        <item
            android:drawable="@drawable/tuzi_1"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_2"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_3"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_4"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_5"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_6"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_7"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_8"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_9"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_10"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_11"
            android:duration="100" />
        <item
            android:drawable="@drawable/tuzi_12"
            android:duration="100" />
    </animation-list>
    
  4. 修改 activity_main.xml 添加一个 ImageView

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView 
            android:id="@+id/img_show"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            android:src="@drawable/anim_tuzi" />
    
    </LinearLayout>
    
  5. 修改 MainActivity.java

    package cn.twle.android.animationdrawable;
    
    import android.graphics.drawable.AnimationDrawable;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
    
        private ImageView img_show;
        private AnimationDrawable ad;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            img_show = (ImageView) findViewById(R.id.img_show);
            // 核心实现代码
            ad = (AnimationDrawable) img_show.getDrawable();
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ad.start();
                }
            }, 300);
        }
    }
    

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

♥   简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.