在Android的2指轻扫 [英] 2 finger swipe in android
问题描述
我试着用 SimpleOnGestureListener
的 onFling
实施方法2指轻扫。然而,我发现, SimpleOnGestureListener
不支持两个手指触摸。
I tried implementing 2 finger swipe using onFling
method of SimpleOnGestureListener
. However,I found that SimpleOnGestureListener
doesn't support two finger touch.
我怎样才能做得出来?
推荐答案
我的解决方案的工作原理,但它很丑陋使用公共静态INT来算多少的手指有。我不得不回fingercount设置为一后,我用两个手指flinged因为我couldnot获得action_pointer_down一个手指了......我不知道为什么......但这种解决方案也将工作更多的手指......希望有人可以使用它在特征
My Solution works, but its quite ugly to use a public static int to count how many fingers there are. i had to set fingercount back to one after i flinged with two fingers because i couldnot get the action_pointer_down for one finger anymore... i dont know why... but this solution would also work for more fingers... hope someone can use it in the feature
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;
}
这篇关于在Android的2指轻扫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!