Drawable Animation 开发者文档


Drawable animation lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The AnimationDrawable class is the basis for Drawable animations.
Drawable动画可以让您一个接一个地加载一系列的Drawable资源来创建动画。 这是一种传统的动画,它的意义是创建一系列不同的图像,按顺序播放,像一卷电影。 AnimationDrawable类是可绘制动画的基础。

While you can define the frames of an animation in your code, using the AnimationDrawable class API, it's more simply accomplished with完成 a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.
虽然您可以在代码中定义动画的框架,但使用AnimationDrawable类API,只需完成一个列出构成动画的框架的XML文件即可完成。 这种动画的XML文件属于您的Android项目的res / drawable /目录。 在这种情况下,(需要设定的)指令是动画每帧的顺序和持续时间。

The XML file consists of an <animation-list> element as the root node and a series of child <item> nodes that each define a frame: a drawable resource for the frame and the frame duration. Here's an example XML file for a Drawable animation:
XML文件由作为根节点的<animation-list>元素和每个定义一个帧的一系列子<item>节点组成:每一帧的drawable资源和持续时间。 以下是可绘制动画的XML文件示例:
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:oneshot="true">
  3.     <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
  4.     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
  5.     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
  6. </animation-list>

This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here's an example Activity, in which the animation is added to an ImageView and then animated when the screen is touched:
此动画只运行三帧。 通过将列表的android:oneshot属性设置为true,它将循环一次,然后停止并按住最后一帧。 如果设置为false,则动画将循环。 将此XML保存为项目的res / drawable /目录中的rocket_thrust.xml,可以将其作为背景图像添加到View,然后调用播放。 下面是一个示例,其中将动画添加到ImageView中,然后在触摸屏幕时进行动画:
  1. AnimationDrawable rocketAnimation;
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  6. rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  7. rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
  8. }
  9. public boolean onTouchEvent(MotionEvent event) {
  10. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  11. rocketAnimation.start();
  12. return true;
  13. }
  14. return super.onTouchEvent(event);
  15. }

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() 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() method in your Activity, which will get called when Android brings your window into focus.
请注意,在Activity的onCreate()方法中,不能调用AnimationDrawable的start()方法,因为AnimationDrawable尚未完全附加到窗口。 如果要立即播放动画,而不需要交互,那么您可能希望在Activity中的onWindowFocusChanged()方法中调用它,当Android将您的窗口置于焦点时,该动画将被调用。

AnimationDrawable 类

继承体系

public class AnimationDrawable extends DrawableContainer implements Runnable, Animatable
java.lang.Object
   ↳ android.graphics.drawable.Drawable
    ↳ android.graphics.drawable.DrawableContainer
    ↳ android.graphics.drawable.AnimationDrawable
可以看到 AnimationDrawable 是 Drawable 的简介子类。

公共方法

  • void addFrame(Drawable frame, int duration):Adds a frame to the animation
  • int getDuration(int i):Return the duration in milliseconds of the frame at the specified index
  • Drawable getFrame(int index):Return the Drawable at the specified frame index
  • int getNumberOfFrames():Return the number of frames in the animation
  • void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme):Inflate this Drawable from an XML resource optionally styled by a theme.
  • boolean isOneShot():Return true of the animation will play once, false otherwise
  • boolean isRunning():Indicates whether the animation is currently running or not.
  • Drawable mutate():Make this drawable mutable易变的.  This operation cannot be reversed反转.
  • void run():This method exists for implementation purpose only and should not be called directly.  Invoke start() instead.
  • void setOneShot(boolean oneShot):Sets whether the animation should play once or repeat.
  • boolean setVisible(boolean visible, boolean restart):Sets whether this AnimationDrawable is visible.
  • void start():Starts the animation, looping if necessary. This method has no effect if the animation is running.
  • void stop():Stops the animation. This method has no effect if the animation is not running.
  • void unscheduleSelf(Runnable what):Use the current Drawable.Callback implementation to have this Drawable unscheduled.
打底色的三个方法是 Animatable 接口中定义的方法

帧动画简介

Drawable Animation 或者 Frame Animation,帧动画,就像GIF图片(或电影)一样,是通过依次显示一系列的 Drawable 来模拟动画的效果。
以<animation-list>为根元素,一个<item>表示一帧要轮换显示的图片
oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
duration属性表示此帧显示的时间

注意:帧动画是指背景动画,所以只能设置 background 属性为指定的帧动画,或在代码中通过 setBackgroundResource(R.drawable.amin_id) 指定帧动画,但是不能设置 src 属性为指定的帧动画。当然两者可以同时设置,此时src保持不变,背景是个动画会改变。

注意:开启或关闭帧动画前最好先判断获取的帧动画是否为空,因为帧动画是通过 getBackground() 强转过来的,可能不存在!

最重要的:不要在onCreate()中调用start方法开启帧动画,因为此时帧动画还没有完全跟Window相关联,如果想要在界面显示时就立即开始动画的话,可以在onWindowFoucsChanged()方法中调用start方法。

虽然定义三种动画的xml文件都可以放置在res/anim/文件夹中,但是建议:
  • 帧动画放在res/drawable文件夹中
  • 补间动画放在res/anim文件夹中
  • 属性动画放在res/animator文件夹中

【帧动画总结】AnimationDrawable Frame的更多相关文章

  1. android 帧动画的实现及图片过多时OOM解决方案(一)

    一,animation_list.xml中静态配置帧动画的顺序,如下: <?xml version="1.0" encoding="utf-8"?> ...

  2. TimePicker控件、帧动画、补间动画

    1.TimePicker控件 最近感觉每个开发平台的控件基本都差不多,在Android中控件的事件和.net控件直接写事件有一定的区别,net事件可以直接界面进行事件的绑定哈.不过在Silverlig ...

  3. Android开发(25)--framebyframe帧动画并实现启动界面到主界面的跳转

    Drawable animation可以加载Drawable资源实现帧动画.AnimationDrawable是实现Drawable animations的基本类.推荐用XML文件的方法实现Drawa ...

  4. android中的逐帧动画

    在android中实现动画最简单的一种方式就是使用逐帧动画(AnimationDrawable).逐帧动画的原理同最古老的动画机制是一样的,通过快速的播放一组变化微小的图片,在人眼的视差时间下,达到一 ...

  5. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  6. Android 学习笔记多媒体技术之 Drawable类+Tween(补间动画)+Frame(帧动画)

    学习内容: 1.了解Drawable类的作用 2.如何使用Drawable... 3.了解Tween动画... 4.如何创建和使用Tween动画... 1.Drawable类...   Drawabl ...

  7. Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation

    程序运行效果图: Android动画主要包含补间动画(Tween)View Animation.帧动画(Frame)Drawable Animation.以及属性动画Property Animatio ...

  8. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  9. 帧动画 AnimationDrawable

    Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果. 首先,在res/drawable中定义动画 < ...

随机推荐

  1. 【caffe-Windows】微软官方caffe之matlab接口配置,以及安装caffe的注意事项

    1.在此之前,记录一下之前的错误,在参考博客[caffe-Windows]caffe+VS2013+Windows+GPU配置+cifar使用进行caffe的安装时,其中的一些步骤可以不做,具体见下图 ...

  2. MVC Partial页面的使用

    先建立Action: public PartialViewResult CurrentCount() { ViewBag.Count = CurrentUserCount; return Partia ...

  3. CentOS 7安装Mysql并设置开机自启动的方法

    CentOS 7不带Mysql数据库了,默认的数据库是MariaDB(Mysql的一个分支). 可以按照以下步骤手动安装Mysql数据库. 1. 下载rpm安装文件 ? 1 wget http://r ...

  4. thinkphp5.0架构总览

    ThinkPHP5.0应用基于MVC(模型-视图-控制器)的方式来组织. MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型(M).视图(V) ...

  5. Eclipse项目红色叹号解决方法

    情况:就是项目出现红色感叹号 解决方法: 对准项目右键选择Build Path → configure build path 点击eclipse项目的configure build path后,在弹出 ...

  6. XSS漏洞自动化攻击工具XSSer

    XSS漏洞自动化攻击工具XSSer   XSS是Web应用常见的漏洞.利用该漏洞,安全人员在网站注入恶意脚本,控制用户浏览器,并发起其他渗透操作.XSSer是Kali Linux提供的一款自动化XSS ...

  7. Prime Number CodeForces - 359C (属于是数论)

    Simon has a prime number x and an array of non-negative integers a1, a2, ..., an. Simon loves fracti ...

  8. [BZOJ4032][HEOI2015]最短不公共子串(Trie+DP)

    在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之——被它们虐. 操作一:对A,B分别建SAM,暴力BFS. 操作二:对B建序列自动机或SAM,A在上面暴力匹配. 操作三:对A,B建 ...

  9. Codeforces Round #346 (Div. 2) G. Fence Divercity dp

    G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...

  10. UESTC 2015dp专题 N 导弹拦截 dp

    导弹拦截 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descrip ...