gpt4 book ai didi

java - 无法在 GLSurfaceView 上获取 android 触摸事件

转载 作者:行者123 更新时间:2023-11-29 21:43:57 24 4
gpt4 key购买 nike

系统没有在我的应用程序中调用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com