2

系统没有在我的应用程序中调用 onTouchEvent。我已经尝试在活动、视图和渲染器中使用 onTouchEvent 实现它。他们都没有被叫到。所以我在活动和视图上尝试了 dispatchTouchEvent,仍然没有被调用。它基于我编译的 San Angeles NDK 示例,它可以正确检测触摸。

下面是完整的 java 源代码,其他都是原生 c++。


package com.example.SanAngeles;
    import javax.microedition.khronos.egl.EGLConfig;
    import javax.microedition.khronos.opengles.GL10;

    import android.app.Activity;
    import android.content.Context;
    import android.opengl.GLSurfaceView;
    import android.os.Bundle;
    import android.view.MotionEvent;

    public class DemoActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mGLView = new DemoGLSurfaceView(this);
            setContentView(mGLView);
        }

        @Override
        protected void onPause() {
            super.onPause();
            mGLView.onPause();
        }

        @Override
        protected void onResume() {
            super.onResume();
            mGLView.onResume();
        }

        private DemoGLSurfaceView mGLView;

        static {
            System.loadLibrary("Android");
        }
    }

    class DemoGLSurfaceView extends GLSurfaceView {
        public DemoGLSurfaceView(Context context) {
            super(context);
            mRenderer = new DemoRenderer();
            mRenderer.context = context;
            setRenderer(mRenderer);
        }
        @Override
        public boolean onTouchEvent(final MotionEvent event) {
            System.out.println("onTouchEvent");
            System.out.println("Motion Event" + event.getAction());
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                System.out.println("ACTION DOWN");
                final int pointerCount = event.getPointerCount();
                System.out.println("down pcount:" + pointerCount);
                for(int i = 0; i < pointerCount; i++)
                {
                    System.out.println("Touch x:" + event.getX(i) + "y:" + event.getY(i));
                    int id = event.getPointerId(i);
                    nativeTouchDown(id, (int)event.getX(i), (int)event.getY(i));
                }
            }
            if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                final int pointerCount = event.getPointerCount();
            System.out.println("up pcount:" + pointerCount);

                for(int i = 0; i < pointerCount; i++)
                {
                    int id = event.getPointerId(i);
                    nativeTouchUp(id);
                }
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                final int pointerCount = event.getPointerCount();
            System.out.println("move pcount:" + pointerCount);

                for(int i = 0; i < pointerCount; i++)
                {
                    int id = event.getPointerId(i);
                    nativeTouchMove(id, (int)event.getX(i), (int)event.getY(i));
                }
            }
            return true;
        }

        DemoRenderer mRenderer;

        private static native void nativePause();
    }

    class DemoRenderer implements GLSurfaceView.Renderer {
        Context context;
        Object am;
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            am = (Object)context.getAssets();
            System.out.println("HDROP:\n AssetManager:" + am);
            nativeInit(am, 500,500);
        }

        public void onSurfaceChanged(GL10 gl, int w, int h) {
            //gl.glViewport(0, 0, w, h);
            System.out.println("Hdrop  surface changed");
            nativeResize(w, h);
        }

        public void onDrawFrame(GL10 gl) {
            nativeRender();
        }

        private static native void nativeInit(Object assetManager, int w, int h);
        private static native void nativeTouchDown(int id, int x, int y);
        private static native void nativeTouchUp(int id);
        private static native void nativeTouchMove(int id, int x, int y);
        private static native void nativeResize(int w, int h);
        private static native void nativeRender();
        private static native void nativeDone();
    }
4

1 回答 1

2

onTouch works everywhere you want (i.e if it's activity or view) as long as you have declared the interface and put the Listener right! On the other hand onTouchEvent is working only inside a View

In other words, onTouch() is used by users of the View to get touch events while onTouchEvent() is used by derived classes of the View to get touch events

Try, replacing onTouchEvent with onTouch..

Hope this helps

于 2013-05-04T05:50:55.983 回答