其他分享
首页 > 其他分享> > android-GLSurfaceview没有收到onTouch事件

android-GLSurfaceview没有收到onTouch事件

作者:互联网

我已经从位于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文件中有一些花哨的东西或设置它的焦点.任何帮助,将不胜感激.

解决方法:

嗯,这是因为如果您查看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();
    }
}

现在应该可以了.

标签:opengl-es,android
来源: https://codeday.me/bug/20191208/2093474.html