12 Mar

Android OpenGL Touch

Cara mendeteksi Event Touch di dalam OpenGL Android adalah sebagai berikut:

Pertama, kita buat dulu class Activitynya (Main.java)

public class Main extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TaruView view = new TaruView(this);
        setContentView(view);
    }
}

Kedua, kita buat dulu class Renderernya (TaruRenderer.java)

public class TaruRenderer implements GLSurfaceView.Renderer {
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Do nothing
    }
 
    @Override
    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);
    }
 
    @Override
    public void onDrawFrame(GL10 gl) {        
        gl.glClearColor(_red, _green, _blue, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    }
}

Ketiga, kita buat class View (TaruView.java)

public class TaruView extends GLSurfaceView {
    private TaruRenderer tr;
 
    public TaruView(Context context) {
        super(context);
        tr = new TaruRenderer();
        setRenderer(tr);
    }

    // letakkan event on touch di sini
    public boolean onTouchEvent(final MotionEvent event) {
        queueEvent(new Runnable() {
            public void run() {
                  // event ketika di touch
            }
        });
        return true;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *