3

我尝试onFling使用SimpleOnGestureListener. 但是,我发现它SimpleOnGestureListener不支持两指触摸。

我该如何解决?

4

3 回答 3

2

我的解决方案有效,但使用公共静态 int 来计算有多少手指非常难看。在我用两根手指弹起后,我不得不将手指计数设置回一,因为我无法再为一根手指获得 action_pointer_down ......我不知道为什么......但是这个解决方案也适用于更多手指......希望有人可以使用它在功能中

public class RemoteFragment extends Fragment{

public static int fingercount = 1;    

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final GestureDetector gdt = new GestureDetector(getActivity(),
            new RemoteGestureListener());

    View view = inflater.inflate(R.layout.gestures, container, false);

    view.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            int action = event.getAction();

            switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.d(TAG,
                        "Pointer-Count ="
                                + String.valueOf(fingerCount = event
                                        .getPointerCount()));

                break;
            }
            gdt.onTouchEvent(event);
            return true;
        }
    });
    return view;
}
}

public class RemoteGestureListener implements OnGestureListenerr {

private final static String TAG = "RemoteGestureListener";

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;


public RemoteGestureListener() {
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {

    Log.d(TAG, "onFling");
    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        if (RemoteFragment.fingerCount == 1) {
            Log.d(TAG, "Left Swipe");
        } else {
            Log.d(TAG, "Left xFinger Swipe");
            RemoteFragment.fingerCount = 1;
        }
        return true; // Right to left
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        if (RemoteFragment.fingerCount == 1) {
            Log.d(TAG, "Right Swipe");
        } else {
            Log.d(TAG, "Right xFinger Swipe");
            RemoteFragment.fingerCount = 1;
        }
        return true; // Left to right
    }

    if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
        if (RemoteFragment.fingerCount == 1) {
            Log.d(TAG, "Top Swipe");
        } else {
            Log.d(TAG, "Top xFinger Swipe");
            RemoteFragment.fingerCount = 1;
        }
        return true; // Bottom to top
    } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
            && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
        if (RemoteFragment.fingerCount == 1) {
            Log.d(TAG, "Bottom Swipe");
        } else {
            Log.d(TAG, "Bottom xFinger Swipe");
            RemoteFragment.fingerCount = 1;
        }
        return true;
    }
    return true;
}
于 2013-12-12T08:11:37.757 回答
1

I had made a library some time ago that makes implementing 1/multi finger swipes very easy.

You can find more detailed documentation of the API here A sample implementation can be found here

The part that you want to look at is

ImageView mv = (ImageView) findViewById(R.id.myview);
final TextView grtv = (TextView) findViewById(R.id.gestureResultTextView);
SimpleFingerGestures.DEBUG = true;
SimpleFingerGestures.CONSUME_TOUCH_EVENTS = true;
SimpleFingerGestures sfg = new SimpleFingerGestures();
sfg.setOnFingerGestureListener(new SimpleFingerGestures.OnFingerGestureListener() {
@Override
public boolean onSwipeUp(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " up");
return false;
}
@Override
public boolean onSwipeDown(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " down");
return false;
}
@Override
public boolean onSwipeLeft(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " left");
return false;
}
@Override
public boolean onSwipeRight(int fingers, long gestureDuration) {
grtv.setText("swiped " + fingers + " right");
return false;
}
@Override
public boolean onPinch(int fingers, long gestureDuration) {
grtv.setText("pinch");
return false;
}
@Override
public boolean onUnpinch(int fingers, long gestureDuration) {
grtv.setText("unpinch");
return false;
}
});
mv.setOnTouchListener(sfg);
于 2014-10-28T23:05:22.483 回答
1

您可以尝试MotionEvent.ACTION_POINTER_DOWN像这样使用 OnTouchListener:

 button.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getAction();

            switch(action & MotionEvent.ACTION_MASK)
            {
                case MotionEvent.ACTION_POINTER_DOWN:
                    Toast.makeText(MyActivity.this, " Two Fingers Tapped Once. Yeeeyy :)", 0).show();

                    // set the mTwoFingersTapped flag to TRUE when we tap with 2 fingers at once
                    mTwoFingersTapped = true;
                    break;
            }
            return false;
        }
    });

是一个教程,它解释了如何做到这一点。

于 2012-12-10T06:41:30.210 回答