Android 之AnimationDrawable

AnimationDrawable是Android实现动画的一种简单的形式。如果需要简单的动画,例如屏幕锁。这里用一个Demo来简单讲解下。

首先最基本的,我们可以参见DOC上的说明,它的资料最正宗!

Class Overview
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call run() to start the animation.

An AnimationDrawable defined in XML consists of a single <animation-list> element, and a series of nested <item> tags. Each item defines a frame of the animation. See the example below.

spin_animation.xml file in res/drawable/ folder:

<!-- Animation frames are wheel0.png -- wheel5.png files inside the
res/drawable/ folder -->


<animation-list

android: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>
Here is the code to load and play this animation.


// 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
()


以上的资料大家也可以在DOC中看的很明白。

一:新建一个工程AnimationDrawableDemo。
二:在res/drawable下面放入我们需要演示的图片。
lock_anim_00.png
lock_anim_02.png
lock_anim_04.png
lock_anim_06.png
lock_anim_08.png
lock_anim_10.png
lock_anim_12.png
lock_anim_14.png


新建一个Animation资源文件。文件如下:
view plaincopy to clipboardprint?<?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/lock_anim_00" android:duration="400" /> <item android:drawable="@drawable/lock_anim_02" android:duration="400" /> <item android:drawable="@drawable/lock_anim_04" android:duration="400" /> <item android:drawable="@drawable/lock_anim_06" android:duration="400" /> <item android:drawable="@drawable/lock_anim_08" android:duration="400" /> <item android:drawable="@drawable/lock_anim_10" android:duration="400" /> <item android:drawable="@drawable/lock_anim_12" android:duration="400" /> <item android:drawable="@drawable/lock_anim_14" android:duration="400" /> </animation-list> <?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/lock_anim_00" android:duration="400" />
<item android:drawable="@drawable/lock_anim_02" android:duration="400" />
<item android:drawable="@drawable/lock_anim_04" android:duration="400" />
<item android:drawable="@drawable/lock_anim_06" android:duration="400" />
<item android:drawable="@drawable/lock_anim_08" android:duration="400" />
<item android:drawable="@drawable/lock_anim_10" android:duration="400" />
<item android:drawable="@drawable/lock_anim_12" android:duration="400" />
<item android:drawable="@drawable/lock_anim_14" android:duration="400" />
</animation-list>



三:在main.xml文件中导入一个ImageView控件。函数代码如下:
view plaincopy to clipboardprint?package com.rocky.studio.ch02.ex2; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class AnimationDrawableDemo extends Activity { private Button m_RunButton; private Button m_StopButton; private ImageView imgView; private AnimationDrawable mAnimation; private static final long START_DELAY = 1000; private Handler mHandler = new Handler(); private Runnable mRunnable = new Runnable() { public void run() { startAnimation(mAnimation); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_RunButton=(Button)this.findViewById(R.id.Button01); m_StopButton=(Button)this.findViewById(R.id.Button02); m_RunButton.setOnClickListener( m_BtnRunClickListener); m_StopButton.setOnClickListener(m_BtnStopClickListener); imgView=(ImageView)this.findViewById(R.id.ImageView01); imgView.setBackgroundResource(R.drawable.lock_anim); mAnimation = (AnimationDrawable) imgView.getBackground(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); //stopAnimation(mAnimation); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); //mHandler.postDelayed(mRunnable, START_DELAY); } private View.OnClickListener m_BtnRunClickListener=new View.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub startAnimation(mAnimation); } }; private View.OnClickListener m_BtnStopClickListener=new View.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub stopAnimation(mAnimation); } }; protected void startAnimation(final AnimationDrawable animation) { if (animation != null && !animation.isRunning()) { animation.run(); } } protected void stopAnimation(final AnimationDrawable animation) { if (animation != null && animation.isRunning()) animation.stop(); } } package com.rocky.studio.ch02.ex2;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationDrawableDemo extends Activity {

private Button m_RunButton;
private Button m_StopButton;
private ImageView imgView;
private AnimationDrawable mAnimation;
private static final long START_DELAY = 1000;


private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
public void run() {
startAnimation(mAnimation);
}
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

m_RunButton=(Button)this.findViewById(R.id.Button01);
m_StopButton=(Button)this.findViewById(R.id.Button02);
m_RunButton.setOnClickListener( m_BtnRunClickListener);
m_StopButton.setOnClickListener(m_BtnStopClickListener);

imgView=(ImageView)this.findViewById(R.id.ImageView01);

imgView.setBackgroundResource(R.drawable.lock_anim);
mAnimation = (AnimationDrawable) imgView.getBackground();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//stopAnimation(mAnimation);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//mHandler.postDelayed(mRunnable, START_DELAY);
}
private View.OnClickListener m_BtnRunClickListener=new View.OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
startAnimation(mAnimation);
}
};
private View.OnClickListener m_BtnStopClickListener=new View.OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopAnimation(mAnimation);
}

};
protected void startAnimation(final AnimationDrawable animation) {
if (animation != null && !animation.isRunning()) {
animation.run();
}
}

protected void stopAnimation(final AnimationDrawable animation) {
if (animation != null && animation.isRunning()) animation.stop();
}

}



四:如果需要一打开就动画显示://mHandler.postDelayed(mRunnable, START_DELAY);
这句话的意思是开启一个线程,开始执行操作。

五:这样就基本完成了操作。最好还是些个Demo来测试练练手。
【Android】解决AnimationDrawable (逐帧动画)导致 Native 层内存增加问题 在上面的代码中,我们首先通过 findViewById() 方法获取一个 ImageView 对象,然后通过 getResources().getDrawable() 方法获取一个 AnimationDrawable 对象,然后将 AnimationDrawable 对象设置给 ImageView,最后调用 start() 方法启动动画。及时释放资源:在动画结束时,及时释放资源,例如调用 AnimationDrawable 的 stop() 方法和 setVisible() 方法,避免资源被持续占用。 阅读详情

相关推荐

Android中AnimationDrawable使用的简单实例

介绍了Android中AnimationDrawable使用的简单实例,有需要的朋友可以参考一下

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

本章介绍AnimationDrawable逐帧动画对象,使用AnimationDrawable+ImageView实现帧动画显示。

zsyf33078的专栏 2963

【3D目标检测】环境搭建(OpenPCDet、MMdetection3d、Det3d)

在Conda虚拟环境中搭建MMdetection3d环境

qq_44703886的博客 4254

Android_AnimationDrawable介绍及使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666) 转自:http://blog.csdn.net/zqiang_55/article/details/8142785 Drawable animation可以加载Drawable资源实现帧动画。AnimationD...

孤云博客 1728

玩转Isaac Sim动态控制:从随机舞动到精准操控Franka的5种控制模式实测

本文深入实测了在NVIDIA Isaac Sim仿真平台中,利用Dynamic Control模块对Franka机械臂进行精准操控的五种核心模式:位置控制、单关节位置控制、速度控制、单关节速度控制和力矩控制。通过详细的代码示例和对比分析,阐述了每种模式的适用场景、参数调优技巧及从随机运动到闭环控制的实现路径,为机器人算法验证提供了实用指南。

night的博客 809

保姆级教程:用Protege 5.5.0从零构建你的第一个知识图谱(附实战案例文件)

本文提供了一份详细的Protege 5.5.0使用指南,从零开始构建知识图谱。涵盖环境配置、本体结构设计、实例填充到可视化展示的全流程,特别适合初学者快速掌握知识建模技术。通过实战案例演示,帮助读者理解如何定义类、属性及建立实例关系,是学习知识图谱构建的实用教程。

weixin_42548816的博客 556

Android中AnimationDrawable,Animator和Animation

AnimationDrawable、Animation、Animator

盛夏光年的学习笔记 1689

Istio ICA考试之路---3-2

部署2个测试容器,分别是test和test2.test注入标签subset=v1。这里文档中省略了metadata的3行,可以从前面任意一个yaml复制粘贴过来。通过test2测试容器访问,50%概率触发500故障注入。通过test测试容器访问,20%概率触发延迟注入。发起20次请求,一共10次被500。

Q先生 732

android之AnimationDrawable

AnimationDrawable是Android实现动画的一种简单的形式,可以用来实现帧动画。 1.在res/drawable下定义friend.xml文件: <animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> 其

2980

图像分割 - 使用Mask R-CNN模型在COCO数据集上进行图像分割任务

图像分割是计算机视觉领域的一个重要任务,它旨在将图像中的每个像素分类为对象或背景。Mask R-CNN是一种基于区域的卷积神经网络,可用于图像分割任务。它是Faster R-CNN的扩展,将目标检测与图像分割结合起来,能够同时检测和分割对象。在本博客中,我们将使用Mask R-CNN模型在COCO数据集上进行图像分割任务。我们将介绍如何下载和准备COCO数据集,以及如何使用Mask R-CNN模型进行训练和推理。

斌擎科技 1190

汇川H5U PLC 实现直线插补、圆弧插补、直线圆弧混合连续插补

使用汇川H5UPLC实现直线插补,圆弧插补,直线圆弧混合路径插补,通过示教可以走出复杂的路径,仅供参考学习。

weixin_39832501的博客 4804

Android AnimationDrawable运行的几种方式

项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷。google搜了下。记录一下。        这个AnimationDrawable.start不能直接写在onClick,onStart,onResume里面,是无效的,无法启动动画,只能写在比如事件监听当中。           以下有几种运行AnimationDrawable的方式。 第一种:在

liuhanhan512的专栏 4537

机器人外接 USB 音响完整配置指南

摘要:本文介绍了为机器人配置外接音响的方法。由于内置音响音量小且有散热噪音,建议使用Type-C接口外接音响。配置步骤包括:1)通过aplay -l命令查看音响端口信息;2)使用aplay命令测试音频播放;3)通过amixer命令设置音量。关键点在于正确识别音响的卡号(如card 2)和设备号(如device 0),并在命令中准确配置这些参数即可实现外接音响的正常使用。

m0_47999117的博客 361

android中通过AnimationDrawable实现动画背景图片

首先创建一个XML文件,里边有很多张图片,记得type类型选择 drawable。 root element选择如下的animation-list <!-- Animation frames are wheel0.png -- wheel5.png files inside the res/drawable/ folder -->

老男孩的网络笔记本 3605

华为OD新系统机试真题-勇攀数字高峰(Java/Py/C/C++/Js/Go)

新系统 华为OD机试新系统【勇登数字高峰】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD新系统机试真题【勇登数字高峰】

qq_45776114的博客 1557

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

android 位移动画路径,Android动画之——AnimationDrawable

最近在学Android的AnimationDrawable,简单的写一个动画的实现过程。简单介绍Animation代表一个动画,目前Android支持逐帧动画(就是所谓的老电影哪样一张一张放映出来)也支持补间动画(就是通过平移、变化计算出来的动画)。代码片段定义动画的XML文件应该放在/res/anim路径下,注意创建的XML类型是TweenAnimation类型的。下面首先定义一个XML文件:a...

weixin_42508058的博客 357

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Android Frame动画——AnimationDrawable

有两种方式 1、通过在xml定义一个animation-list: <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > 然后在Java中的代码处理如下: // Load

laukaka的专栏 1483

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Android AnimationDrawable运行的几种方式

项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷。google搜了下。记录一下。        这个AnimationDrawable.start不能直接写在onClick,onStart,onResume里面,是无效的,无法启动动画,只能写在比如事件监听当中。           以下有几种运行AnimationDrawable的方式。 第一种:在

5975

Android动画 - AnimationDrawable帧动画

类似于电影一样,一帧一帧的播放图片 同样,创建AnimationDrawable有两种方式 XML文件+Java代码 Java代码方式 不同于之前的ViewAnimation,之前的动画xml文件存放位置是:res/anim/xxx.xml 而AnimationDrawable存放位置是:res/drawable/xxx.xmlXML文件+Java代码anim_drawable.xml<?xml

shibin1990_的博客 3368

Android动画 之 FrameAnimation(AnimationDrawable)

public class AnimationDrawable extends DrawableContainer implements Animatable Runnable   一、创建动画播放资源列表 res/anim/animation_drawable.xml 二、设置ImageView的background属性 1.通过xml文件设置<Ima

凡夫草芥 747

Android应用通过AnimationDrawable实现View的动画效果

在Android应用中可以通过AnimationDrawable轻松实现View的动画效果,zhe

mingy的专栏 1739

android startanimation 回调,AnimationDrawable 动画结束监听

最近在做帧动画,由于AnimationDrawable其实就是个Drawable,本身并没有监听,研究了很多方法,网上的方法都是通过启动一个Handler,延时停止动画,这种方法虽然能实现,但是有个很大的问题,就是假如机器卡顿,那么动画就和Handler不同步了,动画持续时间越长,这个问题就越严重,于是各种查找方法,最后在API中发现了一行没有注释的代码,对,就是没有注释,结果就让我忽略的方法ge...

weixin_35110559的博客 1474

Android中使用多张图片组成动画 AnimationDrawable

AnimationDrawable 就是在andriod里可以逐帧的播放图片,然后产生一种动态的效果; Drawable Animation 可以让我们按顺序加载一系列的资源来创建一个动画。动画的创建和传统意义上电影胶卷的播放一样,是通过加载不同的图片,然后按顺序进行播放来实现的。在代 码的实现上 AnimationDrawable 类是基于 Drawable animations 来实现的

Android_I_lOVE的博客 4723
上一篇: Android最简单播放GIF动画方法
下一篇: Android上开发手机浏览器原理
abc20899
博客等级 码龄17年 15粉丝 562原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值