Android应用开发(10)帧动画(AnimationDrawable)

Android应用开发学习笔记——目录索引

本章介绍AnimationDrawable逐帧动画对象,结合上一章Android应用开发(9)图像视图(ImageView)AnimationDrawable+ImageView实现帧动画显示。

参考:https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable

一、AnimationDrawable介绍

用于创建逐帧动画的对象,由一系列 Drawable 对象定义,可用作 View 对象的背景。

创建逐帧动画的最简单方法是在 XML 文件中定义动画,放置在 res/drawable/ 文件夹中,并将其设置为 View 对象的背景。然后,调用start()运行动画。

  1. 通过XML 文件中定义动画,放置在 res/drawable/ 文件夹中

以 XML 定义的 AnimationDrawable 由单个 <animation-list>元素和一系列嵌套 <item>标签组成。每个项目定义一个动画帧。请参见下面的示例。

res/drawable/ 文件夹中的 spin_animation.xml 文件:

 <!-- Animation frames are wheel0.png through wheel5.png
     files inside the res/drawable/ folder -->
 <animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
 </animation-list>
  1. 通过代码加载和播放此动画

创建背景逐帧动画


 // Load the ImageView that will host the animation and
 // set its background to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setBackgroundResource(R.drawable.spin_animation);

 // Get the background, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation (looped playback by default).
 frameAnimation.start();

创建前景逐帧动画


 // Load the ImageView that will host the animation and
 // set its ImageResource to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setImageResource(R.drawable.spin_animation);

 // Get the Drawable, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable();

 // Start the animation (looped playback by default).
 frameAnimation.start();

AnimationDrawable XML 属性

AnimationDrawable XML 属性

android:drawable

对用于框架的可绘制资源的引用。

android:duration

显示此帧的时间量(以毫秒为单位)。

android:oneshot

如果为真,动画将只运行一次然后停止。

android:variablePadding

如果为 true,则允许 drawable 的填充根据所选的当前状态进行更改。

android:visible

提供可绘制对象的初始可见性状态;默认值为 false。

二、AnimationDrawable Public 方法

Public methods

void

addFrame(Drawable frame, int duration)

向动画添加帧

int

getDuration(int i)

Drawable

getFrame(int index)

int

getNumberOfFrames()

void

inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

从 XML 资源中扩充此 Drawable,该资源可选择按主题设置样式。

boolean

isOneShot()

boolean

isRunning()

指示动画当前是否正在运行。

Drawable

mutate()

使这个 drawable 可变。

void

run()

此方法仅用于实现目的,不应直接调用。

void

setOneShot(boolean oneShot)

设置动画是播放一次还是重复播放。

boolean

setVisible(boolean visible, boolean restart)

设置此 AnimationDrawable 是否可见。

void

start()

从第一帧开始播放动画,必要时循环播放。

void

stop()

在当前帧停止动画。

void

unscheduleSelf(Runnable what)

使用当前Callback实现来取消调度此 Drawable。

Java code 添加帧图片,并播放动画


 // 从layout布局文件中获取名叫imageView的ImageView控件
 ImageView imageView = (ImageView)findViewById(R.id.imageView);
 // 创建一个帧动画对象
 AnimationDrawable frameAnimation = new AnimationDrawable();
 // 添加图片到帧动画的列表中
 frameAnimation.addFrame(getDrawable(R.drawable.frame1), 50);
 frameAnimation.addFrame(getDrawable(R.drawable.frame2), 50);
 frameAnimation.addFrame(getDrawable(R.drawable.frame3), 50);
 
 // 设置帧动画是否只播放一次。为true表示只播放一次,为false表示循环播放
 frameAnimation.setOneShot(false);

 // 设置图像视图的图形为帧动画
 imageView.setImageDrawable(frameAnimation);

 // 开始播放帧动画
 frameAnimation.start();

三、AnimationDrawable测试程序

java:

MainActivity.java


package com.example.animationdrawabletest;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity  implements
        View.OnClickListener{
    private static final String TAG = "lzl-test-AnimationDrawable-Test";
    private ImageView mImageViewByCode, mImageViewByXml;
    private Button mButtonByCode, mButtonByXml;
    private AnimationDrawable mAnimationDrawableByCode, mAnimationDrawableByXml;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageViewByCode = (ImageView) findViewById(R.id.imageView_code);
        mImageViewByCode.setOnClickListener(this);
        mButtonByCode = (Button) findViewById(R.id.button_code);
        mButtonByCode.setOnClickListener(this);
        mButtonByCode.setText("开始");

        mImageViewByXml = (ImageView) findViewById(R.id.imageView_xml);
        mImageViewByXml.setOnClickListener(this);
        mButtonByXml = (Button) findViewById(R.id.button_xml);
        mButtonByXml.setOnClickListener(this);
        mButtonByXml.setText("开始");

        setAnimationDrawableByCode();
        setAnimationDrawableByXml();
    }

    private void setAnimationDrawableByCode() {
        mAnimationDrawableByCode = new AnimationDrawable();

        // 下面图片加入到AnimationDrawable的列表中
        mAnimationDrawableByCode.addFrame(getDrawable(R.drawable.fingerprint_unlock_1), 50);
        mAnimationDrawableByCode.addFrame(getDrawable(R.drawable.fingerprint_unlock_2), 50);
        mAnimationDrawableByCode.addFrame(getDrawable(R.drawable.fingerprint_unlock_3), 50);
        mAnimationDrawableByCode.addFrame(getDrawable(R.drawable.fingerprint_unlock_4), 50);
        mAnimationDrawableByCode.addFrame(getDrawable(R.drawable.fingerprint_unlock_5), 50);

        // false表示循环播放
        mAnimationDrawableByCode.setOneShot(false);
        mImageViewByCode.setImageDrawable(mAnimationDrawableByCode);
        mAnimationDrawableByCode.start(); // 开始播放帧动画
    }

    private void setAnimationDrawableByXml() {
        // 设置从animation_list.xml获取
        mImageViewByXml.setImageResource(R.drawable.animation_list);
        mAnimationDrawableByXml = (AnimationDrawable) mImageViewByXml.getDrawable();
        /*mImageViewByXml.setBackgroundResource(R.drawable.animation_list);
        mAnimationDrawableByXml = (AnimationDrawable) mImageViewByXml.getBackground();*/
        mAnimationDrawableByXml.start(); // 开始播放帧动画
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_code || v.getId() == R.id.imageView_code) {
            // 判断帧动画是否正在播放
            if (mAnimationDrawableByCode.isRunning()) {
                mAnimationDrawableByCode.stop(); // 停止播放帧动画
                mButtonByCode.setText("开始");
            } else {
                mAnimationDrawableByCode.start(); // 开始播放帧动画
                mButtonByCode.setText("暂停");
            }
        }

        if (v.getId() == R.id.button_xml || v.getId() == R.id.imageView_xml) {
            // 判断帧动画是否正在播放
            if (mAnimationDrawableByXml.isRunning()) {
                mAnimationDrawableByXml.stop(); // 停止播放帧动画
                mButtonByXml.setText("开始");
            } else {
                mAnimationDrawableByXml.start(); // 开始播放帧动画
                mButtonByXml.setText("暂停");
            }
        }
    }
}

xml:

activity_main.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/fingerprint_unlock_1" />

        <ImageView
            android:id="@+id/imageView_xml"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/fingerprint_unlock_1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button_code"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="30dp"
            android:layout_weight="1"
            android:text="开始" />
        <Button
            android:id="@+id/button_xml"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="30dp"
            android:layout_weight="1"
            android:text="开始" />
    </LinearLayout>
</LinearLayout>

/res/drawable/animation_list.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/fingerprint_unlock_1" android:duration="50" />
    <item android:drawable="@drawable/fingerprint_unlock_2" android:duration="50" />
    <item android:drawable="@drawable/fingerprint_unlock_3" android:duration="50" />
    <item android:drawable="@drawable/fingerprint_unlock_4" android:duration="50" />
    <item android:drawable="@drawable/fingerprint_unlock_5" android:duration="50" />
</animation-list>

模拟器运行

源码

百度网盘链接:百度网盘 请输入提取码 提取码:test

github下载地址:

GitHub - liuzhengliang1102/AndroidStudio-LearnAppDevelopment

AnimationDrawableTest目录

点此查看Android应用开发学习笔记的完整目录

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AnimationDrawable是一个用于实现动画的类。它允许您将多张图片以特定的顺序显示在屏幕上,从而创建连续的动画效果。 要使用AnimationDrawable,首先需要在XML或Java代码中定义一个AnimationDrawable对象,并将每一图片添加到其中。然后,您可以通过调用start()方法启动动画,或者通过调用stop()方法停止动画。 以下是一个使用AnimationDrawable创建动画的示例: 1. 在XML文件中定义AnimationDrawable对象和每一图片: ```xml <animation-list android:id="@+id/animation" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="100"/> <item android:drawable="@drawable/frame2" android:duration="100"/> <item android:drawable="@drawable/frame3" android:duration="100"/> <!-- 添加更多的图片 --> </animation-list> ``` 2. 在Java代码中获取AnimationDrawable对象,并将其应用到ImageView上: ```java ImageView imageView = findViewById(R.id.imageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); ``` 3. 启动动画: ```java animationDrawable.start(); ``` 这样,您就可以在ImageView上显示动画了。每一图片将按照指定的持续时间进行切换,从而呈现出动画效果。 请注意,AnimationDrawable可以通过调用stop()方法停止动画。此外,您还可以设置动画是否循环播放,通过将android:oneshot属性设置为true或false来实现。 希望这个例子能够帮助您理解如何使用AnimationDrawable创建动画!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuzl_2010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值