Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement OnTouch Event

Tags:

android

I've newly learnt OpenGL and I am developing a Game using OpenGL. I don't know how to implement OnTouch Event into image & move image I use the OpenGL Documentation Example OpenGLES 0.1 but nothig done

like image 677
Rupal Thanki Avatar asked Nov 26 '25 15:11

Rupal Thanki


1 Answers

By default you can handle touch in activity using following code,

 @Override
        public boolean onTouch(View v, MotionEvent event) {

            final float mouseSensitivity = 0.5f;

            if(event.getAction()==MotionEvent.ACTION_DOWN){
                startX = event.getX();
                startY = event.getY();
            } else if(event.getAction()==MotionEvent.ACTION_UP){
                startX = 0.0f;
                startY = 0.0f;
            } else if(event.getAction()==MotionEvent.ACTION_MOVE){
                graphicsRenderer.rotate((startY - event.getY()) * mouseSensitivity,(startX - event.getX()) * mouseSensitivity);
                startX = event.getX();
                startY = event.getY();
            }

            return true;
        }

// Where startX and startY are global int variables;

In order to handle over GLSurface view do like this,

class ClearGLSurfaceView extends GLSurfaceView {
    public ClearGLSurfaceView(Context context) {
        super(context);
        mRenderer = new ClearRenderer();
        setRenderer(mRenderer);
    }

    public boolean onTouchEvent(final MotionEvent event) {
        queueEvent(new Runnable(){
            public void run() {
                mRenderer.setColor(event.getX() / getWidth(),
                        event.getY() / getHeight(), 1.0f);
            }});
            return true;
        }

        ClearRenderer mRenderer;
}
like image 164
Karthi Avatar answered Nov 29 '25 05:11

Karthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!