A
A
Andrey Frolov2015-03-31 11:41:24
Android
Andrey Frolov, 2015-03-31 11:41:24

How to catch gestures and recognize the number of fingers touching the screen?

I am writing an application using openGL ES2, in which I need to rotate a figure around its axis, as well as scale it. How to recognize when there is a touch with one finger, and when with two?
So far there is this code:

private float mPreviousX;
    private float mPreviousY;
    private float mDensity;

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (event != null)
        {
            float x = event.getX();
            float y = event.getY();

            if (event.getAction() == MotionEvent.ACTION_MOVE)
            {
                if (mRenderer != null)
                {
                    float deltaX = (x - mPreviousX) / mDensity / 2f;
                    float deltaY = (y - mPreviousY) / mDensity / 2f;
                    mRenderer.mDeltaX += deltaX;
                    mRenderer.mDeltaY += deltaY;
                    //Log.i("System.out", String.valueOf(mRenderer.mDeltaY));
                }
            }

            mPreviousX = x;
            mPreviousY = y;

            mScaleDetector.onTouchEvent(event);

            return true;
        }
        else
        {
            return super.onTouchEvent(event);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2015-03-31
@inham

Better than standard gestures ( https://developer.android.com/training/gestures/in... ). They also work in openGl applications.

D
Dmitry Gavrilenko, 2015-03-31
@Maddox

private int mActivePointerId;
 
public boolean onTouchEvent(MotionEvent event) {
    ....
    // Get the pointer ID
    mActivePointerId = event.getPointerId(0);

    // ... Many touch events later...

    // Use the pointer ID to find the index of the active pointer 
    // and fetch its position
    int pointerIndex = event.findPointerIndex(mActivePointerId);
    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);
}

Read more here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question