我有一个用ScrollView包装的TextView。我需要做的是在TextView上检测缩放和常用手势(如tap,double tab等)。此外,我想要一些手势来处理标准处理程序(例如滚动)。 我只是无法得到基础知识,我不知道我为此编码了什么。我为他们制作了两个探测器(ScaleGestureDetector和GestureDetector)和听众。另外,我为我的TextView设置onTouchListener,如下所示:
tw.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!gestureDetector.onTouchEvent(motionEvent)) {
scaleGestureDetector.onTouchEvent(motionEvent);
};
return true;
}
});
未检测到任何手势。我用Google搜索的每个例子都使用一个探测器。有人可以帮我解决这个概念吗?如何在同一个视图上检测比例(或捏缩放),双击,单击等?
答案 0 :(得分:2)
一种解决方案是为两个探测器调用onTouchEvent
:
tw.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
scaleGestureDetector.onTouchEvent(motionEvent);
return true;
}
});
然而,这可能导致不良行为,因为两个探测器可能同时触发;例如,一个手势可以被识别为缩放手势和滚动手势。
要解决此问题,您可以引入新变量scaleOngoing
来跟踪比例事件是否正在进行:
scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() {
public void onScaleEnd(ScaleGestureDetector detector) {
scaleOngoing = false;
}
public boolean onScaleBegin(ScaleGestureDetector detector) {
scaleOngoing = true;
return true;
}
public boolean onScale(ScaleGestureDetector detector) {
// do scaling here
return false;
}
});
然后让你的GestureDetector
听众检查是否正在进行缩放手势:
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(!scaleOngoing) {
// do scrolling here
}
return true;
}
});
答案 1 :(得分:1)
方法 1:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (mScaleGestureDetector.isInProgress()) {
return false;
}
// Handle Scrolling Event.
return true;
}
方法 2:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
mShouldAllowScrolling++;
}
if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
mShouldAllowScrolling--;
}
boolean retVal = mScaleGestureDetector.onTouchEvent(event);
if (mShouldAllowScrolling == 0) {
retVal = mPanGestureListener.onTouchEvent(event) || retVal;
}
return retVal || super.onTouchEvent(event);
}
答案 2 :(得分:0)
private class GestureListener extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return super.onSingleTapUp(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTap(e);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDown(e);
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
使用此
进行尝试