android的多点触控问题-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

android的多点触控问题

蛮大人123 2016-03-12 12:01:28 1373
relativeLayout.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int pointerCount = event.getPointerCount(); 
                int pointerId = 0;                 
                int action = event.getAction();
                if (pointerCount>1) {
                    Toast.makeText(FlymouseTouch.this, "滑动模式", Toast.LENGTH_SHORT).show();
                    int moreaction = event.getActionMasked();
                    switch (moreaction) {
                    case MotionEvent.ACTION_POINTER_DOWN:
                        x1 = event.getX(1);
                        y1 = event.getY(1);
                        x2 = event.getX(2);
                        y2 = event.getY(2);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mx1 = event.getX(1);
                        my1 = event.getY(1);
                        mx2 = event.getX(2);
                        my2 = event.getY(2);
                        float cx1 = mx1-x1;
                        float cy1 = my1-y1;
                        float cx2 = mx2-x2;
                        float cy2 = my2-y2;
                        NetUtils.SendTouch("appkey", MotionEvent.ACTION_MOVE, cx2, cy2, 6);
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        float supx = event.getX(2);
                        float supy = event.getY(2);
                        NetUtils.SendTouch("appkey", MotionEvent.ACTION_POINTER_2_UP, supx, supy, 6);
                        break;

                    }

这是代码 当我运行的时候第二个手指一放到屏幕上 程序就奔溃了 报错信息如下:

04-11 16:57:43.938: E/InputEventReceiver(7604): Exception dispatching input event.
04-11 16:57:43.939: E/MessageQueue-JNI(7604): Exception in MessageQueue callback: handleReceiveCallback
04-11 16:57:43.943: E/MessageQueue-JNI(7604): java.lang.IllegalArgumentException: pointerIndex out of range
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.MotionEvent.getX(MotionEvent.java:1981)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at com.example.shoubing.FlymouseTouch$2.onTouch(FlymouseTouch.java:108)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.View.dispatchTouchEvent(View.java:7305)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2253)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1950)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1970)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1918)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.View.dispatchPointerEvent(View.java:7506)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3654)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3579)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4849)
04-11 16:57:43.943: E/MessageQueue-JNI(7604): at 

这是什么情况?

云服务器登录 云服务器设置
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:00:39

    onTouch()方法中的参数MontionEvent event默认只是单点触控。你的switch的条件也不对,应该这样写

    switch (event.getAction() & MotionEvent.ACTION_MASK){
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_POINTER_UP:
    }
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题
推荐课程