Answer the question
In order to leave comments, you need to log in
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
Better than standard gestures ( https://developer.android.com/training/gestures/in... ). They also work in openGl applications.
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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question