GLSurfaceview没有获得onTouch事件

时间:2011-02-17 03:54:29

标签: android opengl-es

我已经关注了位于here的Google的onTouch示例。但是,我的日志中没有任何结果。据我所知,我的观点是没有接受任何触摸事件。这是我的代码:

package com.test;

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.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;

public class intro extends Activity {
    static{
        System.loadLibrary("graphrender");
    }
    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mGLView = new GraphGLSurfaceView(this);
        setContentView(mGLView);

    }

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

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

class GraphGLSurfaceView extends GLSurfaceView {
    GraphRenderer mRenderer;    
    public GraphGLSurfaceView(Context context) {
        super(context);
        mRenderer = new GraphRenderer();
        setRenderer(mRenderer);

    }
    public boolean onTouch(View v, MotionEvent event)
    {
        queueEvent(new Runnable(){
            public void run() {
                mRenderer.shout();
            }});
            return true;
    }
}

class GraphRenderer implements GLSurfaceView.Renderer { 
    private static native void nativeSetup();
    private static native void nativeSize(int w, int h);
    private static native void nativeRender();


    private float _red = 0.9f;
    private float _green = 0.2f;
    private float _blue = 0.2f;

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        nativeSetup();
        Log.d("intro", "Got to intro 4" );
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        nativeSize(w,h);
    }

    public void onDrawFrame(GL10 gl) {
        nativeRender();
    }
    public void shout()
    {
        Log.d("Graph Page", "gotta graph");
    }
}

我唯一能想到的是,我必须在xml文件中有一些奇特的东西或设置它的焦点。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

那么这是因为如果您查看GLSurfaceView的API,它实际上没有onTouch方法可供您覆盖。这意味着永远不会调用onTouch方法,这可能解释了为什么你从未看到它被运行。相反,如果您想获取触摸事件,则应该覆盖Activity中的onTouchEvent。像这样:

public class Intro extends Activity {
    static{
        System.loadLibrary("graphrender");
    }
    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mGLView = new GraphGLSurfaceView(this);
        setContentView(mGLView);

    }

    @Override
    protected boolean onTouchEvent(MotionEvent event) {
        Log.i("motion", event);
        return true;
    }

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

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

现在应该可以了。