2个手指在android中滑动

时间:2012-12-10 06:36:17

标签: android onfling

我尝试使用onFling SimpleOnGestureListener方法实施2次手指滑动。但是,我发现SimpleOnGestureListener并不支持两个手指触摸。

我该如何解决?

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

您可以尝试将OnTouchListener与MotionEvent.ACTION_POINTER_DOWN一起使用,如下所示:

 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;
        }
    });

Here是一个教程,解释了如果您需要更多详细信息,该怎么做。我希望它会有所帮助。

答案 2 :(得分:1)

前段时间我做了a library,这使得实现1 /多指滑动非常容易。

您可以找到有关API here的更详细文档 可以找到示例实现here

您要查看的部分是

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);