我如何等待AnimationDrawable继续code之前结束? [英] How do I wait for AnimationDrawable to end before continuing code?

查看:107
本文介绍了我如何等待AnimationDrawable继续code之前结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个AnimationDrawable在屏幕上移动一个字符一个空格。动画工作正常,但我需要从刷新屏幕搪塞了GUI的其余部分。基本上在地图移动时,游戏中有刷新屏幕更新新的位置。在code的重绘是游戏类的内部,而动画是在显示的实际游戏板上的gridview的图像适配器。这里是code两行是麻烦了。

so I have an AnimationDrawable to move a character one space on the screen. The animation works fine, but I need to stall out the rest of the GUI from refreshing the screen. Basically when the map moves, the game has to refresh the screen to update the new location. The code for the redraw is inside of the game class, and the animation is in the image adapter for the gridview that displays the actual game board. Here's the two lines of code that are trouble.

    playerAdapter.animateLeft();
    redraw();

第一行调用它有两个动画需要发生的,都接受700毫秒适配器。然后,重新绘制更新主板本身。问题是,重绘不等待动画。我使用的睡眠之类的东西尝试,但只是暂停整个该死的事情,没有任何反应。我想我需要使用线程或处理程序,但我不能确定如何工作的。有任何想法吗?所有的帮助是非常AP preciated:)

The first line calls the adapter which has the two animations that need to happen, both take 700ms. Then, redraw updates the board itself. The problem is, redraw doesn't wait for the animations. I've tried using sleep and stuff like that but that just pauses the whole damn thing and nothing happens. I think I need to use Threads or Handlers but I'm unsure how that works. Any ideas? All help is much appreciated :)

推荐答案

尝试:

YourAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) { }

    @Override
    public void onAnimationEnd(Animation animation) {
        //Ended
    }

    @Override
    public void onAnimationRepeat(Animation animation) { }
});

编辑:哎呀。没有读过绘制的一部分。下面是我用它来检查是否所有的帧都在一个递归函数:

Oops. Didn't read the drawable part. Here's a recursive function I use to check if all frames are over:

 private void checkIfAnimationDone(AnimationDrawable anim){
        final AnimationDrawable a = anim;
        int timeBetweenChecks = 300;
        Handler h = new Handler();
        h.postDelayed(new Runnable(){
            public void run(){
                if (a.getCurrent() != a.getFrame(a.getNumberOfFrames() - 1)){
                    checkIfAnimationDone(a);
                } else{
                    Toast.makeText(getApplicationContext(), "ANIMATION DONE!", Toast.LENGTH_SHORT).show();
                }
            }
        }, timeBetweenChecks);
    };

您也可以登录的时候,当你启动动画,持续时间(你的前手知道)添加到它获得的结束时间。然而,这可能是不准确的,如设置和启动动画需要一点时间也是如此。

You could also log the time when you start the animation, and add the duration (which you know before hand) to it to get the ending time. However, this can be inaccurate, as setting and starting the animation takes a bit of time as well.

这篇关于我如何等待AnimationDrawable继续code之前结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆