AnimationDrawabl主要通过xml实现逐帧动画,SDK实例如下:

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

例子很简单,清楚,现在说说遇到的一个问题

最近遇到一个问题,就是设计了一个逐帧动画,但是oneshot=true,这样的话,当调用AnimationDrawable.start()的方法以后,就只播放一次。

但是我不希望在动画没有结束前,start()被重复调用,因此:

if(!mAnimation.isRunning()){mAnimation.start();
}

我在start方法前加了一个判定条件,就是当前不处于isRunning状态时再播放。

但是问题出现了,当我下次触发,需要调用到start方法是,isRunning始终返回true;

后来我查阅源码,跟网上资料,得出结论如下:

首先看看isRunning()

      /*** <p>Indicates whether the animation is currently running or not.</p>** @return true if the animation is running, false otherwise*/public boolean isRunning() {return mCurFrame > -1;}

这里的mCurFrame是当前播放那一帧,当oneshot=true时,播放完成后,停留在最后一帧,也就是此时的mCurFrame保存的是最后一帧的序号,因此,当我们此时调用isRunning()的时候始终返回为true;

那么如何使得isRunning返回false呢?我们来看看stop方法:

/*** <p>Stops the animation. This method has no effect if the animation is* not running.</p>** @see #isRunning()* @see #start()*/public void stop() {if (isRunning()) {unscheduleSelf(this);}}@Overridepublic void unscheduleSelf(Runnable what) {mCurFrame = -1;super.unscheduleSelf(what);}

我们看到调用stop方法时,会间接的将mCurFrame = -1;

现在我们回头想先oneShot的含义是完整的播放一次动画,并不是我们理解的动画播放一次,而是只从start 到stop,才算是完整的一次动画,因此android将stop的方法开放给用户,让用户自行控制oneshot的周期。

所以要解决最开始的问题,一定要手动显式的调用一次stop方法就可以了,具体的,要自己定义一个延时,比如handler或者timer都可以。

转载于:https://www.cnblogs.com/qgli/p/3311978.html

Android 逐帧动画isRunning 一直返回true的问题相关推荐

  1. Android 逐帧动画(Frame)

    Android 逐帧动画(Frame)  很好理解就是将多张图片放到一个容器里面通过控制这些图片一帧一张图片从而形成动画 使用的使用通过AnimationDrawable 加载放好的图片 然后通过调用 ...

  2. android 创建帧动画,Android 逐帧动画创建实例详解

    Android 逐帧动画创建实例详解 前言: 我们看早期电影的时候,电影通常是一张一张播放,用我们现在专有名词来说,就是一帧帧来,安卓同样有这样动画效果的编排形式. 那么我们先定义逐帧动画xml文件 ...

  3. Android逐帧动画——让图片动起来

    Android逐帧动画--让图片动起来 前言:逐帧动画要求开发者把动画过程的每张静态图片都收集起来,然后由android来控制依次显示这些静态图片,然后利用人眼视觉暂留的原理,给用户造成"动 ...

  4. android 逐帧动画自动播放以及逐帧动画与渐变动画结合的停止问题

    关于逐帧动画的自动播放: android 逐帧动画一般不能直接在onCreat()方法里直接调用.start(),否则只是播放动画的第一帧,可重写onWindowFocusChanged(boolea ...

  5. android语音动画,Android逐帧动画的简单使用-语音播放效果的实现

    逐帧动画(Frame-By-Frame Animation)原理很简单,即快速切换不同的图片,形成动画效果.Android中逐帧动画的实现方式也很简单,下面以语音播放效果为例说明. 1. 首先准备图片 ...

  6. Android逐帧动画和补间动画

    本篇博客来看一下Android中的逐帧动画和补间动画. 一.逐帧动画 逐帧动画也叫Drawable Animation. 在Android中实现逐帧动画,就是由设计师给出一系列状态不断变化的图片, 开 ...

  7. Android -- 逐帧动画

    在处理耗时工作的时候,大多数会弹出一个加载的框,里面有一个连续旋转的图片,很多时候都是用一张图片,使用rotate来设定旋转,不过看起来不太美观,没有形象感,在3.0之前Android有两种动画效果分 ...

  8. android 张口逐帧动画,Android中实现一个简单的逐帧动画(附代码下载)

    场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 实现 首先准备一组不同表情的照片,放在res/drawable下,然后在此目录下新建动画资源文件fairy.xml 这 ...

  9. android 播放gif动画效果,android 通过帧动画方式播放Gif动画

    注意:经过本人测试,这个方法很耗内存, 图片一多就崩了.慎用 <1>用工具(photoshop或者FireWorks)将GIF动画图片分解成多个GIF静态图片,然后保存在res\drawa ...

  10. Android动画——逐帧动画

    1.概述 Android 平台有一套完整的动画框架,在Android3.0之前有两种动画,一种方式是补间动画 Tween Animation.另一种叫逐帧动画 Frame Animation(也称Dr ...

最新文章

  1. 2022-2028年中国手机配件行业发展前景战略及投资风险预测分析报告
  2. A monad tutorial for Clojure programmers (part 3)
  3. 小霸王双核/四核手机最新参数曝光
  4. “不允许对64位应用程序进行修改”的解决方法 --“Changes to 64-bit applications are not allowed.”
  5. 新手入门Web前端,你需要克服这几点困难
  6. python目标检测答案_你好,这里有一份2019年目标检测指南
  7. mysql新增阵列df_DF学Mysql(三)——索引操作
  8. 远程桌面剪贴板失效的解决办法
  9. 微信小程序简单论坛实现demo,供参考。
  10. 安装thrift遇到Error: libcrypto required
  11. Flash遮罩之放大镜
  12. matlab有限元分析杆单元,有限元实验1-杆单元有限元分析
  13. 调平均律依据拍音来判断准确性的理论简析
  14. seo具体怎么优化-优化SEO的方法
  15. 【程序9】题目:要求输出国际象棋棋盘
  16. 【Codeforces】1051F. The Shortest Statement【MST+LCA+最短路】
  17. python复杂网络点图可视化_数据分析:R与Python怎么选?
  18. 微信小程序图片(头像)裁剪工具we-cropper含2d版-完整版
  19. python多边形的绘制教程_绘制最新:python绘制封闭多边形教程_爱安网 LoveAn.com
  20. 20162316刘诚昊 17年10月9日测验“排序课堂测试”

热门文章

  1. win10 Hyper-V 虚拟机 装 boot2docker
  2. Html5 + fromData + Spring MVC 单文件、多文件上传
  3. [SCOI2005]骑士精神
  4. bzoj 1924 所驼门王的宝藏
  5. Spring之AntPathMatcher
  6. uva 11892 - ENimEN(推理)
  7. CentOS 关闭暂不需要的系统服务
  8. Linux下的基本操作
  9. 红帽学习笔记[RHCSA] 第六课[进程、服务相关]
  10. 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 或批处理文件。报错...