停止移动球按钮单击

时间:2013-06-28 06:11:24

标签: android

我有一个从一开始就一直在移动的球。我想在点击按钮时停止球。当我点击按钮时它会显示Toast,但是球继续移动。它不会停止。

请指导我如何在点击按钮时停止球。我的活动代码就在那里。

@SuppressLint("HandlerLeak")
public class BounceActivity extends Activity {

    private static final int GAME_START = 500;
    private static final int GAME_STOP = 600;

    Thread myRefreshThread = null;
    BounceView myBounceView = null;
    int width = 0;
    int height = 0;

    Handler myGUIUpdateHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
            case BounceActivity.GAME_START:
                myBounceView.invalidate();
                break;
            case BounceActivity.GAME_STOP:
                Log.d("BounceView", "Game state: " + BounceView.game_state);
                Toast.makeText(BounceActivity.this, "Game stopped", Toast.LENGTH_SHORT).show();
                myRefreshThread = null;
                break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        this.myBounceView = new BounceView(this, dm.widthPixels, dm.heightPixels);
        this.setContentView(myBounceView);
        myRefreshThread = new Thread(new RefreshRunner());
        myRefreshThread.start();
    }

    class RefreshRunner implements Runnable {

        @Override
        public void run() {
            while (myRefreshThread != null) {
                if(BounceView.game_state == 0) {
                    Message msg = Message.obtain();
                    msg.what = BounceActivity.GAME_START;
                    myGUIUpdateHandler.sendMessage(msg);
                } else if (BounceView.game_state == 1) {
                    Message msg = Message.obtain();
                    msg.what = BounceActivity.GAME_STOP;
                    myGUIUpdateHandler.sendMessage(msg);

                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }

    }}

1 个答案:

答案 0 :(得分:0)

您正在不同的线程上访问和设置myRefreshThread,这可能会有问题。

不是在while循环中检查myRefreshThread是否为null,而是可以向RefreshRunner添加“running”标志,并将stop方法设置为false。 然后在while循环中检查该标志。

保持对RefreshRunner的引用(传递给myRefreshThread的引用),并在希望动画停止时调用stop()。

class RefreshRunner implements Runnable {

    private void mRunning = true;

    public void stop () {
          mRunning = false;
    }

    @Override
    public void run() {
        while (mRunning) {
            if(BounceView.game_state == 0) {
                Message msg = Message.obtain();
                msg.what = BounceActivity.GAME_START;
                myGUIUpdateHandler.sendMessage(msg);
            } else if (BounceView.game_state == 1) {
                Message msg = Message.obtain();
                msg.what = BounceActivity.GAME_STOP;
                myGUIUpdateHandler.sendMessage(msg);

            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

}}