在视图上添加多个手势,不起作用
Adding multiple gestures on a view, not working
我正在尝试将多个手势添加到一个视图中。我已经成功地让它们分别工作,但是当我组合时,只有一个最终工作,我做错了什么?
我要添加的第一个手势是 Scale Gesture
:
public class MyScaleGestures implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
private View view;
private ScaleGestureDetector gestureScale;
private float scaleFactor = 1;
private boolean inScale = false;
public MyScaleGestures (Context c){ gestureScale = new ScaleGestureDetector(c, this); }
@Override
public boolean onTouch(View view, MotionEvent event) {
this.view = view;
gestureScale.onTouchEvent(event);
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
Log.d(TAG, "onScale: " + scaleFactor);
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) { inScale = false; }
}
我的第二个手势是 SimpleOnGestureListener
:
private class FlingGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
}
合二为一class:
public class OnSwipeTouchListener implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
private final GestureDetector gestureDetector;
private ScaleGestureDetector gestureScale;
private OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new FlingGestureDetector());
gestureScale = new ScaleGestureDetector(context, this);
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
gestureScale.onTouchEvent(event);
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(TAG, "onScale: " + detector.getScaleFactor());
return false;
}
并使用它:
sceneView.setOnTouchListener(new OnSwipeTouchListener(this));
将它们组合后,第一个手势只有一个有效 (gestureDetector)
。我怎样才能让两者都听取意见?
组合它们时,在 gestureScale
中执行 pinch
操作时永远不会调用 onScale
方法。但是当我不包括其他手势时它会单独工作。
您需要将所有手势都集中在一个 OnTouchListener
class 上并实现您的逻辑。
在下面的示例中,我遗漏了所有参数。并且你应该根据你的要求提供所有这些。
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
class MyGesture implements View.OnTouchListener {
private View view;
private MyDetector myDetector;
private ScaleGestureDetector scaleDetector;
private GestureDetector gestureDetector;
public MyGesture(View view, MyDetector myDetector) {
this.view = view;
this.myDetector = myDetector;
initialGestures();
}
private void initialGestures() {
scaleDetector = new ScaleGestureDetector(
view.getContext(),
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
myDetector.onScale();
return true;
}
});
gestureDetector = new GestureDetector(
view.getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
myDetector.onFling();
return false;
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
scaleDetector.onTouchEvent(event);
return true;
}
interface MyDetector {
void onScale();
void onFling();
}
}
并使用它:
myView.setOnTouchListener(new MyGesture(myView, new MyGesture.MyDetector() {
@Override
public void onScale() {
// do sth
}
@Override
public void onFling() {
// do sth
}
}));
我正在尝试将多个手势添加到一个视图中。我已经成功地让它们分别工作,但是当我组合时,只有一个最终工作,我做错了什么?
我要添加的第一个手势是 Scale Gesture
:
public class MyScaleGestures implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
private View view;
private ScaleGestureDetector gestureScale;
private float scaleFactor = 1;
private boolean inScale = false;
public MyScaleGestures (Context c){ gestureScale = new ScaleGestureDetector(c, this); }
@Override
public boolean onTouch(View view, MotionEvent event) {
this.view = view;
gestureScale.onTouchEvent(event);
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
Log.d(TAG, "onScale: " + scaleFactor);
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) { inScale = false; }
}
我的第二个手势是 SimpleOnGestureListener
:
private class FlingGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
}
合二为一class:
public class OnSwipeTouchListener implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
private final GestureDetector gestureDetector;
private ScaleGestureDetector gestureScale;
private OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new FlingGestureDetector());
gestureScale = new ScaleGestureDetector(context, this);
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
gestureScale.onTouchEvent(event);
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(TAG, "onScale: " + detector.getScaleFactor());
return false;
}
并使用它:
sceneView.setOnTouchListener(new OnSwipeTouchListener(this));
将它们组合后,第一个手势只有一个有效 (gestureDetector)
。我怎样才能让两者都听取意见?
组合它们时,在 gestureScale
中执行 pinch
操作时永远不会调用 onScale
方法。但是当我不包括其他手势时它会单独工作。
您需要将所有手势都集中在一个 OnTouchListener
class 上并实现您的逻辑。
在下面的示例中,我遗漏了所有参数。并且你应该根据你的要求提供所有这些。
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
class MyGesture implements View.OnTouchListener {
private View view;
private MyDetector myDetector;
private ScaleGestureDetector scaleDetector;
private GestureDetector gestureDetector;
public MyGesture(View view, MyDetector myDetector) {
this.view = view;
this.myDetector = myDetector;
initialGestures();
}
private void initialGestures() {
scaleDetector = new ScaleGestureDetector(
view.getContext(),
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
myDetector.onScale();
return true;
}
});
gestureDetector = new GestureDetector(
view.getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
myDetector.onFling();
return false;
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
scaleDetector.onTouchEvent(event);
return true;
}
interface MyDetector {
void onScale();
void onFling();
}
}
并使用它:
myView.setOnTouchListener(new MyGesture(myView, new MyGesture.MyDetector() {
@Override
public void onScale() {
// do sth
}
@Override
public void onFling() {
// do sth
}
}));