gpt4 book ai didi

android - AnimationDrawable暂停?

转载 作者:行者123 更新时间:2023-11-29 14:40:50 24 4
gpt4 key购买 nike

您好,我试图暂停动画可绘制但未能成功。在堆栈溢出的帮助下,我至少得到了 animationDrawable 结束监听器的帮助,这里是它的代码。有什么方法可以暂停动画 Drawable 并从它暂停的地方启动它 ...

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);


}

请帮忙,有什么办法可以暂停吗?

最佳答案

看了一圈,好像没有办法直接搞定。 AnimationDrawable 中用于设置框架的所有变量和方法都是私有(private)的。但是,您可以在点击暂停时获取动画所在帧的索引,然后通过从播放的最后一帧的索引遍历原始动画来创建新动画(然后重新开始并添加其余的动画是周期性的)。

我从一个基于 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();
}

关于android - AnimationDrawable暂停?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10755416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com