Android 逐帧动画isRunning 一直返回true的问题
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 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();
例子很简单,清楚,现在说说遇到的一个问题
最近遇到一个问题,就是设计了一个逐帧动画,但是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);
}
}
@Override
public void unscheduleSelf(Runnable what) {
mCurFrame = -1;
super.unscheduleSelf(what);
}
我们看到调用stop方法时,会间接的将mCurFrame = -1;
现在我们回头想先oneShot的含义是完整的播放一次动画,并不是我们理解的动画播放一次,而是只从start 到stop,才算是完整的一次动画,因此android将stop的方法开放给用户,让用户自行控制oneshot的周期。
所以要解决最开始的问题,一定要手动显式的调用一次stop方法就可以了,具体的,要自己定义一个延时,比如handler或者timer都可以。
Android 逐帧动画isRunning 一直返回true的问题的更多相关文章
- Android 逐帧动画( Drawable 动画),这一篇就够了
前言 作为 Android 最常见的两种动画形式,逐帧动画( Drawable 动画),有着极其广泛的应用,它的原理与早起的电影以及 GIF 类似,就是把一张的图,按顺序快速切换,这样一来看上去就好像 ...
- Android 逐帧动画
原理: 逐帧动画是最简单的一种动画.原理就是把几张图片连续显示出来,以达到动画的效果.就相当于下面这种手绘翻页动画啦~ 实现: 1.需要建立一个animation-list来设置静态图片资源.持续时间 ...
- Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)
就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. androi ...
- Android动画效果之Frame Animation(逐帧动画)
前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...
- Android动画之逐帧动画(FrameAnimation)详解
今天我们就来学习逐帧动画,废话少说直接上效果图如下: 帧动画的实现方式有两种: 一.在res/drawable文件夹下新建animation-list的XML实现帧动画 1.首先在res/drawab ...
- Android简单逐帧动画Frame的实现(三)
android之动画(三)通过AnimationDrawable控制逐帧动画 android与逐帧动画: 效果图: 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态. ...
- 转Android 用Animation-list实现逐帧动画
Android 用Animation-list实现逐帧动画 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/arti ...
- Android 学习之逐帧动画(Frame)
帧动画就是将一些列图片.依次播放. 利用肉眼的"视觉暂留"的原理,给用户的感觉是动画的错觉,逐帧动画的原理和早期的电影原理是一样的. a:须要定义逐帧动画,能够通过代码定义.也能够 ...
- Android中实现一个简单的逐帧动画(附代码下载)
场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...
随机推荐
- javascript中强制类型转换
javascript开发过程中,强制类型转换一般发生在条件判断和==运算符.其他情况,发生的类型转换(与这两种情况也是基本类似,属于万变不离其宗的范畴),暂不讨论. == 双等运算符 考虑代码: a ...
- PHP能得到你是从什么页面过来的,r…
在开发web程序的时候,有时我们需要得到用户是从什么页面连过来的,这就用到了referer. 它是http协议,所以任何能开发web程序的语言都可以实现,比如jsp中是: request.getHea ...
- PHP-popen() 函数打开进程文件指针
待更新 版权声明:本文为博主原创文章,未经博主允许不得转载.
- SQL 中case when then else 用法
SQL如下: SELECT DISTINCTsy_haken_type,sy_sagyo_type,sy_kokyaku_cdFROm tbl_syukeiWHERE (sy_sagyo_ymd be ...
- CI框架篇之模型篇--AR操作(2)
CodeIgniter 和众多的框架一样,有属于自己的一套对数据库的操作方式,本框架更是如此 有属于自己的一套对数据库的安全并且简单的操作, 成为AR操作:下面来对AR操作进行介绍: 首先,确定要启动 ...
- JQuery Datatables(一)
最近项目中用了Bootstrap的样式风格,控件用了JQuery Datatables,主要有几下几点目标: 实现BootStrap风格的table,使用Ajax获取数据,并有勾选项 可以实现全选,单 ...
- hadoop_并行写操作思路
这篇文章是关于,如何修改hadoop的src以实现在client端上传大文件到HDFS的时候, 为了提高上传的效率实现将文件划分成多个块,将块并行的写入到datanode的各个block中 的初步的想 ...
- Oracle 11g 新特性(一)-- 虚拟列
数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Oracle11g 增加了虚拟列的新特性, 具体说明如 ...
- MORE ABORT AWR?
For some time, Oracle’s solution in this area has been its built-in tool, Statspack.Oracle Database ...
- LLDB中的小技巧
1.打印视图层次结构 po [self.view recursiveDescription] 2.临时调整界面UI 比如说现在你需要改变一个控件的背景色来更好的查看布局的问题,这是就不需 ...