简体   繁体   English

暂停AnimationDrawable并从同一帧继续

[英]Pause AnimationDrawable and resume from the same frame

I am trying to animate a set of images using AnimationDrawable. 我正在尝试使用AnimationDrawable为一组图像设置动画。 The user should have the ability to pause/resume the animation on a click of a button. 用户应该能够通过单击按钮来暂停/恢复动画。 I am doing so, using setVisible(boolean visible, boolean restart) . 我这样做是使用setVisible(boolean visible, boolean restart) Not completely sure if I understand the documentation correctly, but every time pause is pressed, the animation starts from the beginning. 不能完全确定我是否正确理解了文档,但是每次按下暂停时,动画都会从头开始。

Is there any way to resume the animation from the same frame? 有什么办法可以从同一帧恢复动画?

From AnimationDrawable API : AnimationDrawable API中

...If restart is false, the drawable will resume from the most recent frame ...如果重新启动为假,则可绘制对象将从最近的帧恢复

Just to be clear: 只是要清楚:

activityAnimation.setVisible(false, false); Should stop the animation. 应该停止动画。

activityAnimation.setVisible(true, false); Should resume the animation from the same frame (it doesn't). 应该从同一帧恢复动画(不是)。


Here is my code: 这是我的代码:

The animation start() is called in onWindowFocusChanged . 动画start()onWindowFocusChangedonWindowFocusChanged

The animation and the buttons are created in onCreate : 动画和按钮在onCreate中创建:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content);

    ImageView activityImage = (ImageView) findViewById(R.id.activity_image);
    activityImage.setBackgroundResource(R.drawable.anim);
    activityAnimation = (AnimationDrawable) activityImage.getBackground();

    ....
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageButton b = (ImageButton) v;
            if ((Integer)b.getTag() == R.drawable.pause) {
                b.setImageResource(R.drawable.play);
                b.setTag(R.drawable.play);
                activityAnimation.setVisible(false, false);
            } else {
                b.setImageResource(R.drawable.pause);
                b.setTag(R.drawable.pause);
                activityAnimation.setVisible(true, false);
            }
        }
    });...
}

This is the content of the XML file 这是XML文件的内容

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item android:drawable="@drawable/one" android:duration="1000" />
    <item android:drawable="@drawable/two" android:duration="1000" />
    <item android:drawable="@drawable/three" android:duration="1000" />
    <item android:drawable="@drawable/four" android:duration="1000" />

</animation-list>

I have noticed several of these old posts, but non had the answer. 我注意到其中一些旧帖子,但没有答案。

How to reset AnimationDrawable (The other side of this issue) 如何重置AnimationDrawable (此问题的另一面)

How do I pause frame animation using AnimationDrawable? 如何使用AnimationDrawable暂停帧动画? [Closed] [关闭]

Since I needed a timer on the screen as well, the answer in my case was simply to create a runnable replace the ImageView background every second. 由于我还需要在屏幕上放置一个计时器,因此,我的回答很简单,就是每秒创建一个可运行的替换ImageView背景。

Here is the general idea: 这是一般的想法:

  1. Create the runnable, ImageView and few helpers: 创建可运行的ImageView和一些帮助程序:

     ImageView exImage = null; long startTime = 0; long pauseTime = 0; long pauseStartTime = 0; List<Integer> animList = new ArrayList<>(); int animIt = 0; Handler timerHandler = new Handler(); Runnable timerRunnable = new Runnable() { @Override public void run() { long millis = System.currentTimeMillis() - startTime - pauseTime; double dSecs = (double) (millis / 100); int pace = 10; if (dSecs % pace == 0.0 && !animList.isEmpty()) { animIt = (animIt == animList.size() - 1) ? 0 : animIt + 1; exImage.setBackgroundResource(animList.get(animIt)); } timerHandler.postDelayed(this, 100); } }; 
  2. In OnCreate bind the pause and play buttons, fire the timer and set the first ImageView OnCreate绑定暂停和播放按钮,触发计时器并设置第一个ImageView

     @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exercise); // Set the image view exImage = (ImageView) findViewById(R.id.image_zero); exImage.setBackgroundResource(R.drawable.fe_0); // Start the timer timerHandler.postDelayed(timerRunnable, 0); // Bind the buttons ImageButton pp = (ImageButton) findViewById(R.id.button_play_pause_toggle); pp.setImageResource(R.drawable.pause); pp.setTag(R.drawable.pause); pp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageButton pp = (ImageButton) v; //Pause if ((Integer) pp.getTag() == R.drawable.pause) { pauseStartTime = System.currentTimeMillis(); timerHandler.removeCallbacks(timerRunnable); } else { // Resume pauseTime += System.currentTimeMillis() - pauseStartTime; timerHandler.postDelayed(timerRunnable, 0); } } }); } 

To pause and resume AnimationDrawable, simply customize it and handle with only one variable. 要暂停和恢复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
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM