简体   繁体   English

MotionEvent-Java中的action_move(监听action_up)

[英]MotionEvent - action_move (listening for action_up) in java

Let's say that I have a motion event, that when I hold a key my cannon shoots balls. 假设我有一个动作事件,当我按住钥匙时,我的大炮会发射球。 Everything is okay but I needed to create a while() loop for this inside this event, cause balls were lagging. 一切都很好,但是我需要在事件内部为此创建一个while()循环,这会导致球滞后。

The point is here I can't escape this event. 关键是我无法逃脱此事件。 The while loop is infinite and I can't listen for ACTION_UP. while循环是无限的,我无法收听ACTION_UP。 Is there any way to stop this on ACTION_UP while being in this loop? 在此循环中,有什么方法可以在ACTION_UP上停止此操作?

EDIT: part of the code: 编辑:部分代码:

@Override
public void surfaceCreated(SurfaceHolder holder) {

    game = new Game(holder, resources);
    game.start();
    shootingThread = new Thread(new Runnable() {
        @Override
        public void run(){
            while(running) {
                int size = game.gameLoop.balls.size();
                    if (size == 0) {
                        game.gameLoop.balls.add(new Ball(metrics.widthPixels, metrics.heightPixels, touched_x, touched_y, game.gameLoop.ball_bmp_width, metrics.heightPixels - game.gameLoop.ball_bmp_width / 2));
                    } else if (size > 0 && game.gameLoop.balls.get(size - 1).image_center_y < metrics.heightPixels - game.gameLoop.ball_bmp_width - 50)
                        game.gameLoop.balls.add(new Ball(metrics.widthPixels, metrics.heightPixels, touched_x, touched_y, game.gameLoop.ball_bmp_width, metrics.heightPixels - game.gameLoop.ball_bmp_width / 2));
                }
            // }
        }
    });
    shootingThread.start();

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    touched_x = event.getX();
    touched_y = event.getY();

    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        startShootTime = new Date().getTime();
        running = true;
        shootingThread.run();
        Log.i("", "\n\ndown\n\n");

    } else if (action == MotionEvent.ACTION_MOVE ) {
        touched_x = event.getX();
       touched_y = event.getY();


    } else if (action == MotionEvent.ACTION_UP) {

        Log.i("", "\n\nup\n\n");
        running = false;
    }

    return true;
}

The onTouchEvent method fires events as they come in and should not be blocked. onTouchEvent方法会在事件进入时触发事件,因此不应将其阻止。 If you want to continuously trigger an action while the user's fingers are down then once you get the ACTION_DOWN event you should kick off a thread (or async task or timer or any other form of action on another thread) which should then add the balls to your game loop. 如果要在用户的手指按下时连续触发动作,则一旦获得ACTION_DOWN事件,就应该启动一个线程(或异步任务或计时器或其他线程上的任何其他形式的动作),然后将球添加到您的游戏循环。 Then in your main thread you'll need to determine if there were changes on the UI and invalidate/draw your updated views. 然后,在主线程中,您需要确定UI上是否有更改,并使无效的视图/绘制更新的视图。

However, ACTION_MOVE will get fired a lot while the user has their finger down. 但是,当用户将手指放下时,ACTION_MOVE将被触发很多次。 So you'll have to determine for yourself whether that is fast enough or if you need to move that logic to another thread. 因此,您必须自己确定这是否足够快,还是需要将该逻辑移至另一个线程。

Note that onTouchEvent will fire as fast as you can consume it, so if you run into performance problems it is actually ok to put a small sleep here. 请注意,onTouchEvent将以您可以消耗的最快的速度触发,因此,如果遇到性能问题,可以在这里稍作睡眠。 You can see documentation of that recommendation here 您可以在此处查看该建议的文档

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

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