我尝试使用SimpleOnGestureListener
中的onFling
方法实现双指滑动。但是,我发现SimpleOnGestureListener
不支持两个手指触摸。
我该怎么解决?
我的解决方案可行,但使用公共静态整数来计算手指数量有点丑陋。我不得不在使用两个手指后将 fingercount 重置为一个,因为我无法再获取一个手指的 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;
}
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;
}
});
这里有一个教程,解释了如何做到这一点。
我之前做了一个图书馆,使得实现单指/多指滑动非常容易。
你可以在这里找到更详细的API文档。 示例实现可以在这里找到。
你想要查看的部分是
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);