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 关注公众号 霸道的程序猿 ...
随机推荐
- WPF 格式化输出- IValueConverter接口的使用
以前在用ASP.NET 做B/S系统时,可以方便地在GRIDVIEW DATAList等数据控件中,使用自定义的代码逻辑,比如 使用 <%# GetBalance(custID) %> 这 ...
- C#教程之打印和打印预览
最近研究一了一下关于PDF打印和打印预览的功能,在此小小的总结记录一下学习过程. 实现打印和打印预览的方法,一般要实现如下的菜单项:打印.打印预览.页面设置. PrintDocument类 Print ...
- php读取操作大文件
在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数,简简单单的几行代码就能 很漂亮的完成我们所需要的功能.但当所操作的文件是一个比较大的 ...
- php递归创建目录
/** * 递归创建目录 * @param [string] $path [创建的目录] * @return [type] [description] */ function mk_Dir($path ...
- 记录Access数据库更新操作大坑一个
对于更新Access数据库的操作,必须保持参数数组与sql语句中参数顺序一致,如下: public bool Update(MyModel model) { StringBuilder strSql ...
- Java SE (1)之 JFrame 组件 BorderLayout 布局
JAVA 初期,练习SE ,桌面程序, package com.sunzhiyan; import java.awt.*; import java.awt.event.*; import javax. ...
- [序列化] SerializeHelper--序列化操作帮助类 (转载)
点击下载 SerializeHelper.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.XML序列化2.Json序列化3.SoapFormatter序列化4.BinaryFormatte ...
- 使用graphics2D给图片上画字符
//读取图片BufferedImage frontImage = ImageIO.read(new File(eCardXMLConfigManager.geteCardXMLConfigManage ...
- Java随机生成定长纯数字或数字字母混合数
(转)Java随机生成定长纯数字或数字字母混合数 运行效果图: 具体实现代码
- ios后台程序持续运行方法
iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到系统的很多限制,这样可以提高电池的使用和用户体验.但是有很多社交类的软件,如果因为它在后台就不能刷新到新的数据的话 ...