簡體   English   中英

Android 多點觸控不工作

[英]Android multi touch not working

我最近一直在嘗試為我的應用程序使用多點觸控,但未能找到解決我的問題的任何幫助,我發布了我遇到問題的代碼。 我正在使用 api 級別為 7 的舊 adk 多點觸控可用於運行 2.1 的機器人嗎?

謝謝

@Override
    public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);


            int action = event.getAction() & MotionEvent.ACTION_MASK;

            switch (action) {
            case MotionEvent.ACTION_DOWN: {
                float tx = event.getX();
                float ty = event.getY();
                if (tx > joystick.getWidth() - joystick.getWidth() && tx < (joystick.getWidth() / 3) && ty > getHeight() - joystick.getHeight()) {
                    left();
                    invalidate();
                }
                if (tx > (joystick.getWidth()/3)*2 && tx < (joystick.getWidth()) && ty > getHeight() - joystick.getHeight()) {
                    right();
                    invalidate();
                }
                if (tx > getWidth() - button.getWidth() - (button.getWidth() / 4)
                        && ty > getHeight() - button.getHeight()
                                - (button.getHeight() / 4)) {
                    jump();
                    invalidate();
                }else{jump_true = 0 ;
                jump_is = true;}
                    break;
            }
            case MotionEvent.ACTION_MOVE: {
                float txm = event.getX();
                float tym = event.getY();
                if (txm > joystick.getWidth() - joystick.getWidth() && txm < (joystick.getWidth() / 3) && tym > getHeight() - joystick.getHeight()) {
                    left();
                    right = false;
                    invalidate();
                }
                if (txm > getWidth() - button.getWidth() - (button.getWidth() / 4)
                        && tym > getHeight() - button.getHeight()
                                - (button.getHeight() / 4)) {
                    //jump();
                    //invalidate();
                }else{
                    //jump_true = 0 ;
                    //jump_is = true;
                }
                if (txm > (joystick.getWidth()/3)*2 && txm < (joystick.getWidth()) && tym > getHeight() - joystick.getHeight()) {
                    right();
                    left = false;
                    invalidate();
                }
                if (txm > getWidth() - button.getWidth() - (button.getWidth() / 4)
                        && tym > getHeight() - button.getHeight()
                                - (button.getHeight() / 4)) {
                    jump();
                    invalidate();
                }else{jump_true = 0 ;
                jump_is = true;}
                    break;
            }

            case MotionEvent.ACTION_POINTER_DOWN: {
                float txp = event.getX();
                float typ = event.getY();
                if (txp > getWidth() - button.getWidth() - (button.getWidth() / 4)
                        && typ > getHeight() - button.getHeight()
                                - (button.getHeight() / 4)) {
                    jump();
                    invalidate();
                }else{jump_true = 0 ;
                jump_is = true;}

                if (txp > joystick.getWidth() - joystick.getWidth() && txp < (joystick.getWidth() / 3) && typ > getHeight() - joystick.getHeight()) {
                    left();
                    right = false;
                    invalidate();
                }
                if (txp > getWidth() - button.getWidth() - (button.getWidth() / 4)
                        && typ > getHeight() - button.getHeight()
                                - (button.getHeight() / 4)) {
                    //jump();
                    //invalidate();
                }else{
                    //jump_true = 0 ;
                    //jump_is = true;
                }
                if (txp > (joystick.getWidth()/3)*2 && txp < (joystick.getWidth()) && typ > getHeight() - joystick.getHeight()) {
                    right();
                    left = false;
                    invalidate();
                }
            break;
            }
            case MotionEvent.ACTION_POINTER_UP: {
                touch_up();
                invalidate();
                    break;
            }
            case MotionEvent.ACTION_UP: {
                touch_up();
                invalidate();
                    break;
            }
            }

            return true;
    }


    private void touch_up() {
        left = false;
        right = false;
        jump_true = 0 ;
        jump_is = true;
    }

    private void left() {
        left = true;
    }
    private void right(){
        right = true;
    }
    private void jump() {
        jump_true=1;
        jump_done = false;
    }

是的,多點觸控適用於 2.0 及更高版本。 請參閱http://androidandme.com/2009/10/news/android-2-0-does-indeed-feature-multitouch/

此外,使用 logcat 確保您的運動事件被觸發。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM