系统没有在我的应用程序中调用 onTouchEvent。 我尝试在 Activity 、 View 和渲染器中使用 onTouchEvent 来实现它。他们都没有接到电话。 所以我在 Activity 和 View 上尝试了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();
}
最佳答案
onTouch 可以在您想要的任何地方(即,如果它是 Activity 或 View )工作,只要您声明了接口(interface)并将监听器放在正确的位置!另一方面,onTouchEvent仅在 View 内工作
换句话说,onTouch()由View的用户使用来获取触摸事件,而onTouchEvent()由View的派生类使用获取触摸事件
尝试用 onTouch 替换 onTouchEvent..
希望这有帮助
关于java - 无法在 GLSurfaceView 上获取 Android 触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16370929/