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

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

首先创建一个XML文件,里边有很多张图片,记得type类型选择 drawable。 root element选择如下的animation-list

<!-- Animation frames are wheel0.png -- 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>


上边的图片就是很多帧图片组合成一个动态的图片。。后边的参数是间隔时间50毫秒,  oneshot属性为false,表示循环播放,不是只循环一次。。

后边就是如下代码了,把这个资源文件设置为一个背景,再获取这个背景,强转为AnimationDrawable,之后调用start()方法开启,停止动画是stop();

 

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


需要说明的一点是,start方法不能使用在oncreate()里,因为这时候应用窗体还没加载完毕了。。启动动画两种方式,手动的,比如在一个按钮的点击事件里,启动。

 

另一个,自动启动,重写下边的方法。就是应用程序窗体是否加载完毕并获得了焦点。

 

@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if(hasFocus)anim.start();
		else anim.stop();
		super.onWindowFocusChanged(hasFocus);
	}


刚开始弄这个,为了实现自动开启动画,我还弄了个handler来开启动画,后来看了API才找到上边简单的方法,希望对大家有帮助。

下边是API的描述:

public void start ()
Added in API level 1

Starts the animation, looping if necessary. This method has no effect if the animation is running. Do not call this in the onCreate(Bundle) method of your activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged(boolean) method in your activity, which will get called when Android brings your window into focus.

 

AndroidAnimationDrawable动画实现 AnimationDrawableAndroid 框架中的一个类,用于在视图(如 ImageView)中播放帧动画。帧动画是通过一系列静态图像(帧)的连续显示来创建动画效果的。AnimationDrawable 类提供了加载和播放这些帧动画的功能。 阅读详情

相关推荐

AndroidAnimationDrawable使用的简单实例

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

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

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

zsyf33078的专栏 2950

Android应用源码】高仿墨迹天气背景动画效果-云,风,雪等.zip

资源说明: 1:本资料仅用作交流学习参考,请切勿用于商业用途。 2:一套精品实用安卓应用APP源码资源,无论是入门练手还是项目复用都超实用,省去重复开发时间,让开发少走弯路!

自定义视图:图形与图像的处理(一):使用简单图片

Android系统提供了ImageView显示普通的静态图片,也提供了AnimationDrawable来开发逐帧动画,还可通过Animation对普通图片使用补间动画。图形、图像处理不仅对Android系统的应用界面非常重要,而且Android系统上的益智类游戏、2D游戏都需要大量的图形、图像处理。 所谓游戏,本质就是提供更逼真的、能模拟某种环境的用户界面,并根据某种规则来响应用户操作。为了提供更逼真的用户界面,需要借助于图形、图像处理。 从广义的角度来看,Android应用中

indeedes的博客 1210

Android加载AnimatedImageDrawable播放gif动态图,Kotlin

Android AnimationDrawable动画与APP启动、加载引导页面(画面)AnimationDrawableAndroid的Frame动画,可以简单的认为此AnimationDrawable能够将一系列资源图片加载成“电影”一样播放。Android加载Gif图片的一般方法:Movie实现Android的ImageView无法直接加载Gif图片,如果需要在自己的代码中加载一个gif图片(这很常见,比如下载过程中的loading以示正在下载的转动的圆球),则无法直接用ImageView。

Zhang Phil 2079

Android--xml实现动画

使用AnimationDrawable类的API.,可以简单通过XML文件 列出动画中的所有帧,这种类别动画的XML文件放入工程中的res/anim目录 在XML文件包含一个根节点元素和好几个子节点来定义每帧。一个资源分别定 义了帧的名字与帧的持续时间。,下面为范例: <animation-list xmlns:android="http://schemas.android.com/apk/re

tzyh0116的专栏 505

利用AnimationDrawable实现控件的背景图片循环切换

一、适用情况 1)任何有 android:background ="" 属性的控件。 2)实现的效果:在同一个控件的位置,图片循环切换。如果是几张连续的图片,你可以实现比如说小孩在跑步,文件在传输等动画效果。因为懒,没有效果图提供。二、实现方式: 贼简单,分三步走 1)在res/drawable下建立一个img_anim.xml文件,如下:<?xml version="1.0" encodi

jingwenpeng的博客 1503

anim动画

制作animation类型的背景图片; duration;持续事件;单位是毫秒; <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/a1" android:duration="12

zy的博客 1124

动画——animation

ImageView startGame = (ImageView) findViewById(R.id.egg); startGame.setBackgroundResource(R.drawable.animation1); animationDrawable = (AnimationDrawable) startGame.getBackground(); animationDrawable.s

雨一直下 676

34 帧动画

在res/anim目录下新建文件: <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"><!--android:oneshot="...

weixin_30566063的博客 124

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

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

Android_I_lOVE的博客 4717

Android 编程下 AnimationDrawable (动画)的实现

Drawable Animation 可以让我们按顺序加载一系列的资源来创建一个动画动画的创建和传统意义上电影胶卷的播放一样,是通过加载不同的图片,然后按顺序进行播放来实现的。在代 码的实现AnimationDrawable 类是基于 Drawable animations 来实现的。 虽然我们可以通过 AnimationDrawable 类的 API 在代码中定义一个动画的所有帧,但

practicemperfect的博客 509

android动画xml,Android--xml实现动画

使用AnimationDrawable类的API.,可以简单通过XML文件列出动画中的所有帧,这种类别动画的XML文件放入工程中的res/anim目录在XML文件包含一个根节点元素和好几个子节点来定义每帧。一个资源分别定义了帧的名字与帧的持续时间。,下面为范例:xmlns:android="http://schemas.android.com/apk/res/android"android:one...

weixin_30738065的博客 284

Android成长之路-实现简单动画

实现简单动画: 在drawable目录中放入图片, 并且创建xml文件 frame.xml 存入图片,如下: <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">

2262

android添加人物动画效果,为可绘制图形添加动画效果

在某些情况下,图片需要在屏幕上呈现动画效果。如果您希望显示由多张图片组成的自定义加载动画,或者希望一个图标在用户执行操作后变为另一个图标,这种做法就非常实用。Android 提供了两个选项供您为可绘制对象添加动画效果。第一个选项是使用 AnimationDrawable。使用该选项,您可以指定多个静态可绘制对象文件(每次展示一个)来创建动画。第二个选项是使用 AnimatedVectorDrawa...

weixin_42602726的博客 428

Android利用Drawable Animation实现图片动画播放

关注微信号:javalearns   随时随地学Java 或扫一扫 随时随地学Java (目前只能用在View对象上的动画效果的实现有两种,一种就是上一篇的View Animation,即补间动画,剩下的一种,就是这一篇要介绍的,Drawable Animation,即帧动画。在最新版本的API中,出现了一个更加强大的方式,PropertyAnim

681
上一篇: android中如何实现开机启动某些服务,activity等
下一篇: Intent一堆属性的意义
29boy
博客等级 码龄17年 7粉丝 22原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值