您好,我试图暂停动画绘图,但不能成功。在堆栈溢出的帮助下,我至少得到了animationDrawable end listener的帮助,这里是它的代码。有没有什么可能的方法,我可以暂停动画绘图,并从它暂停的地方开始…
public abstract class CustomAnimationDrawable extends AnimationDrawable{
/** Handles the animation callback. */
Handler mAnimationHandler;
Runnable r= new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
onAnimationFinish();
}
};
public CustomAnimationDrawable(AnimationDrawable aniDrawable) {
/* Add each frame to our animation drawable */
for (int i = 0; i < aniDrawable.getNumberOfFrames(); i++) {
this.addFrame(aniDrawable.getFrame(i), aniDrawable.getDuration(i));
}
}
@Override
public void start() {
super.start();
/*
* Call super.start() to call the base class start animation method.
* Then add a handler to call onAnimationFinish() when the total
* duration for the animation has passed
*/
mAnimationHandler = new Handler();
mAnimationHandler.postDelayed(r, getTotalDuration());
}
/**
* Gets the total duration of all frames.
*
* @return The total duration.
*/
public int getTotalDuration() {
int iDuration = 0;
for (int i = 0; i < this.getNumberOfFrames(); i++) {
iDuration += this.getDuration(i);
}
return iDuration;
}
/**
* Called when the animation finishes.
*/
abstract void onAnimationFinish();
public void destroy()
{
mAnimationHandler.removeCallbacksAndMessages(r);
mAnimationHandler.removeCallbacksAndMessages(null);
}}
请帮忙,有什么办法可以让我暂停一下吗?
发布于 2012-11-29 04:14:17
我一直在四处寻找,似乎没有直接做到这一点的方法。在AnimationDrawable中设置帧的所有变量和方法都是私有的。但是,您可以在单击pause时获取动画所在帧的索引,然后通过从播放的最后一帧的索引遍历原始动画来创建新动画(如果动画是周期性的,则重新开始并添加其余帧)。
我从一个基于XML的动画循环开始,然后添加了另一个用于暂停和恢复的循环。这就是我最终要做的事情,到目前为止,它工作得很好,相关变量列在前面。
private ImageView sphere;
private AnimationDrawable sphereAnimation;
private AnimationDrawable sphereResume;
private AnimationDrawable activeAnimation;
private Drawable currentFrame;
private Drawable checkFrame;
private int frameIndex;
private void pause()
{
looping = false;
sphereResume = new AnimationDrawable();
activeAnimation.stop();
currentFrame = activeAnimation.getCurrent();
frameLoop:
for(int i = 0; i < sphereAnimation.getNumberOfFrames(); i++)
{
checkFrame = activeAnimation.getFrame(i);
if(checkFrame == currentFrame)
{
frameIndex = i;
for(int k = frameIndex; k < activeAnimation.getNumberOfFrames(); k++)
{
Drawable frame = activeAnimation.getFrame(k);
sphereResume.addFrame(frame, 50);
}
for(int k = 0; k < frameIndex; k++)
{
Drawable frame = activeAnimation.getFrame(k);
sphereResume.addFrame(frame, 50);
}
activeAnimation = sphereResume;
sphere.setImageDrawable(activeAnimation);
sphere.invalidate();
break frameLoop;
}
}
}
private void play()
{
looping = false;
activeAnimation.setOneShot(true);
activeAnimation.start();
}
private void stop()
{
looping = false;
activeAnimation.stop();
activeAnimation = sphereAnimation;
sphere.setImageDrawable(activeAnimation);
}
private void loop()
{
looping = true;
stopSoundEffect();
activeAnimation.setOneShot(false);
activeAnimation.start();
}发布于 2017-03-03 10:58:18
Zorbert Crenshaw的方法很好,但它很复杂。
我有另一个解决方案(需要用try/catch包围,因为反射是不安全的)。
可以在停止动画之前调用getCurFrameFromAnimationDrawable保存当前帧,也可以调用setCurFrameToAnimationDrawable从任何帧开始。
“30L”应替换为您的持续时间参数。
private int getCurFrameFromAnimationDrawable(AnimationDrawable drawable){
try {
Field mCurFrame = drawable.getClass().getDeclaredField("mCurFrame");
mCurFrame.setAccessible(true);
return mCurFrame.getInt(drawable);
}catch (Exception ex){
Log.e(AnimTool.class.getName(), "getCurFrameFromAnimationDrawable: Exception");
return 0;
}
}
private void setCurFrameToAnimationDrawable(AnimationDrawable drawable, int curFrame){
try {
Field mCurFrame = drawable.getClass().getDeclaredField("mCurFrame");
mCurFrame.setAccessible(true);
mCurFrame.setInt(drawable, curFrame);
Class[] param = new Class[]{Runnable.class, long.class};
Method method = Drawable.class.getDeclaredMethod("scheduleSelf", param);
method.setAccessible(true);
method.invoke(drawable, drawable, 30L);
}catch (Exception ex){
Log.e(AnimTool.class.getName(), "setCurFrameToAnimationDrawable: Exception");
}
}发布于 2019-01-09 14:38:29
这是对这个问题的迟来的回答。但这可能会对另一个有所帮助。要暂停和恢复AnimationDrawable,只需对其进行自定义并仅使用一个变量进行处理。
class CustomAnimationDrawable1() : AnimationDrawable() {
private var finished = false
private var animationFinishListener: AnimationFinishListener? = null
private var isPause = false
private var currentFrame = 0
fun setFinishListener(animationFinishListener: AnimationFinishListener?) {
this.animationFinishListener = animationFinishListener
}
interface AnimationFinishListener {
fun onAnimationFinished()
}
override fun selectDrawable(index: Int): Boolean {
val ret = if (isPause) {
super.selectDrawable(currentFrame)
} else {
super.selectDrawable(index)
}
if (!isPause) {
currentFrame = index
if (index == numberOfFrames - 1) {
if (!finished && ret && !isOneShot) {
finished = true
if (animationFinishListener != null) animationFinishListener!!.onAnimationFinished()
}
}
}
return ret
}
fun pause() {
isPause = true
}
fun resume() {
isPause = false
}}
https://stackoverflow.com/questions/10755416
复制相似问题