最近在做个小项目的时候,需要用到帧动画。对应着某种状态,该动画可以停止和再次播放。我们知道,通过函数

someAnimObj.start()
someAnimObj.stop()

可以很容易地实现这两种需求,但是如果单纯这样做的话,会出现一个问题:第二次播放的第一帧竟然是上次停止播放时候的最后一帧

搜索了一下,发现这个是Android帧动画的通病。现把解决方法写出来。

android 5.0以上的版本

Drawable drawable = mHeaderImage.getDrawable();
((AnimationDrawable) mHeaderImage.getDrawable()).stop();
mHeaderImage.setImageDrawable(null);
mHeaderImage.setImageDrawable(drawable);

总的来说,就是重新加载了一下动画背景,使得帧动画得以重置。但是在Andorid 5.0以下的版本,好像没效果。不知道具体原因是什么,后来在网上Google到有另一种方法:

android 5.0以下的版本

The API is a bit weird here, because the restart function is inside the setVisible function of AnimationDrawable. If you don't restart and just do another start() on an already finished animation - it will just jump to the last frame. You must reset the animation before starting it.

However if you do setVisible(true,true) your animation will run twice!, so you must dosetVisible(false,true);. This will reset the animation for a start() operation.

ImageView animateHere = (ImageView) findViewById(R.id.animate_here);
animateHere.setImageDrawable(((ImageView)findViewById(R.id.anim_preloaded_imgview).getDrawable());
AnimationDrawable anim = (AnimationDrawable) animateHere.getDrawable();
anim.setVisible(false, true); //reset! see previous section
anim.start(); //now good to start, even if already fired before

 这种方法在5.0以上有没有效果了。所以把这两种方法都用上,就可以兼容这两个版本了。

 

 

Resetting Frame Animation的更多相关文章

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

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

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

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

  3. Android 动画——Frame Animation与Tween Animation

    很多手机应用的引导页都是动画的,添加动画后的应用画面会更加生动灵活,今天博主也学习了Android中Animation的使用,下面来总结下.  android中的Animation分为两种,一种是Fr ...

  4. Frame animation

    [Frame animation] An animation defined in XML that shows a sequence of images in order (like a film) ...

  5. <Android 基础(二十五)> Frame Animation

    简介 Frame Animation, 逐帧动画,通过定义一系列的Drawable对象来实现动画效果,可以用来作为视图的背景. Frame Animation在代码中体现为AnimationDrawa ...

  6. 我的Android进阶之旅------>Android之动画之Frame Animation实例

    ============================首先看看官网上关于Frame animation的介绍================================ 地址:http://de ...

  7. Android基础夯实--重温动画(二)之Frame Animation

    心灵鸡汤:天下事有难易乎,为之,则难者亦易矣:不为,则易者亦难矣. 摘要 当你已经掌握了Tween Animation之后,再来看Frame Animation,你就会顿悟,喔,原来Frame Ani ...

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

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

  9. 详解Android动画之Frame Animation

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

随机推荐

  1. 我对Stub和Mock的理解

    介绍 使用测试驱动开发大半年了,我还是对Stub和Mock的认识比较模糊,没有进行系统整理. 今天查阅了相关资料,觉得写得很不错,所以我试图在博文中对资料进行整理一下,再加上一些自己的观点. 本文是目 ...

  2. Configure Red Hat Enterprise Linux shared disk cluster for SQL Server——RHEL上的“类”SQL Server Cluster功能

    下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) ...

  3. Java反射机制剖析(二)-功能以及举例

    从<java反射机制剖析(一)>的API我们看到了许多接口和类,我们能够通过这些接口做些什么呢? 从上篇API中我们能看到它能够完成下面的这些功能: 1)     获得类 A.     运 ...

  4. Shiny for Interactive Application Development using R(转)

    This slidify-based deck introduces the shiny package from R-Studio and walks one through the develop ...

  5. springboot thymeleaf和shiro标签整合

    这里用的是 thymeleaf 2.x版本的 添加依赖 <dependency> <groupId>com.github.theborakompanioni</group ...

  6. 02-C#(基础)基本的定义和说明

    C#程序或DLL的源码是一组类型的声明 类:类型是一种模板,可以把类型想象成一个用来创建数据结构的模板.模板本身并不是数据结构,但它详细说明了该模板构造的对象的特征. 命名空间:它是一种把相关的类型声 ...

  7. mongoose populate

    mongoose具备关系数据库一样的关联查询,通过在schema模型中设置ref属性,然后在查询时使用populate关键字,可以达到关联查询的目的. 以下内容参考了mongoose官方文档http: ...

  8. python之路 socket,socketsever初探

    socket的英文原义是"孔"或"插座".作为BSD UNIX的进程通信机制,取后一种意思.通常也称作"套接字",用于描述IP地址和端口,是 ...

  9. Java经典编程题50道之八

    求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制. public class Example ...

  10. C# 通过 oledb 操作Excel

    public string GetConnectionString()         {             Dictionary<string, string> props = n ...