在开始实例讲解之前,先引用官方文档中的一段话:

Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画。Frame动画可以被定义在XML文件中,也可以完全编码实现。

如果被定义在XML文件中,我们可以放置在/res下的anim或drawable目录中(/res/[anim | drawable]/filename.xml),文件名可以作为资源ID在代码中引用;如果由完全由编码实现,我们需要使用到 AnimationDrawable对象。

如果是将动画定义在XML文件中的话,语法如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot=["true" | "false"] >
  4. <item
  5. android:drawable="@[package:]drawable/drawable_resource_name"
  6. android:duration="integer" />
  7. </animation-list>

需要注意的是:

<animation-list>元素是必须的,并且必须要作为根元素,可以包含一或多个<item>元素;android:onshot如果定义为true的话,此动画只会执行一次,如果为false则一直循环。

<item>元素代表一帧动画,android:drawable指定此帧动画所对应的图片资源,android:druation代表此帧持续的时间,整数,单位为毫秒。

文档接下来的示例我就不在解说了,因为接下来我们也要结合自己的实例演示一下这个过程。

我们新建一个名为anim的工程,将四张连续的图片分别命名为f1.png,f2.png,f3.png,f4.png,放于drawable目录,然后新建一个frame.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="false">
  4. <item android:drawable="@drawable/f1" android:duration="300" />
  5. <item android:drawable="@drawable/f2" android:duration="300" />
  6. <item android:drawable="@drawable/f3" android:duration="300" />
  7. <item android:drawable="@drawable/f4" android:duration="300" />
  8. </animation-list>

我们可以将frame.xml文件放置于drawable或anim目录,官方文档上是放到了drawable中了,大家可以根据喜好来放置,放在这两个目录都是可以运行的。

然后介绍一下布局文件res/layout/frame.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <ImageView
  8. android:id="@+id/frame_image"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:layout_weight="1"/>
  12. <Button
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="stopFrame"
  16. android:onClick="stopFrame"/>
  17. <Button
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:text="runFrame"
  21. android:onClick="runFrame"/>
  22. </LinearLayout>

我们定义了一个ImageView作为动画的载体,然后定义了两个按钮,分别是停止和启动动画。

接下来介绍一下如何通过加载动画定义文件来实现动画的效果。我们首先会这样写:

  1. package com.scott.anim;
  2. import android.app.Activity;
  3. import android.graphics.drawable.AnimationDrawable;
  4. import android.graphics.drawable.Drawable;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. public class FrameActivity extends Activity {
  9. private ImageView image;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.frame);
  14. image = (ImageView) findViewById(R.id.frame_image);
  15. image.setBackgroundResource(R.anim.frame);
  16. AnimationDrawable anim = (AnimationDrawable) image.getBackground();
  17. anim.start();
  18. }
  19. }

看 似十分完美,跟官方文档上写的一样,然而当我们运行这个程序时会发现,它只停留在第一帧,并没有出现我们期望的动画,也许你会失望的说一 句:“Why?”,然后你把相应的代码放在一个按钮的点击事件中,动画就顺利执行了,再移回到onCreate中,还是没效果,这个时候估计你会气急败坏 的吼一句:“What the fuck!”。但是,什么原因呢?如何解决呢?

出现这种现象是因为当我们在onCreate中调用AnimationDrawable的start方法时,窗口Window对象还没有完全初始 化,AnimationDrawable不能完全追加到窗口Window对象中,那么该怎么办呢?我们需要把这段代码放在 onWindowFocusChanged方法中,当Activity展示给用户时,onWindowFocusChanged方法就会被调用,我们正是 在这个时候实现我们的动画效果。当然,onWindowFocusChanged是在onCreate之后被调用的,如图:

然后我们需要重写一下代码:

  1. package com.scott.anim;
  2. import android.app.Activity;
  3. import android.graphics.drawable.AnimationDrawable;
  4. import android.graphics.drawable.Drawable;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. public class FrameActivity extends Activity {
  9. private ImageView image;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.frame);
  14. image = (ImageView) findViewById(R.id.frame_image);
  15. }
  16. @Override
  17. public void onWindowFocusChanged(boolean hasFocus) {
  18. super.onWindowFocusChanged(hasFocus);
  19. image.setBackgroundResource(R.anim.frame);
  20. AnimationDrawable anim = (AnimationDrawable) image.getBackground();
  21. anim.start();
  22. }
  23. }

运行一下,动画就可以正常显示了。

如果在有些场合,我们需要用纯代码方式实现一个动画,我们可以这样写:

  1. AnimationDrawable anim = new AnimationDrawable();
  2. for (int i = 1; i <= 4; i++) {
  3. int id = getResources().getIdentifier("f" + i, "drawable", getPackageName());
  4. Drawable drawable = getResources().getDrawable(id);
  5. anim.addFrame(drawable, 300);
  6. }
  7. anim.setOneShot(false);
  8. image.setBackgroundDrawable(anim);
  9. anim.start();

完整的FrameActivity.java代码如下:

    1. package com.scott.anim;
    2. import android.app.Activity;
    3. import android.graphics.drawable.AnimationDrawable;
    4. import android.graphics.drawable.Drawable;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.ImageView;
    8. public class FrameActivity extends Activity {
    9. private ImageView image;
    10. @Override
    11. protected void onCreate(Bundle savedInstanceState) {
    12. super.onCreate(savedInstanceState);
    13. setContentView(R.layout.frame);
    14. image = (ImageView) findViewById(R.id.frame_image);
    15. }
    16. @Override
    17. public void onWindowFocusChanged(boolean hasFocus) {
    18. super.onWindowFocusChanged(hasFocus);
    19. image.setBackgroundResource(R.anim.frame);  //将动画资源文件设置为ImageView的背景
    20. AnimationDrawable anim = (AnimationDrawable) image.getBackground(); //获取ImageView背景,此时已被编译成AnimationDrawable
    21. anim.start();   //开始动画
    22. }
    23. public void stopFrame(View view) {
    24. AnimationDrawable anim = (AnimationDrawable) image.getBackground();
    25. if (anim.isRunning()) { //如果正在运行,就停止
    26. anim.stop();
    27. }
    28. }
    29. public void runFrame(View view) {
    30. //完全编码实现的动画效果
    31. AnimationDrawable anim = new AnimationDrawable();
    32. for (int i = 1; i <= 4; i++) {
    33. //根据资源名称和目录获取R.java中对应的资源ID
    34. int id = getResources().getIdentifier("f" + i, "drawable", getPackageName());
    35. //根据资源ID获取到Drawable对象
    36. Drawable drawable = getResources().getDrawable(id);
    37. //将此帧添加到AnimationDrawable中
    38. anim.addFrame(drawable, 300);
    39. }
    40. anim.setOneShot(false); //设置为loop
    41. image.setBackgroundDrawable(anim);  //将动画设置为ImageView背景
    42. anim.start();   //开始动画
    43. }
    44. }

详解Android动画之Frame Animation的更多相关文章

  1. 详解Android动画之Frame Animation(转)

    在开始实例讲解之前,先引用官方文档中的一段话: Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画.Frame动画可以被定义在XML文件中,也可以完全编码实现. ...

  2. 详解Android动画之Tween Animation

    前面讲了动画中的Frame动画,今天就来详细讲解一下Tween动画的使用. 同样,在开始实例演示之前,先引用官方文档中的一段话: Tween动画是操作某个控件让其展现出旋转.渐变.移动.缩放的这么一种 ...

  3. TranslateAnimation详解 Android动画。

    TranslateAnimation详解 Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, Tr ...

  4. 【Android】详解Android动画

    目录结构: contents structure [+] 补间动画 使用java代码实现Alpha.Rotate.Scale.Translate动画 通过xml文件实现Alpha.Rotate.Sca ...

  5. 【Android】详解Android动画之Interpolator插入器

    Interpolator英文意思是: 篡改者; 分类机; 校对机 SDK对Interpolator的描述是:An interpolator defines the rate of change of ...

  6. [转]ANDROID L——Material Design详解(动画篇)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 转自:http://blog.csdn.net/a396901990/article/de ...

  7. ios学习--详解IPhone动画效果类型及实现方法

    详解IPhone动画效果类型及实现方法是本文要介绍的内容,主要介绍了iphone中动画的实现方法,不多说,我们一起来看内容. 实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一 ...

  8. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  9. css 12-CSS3属性详解:动画详解

    12-CSS3属性详解:动画详解 #前言 本文主要内容: 过渡:transition 2D 转换 transform 3D 转换 transform 动画:animation #过渡:transiti ...

随机推荐

  1. 15 个响应式的 jQuery 图像滑块插件

    设计师和开发人员总是试图使用新技术让网站更智能,而我们发现在许多网站上 jQuery 的图像滑块插件是非常受欢迎的.本文继续介绍 15 个 jQuery 图像滑块插件以供您选择. ELASTISLID ...

  2. 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.

    语法:append lines of itab1 [ from n1 ] [ to n2 ] to itab2. DATA:BEGIN OF gt_00 OCCURS 0,        l_01   ...

  3. IAR ARM、IAR STM8、IAR MSP430共用一个IDE

    转自IAR ARM.IAR STM8.IAR MSP430共用一个IDE 试了安装好多个不同版本不同编译器的IAR,终于明白不同编译器的IAR共用IDE的条件,把几个不同编译器的IAR安装在一起,共用 ...

  4. Android学习及如何利用android来赚钱

    一.如何学习Android      android开发(这里不提platform和底层驱动)你需要对Java有个良好的基础,一般我们用Eclipse作为开发工具.对于过多的具体知识详细介绍我这里不展 ...

  5. NAND Flash的基本操作——读、写、擦除

    基本操作 这里将会简要介绍一下NAND Flash的基本操作在NAND Flash内部是如何进行的,基本操作包括:读.写和擦除.   读:     当我们读取一个存储单元中的数据时(如图2.4),是使 ...

  6. ListView getView中放置多个item和getItemViewType的用法

    ListView 和 Adapter 的基础 工作原理: ListView 针对List中每个item,要求 adapter “给我一个视图” (getView). 一个新的视图被返回并显示 如果我们 ...

  7. 【踩坑记】从HybridApp到ReactNative

    前言 随着移动互联网的兴起,Webapp开始大行其道.大概在15年下半年的时候我接触到了HybridApp.因为当时还没毕业嘛,所以并不清楚自己未来的方向,所以就投入了HybridApp的怀抱. Hy ...

  8. bower初接触

    之前从Steve Sanderson的博文Architecting large Single Page Applications with Knockout.js中学习了用Yeoman创建Knocko ...

  9. [置顶] 【Git入门之一】Git是神马?

    1.Git是神马? 一个开源的分布式版本控制系统,可以有效的高速的控制管理各种从小到大的项目版本.他的作者就是大名鼎鼎的Linux系统创始人Linus. 2.分布式又是神马? 先看看集中式.简单说来, ...

  10. Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释

    本文大部分参考自 http://blog.csdn.net/mayingcai1987/article/details/6200909 ,对原文中的讲解FLAG_NEW_TASK地方加了一些自己的观点 ...