我的目标是在相机移动时显示ImageView,但仅在用户移动地图时显示,而不是在放大或缩小时显示。我怎样才能区分这两者?
我尝试使用OnCameraMove和OnCameraIdle侦听器,但缩放被注册为移动,而OnCameraMoveStartedListener只能注册移动是否与用户相关。
同样在变焦时我需要将变焦对焦。希望这是有道理的。
答案 0 :(得分:0)
不可思议的是,无法区分缩放和直接移动地图。然而,有一种hacky方式能够完成你正在寻找的东西。您可以创建一个扩展FrameLayout的包装类。在包装器内部覆盖dispatchTouchEvent方法并实现用于处理缩放/拖动的逻辑或用于区分用户交互所需的任何内容。例如,为了检测缩放,我们只想确保有2个手指,它们已经移动超过某个阈值并且它们正在相反的方向上移动。为此,我们必须覆盖dispatchTouchEvent
switch (ev.getAction()) {
case MotionEvent.ACTION_MOVE:
boolean isPrimMoving = isScrollGesture(event, 0, mPrimStartTouchEventX,
mPrimStartTouchEventY);
boolean isSecMoving = (mPtrCount > 1 && isScrollGesture(event, 1,
mSecStartTouchEventX, mSecStartTouchEventY));
if (mPtrCount > 1 && isPinchGesture(event)) {
Log.d("TAG", "PINCH! OUCH!");
} else if (isPrimMoving || isSecMoving) {
// A 1 finger or 2 finger scroll.
if (isPrimMoving && isSecMoving) {
Log.d("TAG", "Two finger scroll");
} else {
Log.d("TAG", "One finger scroll");
}}
break;
此类将声明一个接口,该接口将根据用户交互更新活动。然后,在片段中启动此类
mTouchView = new myWrapperClass(getActivity());
mTouchView.addView(mOriginalContentView);
return mTouchView;
您需要覆盖onTouch
private float mPrimStartTouchEventX = -1;
private float mPrimStartTouchEventY = -1;
private float mSecStartTouchEventX = -1;
private float mSecStartTouchEventY = -1;
private float mPrimSecStartTouchDistance = 0;
...
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = (event.getAction() & MotionEvent.ACTION_MASK);
switch (action) {
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
mPtrCount++;
if (mPtrCount == 1) {
mPrimStartTouchEventX = event.getX(0);
mPrimStartTouchEventY = event.getY(0);
Log.d("TAG", String.format("POINTER ONE X = %.5f, Y = %.5f", mPrimStartTouchEventX, mPrimStartTouchEventY));
}
if (mPtrCount == 2) {
// Starting distance between fingers
mSecStartTouchEventX = event.getX(1);
mSecStartTouchEventY = event.getY(1);
mPrimSecStartTouchDistance = distance(event, 0, 1);
Log.d("TAG", String.format("POINTER TWO X = %.5f, Y = %.5f", mSecStartTouchEventX, mSecStartTouchEventY));
}
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
mPtrCount--;
if (mPtrCount < 2) {
mSecStartTouchEventX = -1;
mSecStartTouchEventY = -1;
}
if (mPtrCount < 1) {
mPrimStartTouchEventX = -1;
mPrimStartTouchEventY = -1;
}
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "Move " + mPtrCount);
break;
}
return true;
}
编辑:低于isPinchGesture
&amp;的执行情况isScrollGesture
private boolean isPinchGesture(MotionEvent event) {
if (event.getPointerCount() == 2) {
final float distanceCurrent = distance(event, 0, 1);
final float diffPrimX = mPrimStartTouchEventX - event.getX(0);
final float diffPrimY = mPrimStartTouchEventY - event.getY(0);
final float diffSecX = mSecStartTouchEventX - event.getX(1);
final float diffSecY = mSecStartTouchEventY - event.getY(1);
if (Math.abs(distanceCurrent - mPrimSecStartTouchDistance) > mViewScaledTouchSlop
// and the fingers are moving in opposing directions
&& (diffPrimY * diffSecY) <= 0
&& (diffPrimX * diffSecX) <= 0) {
return true;
}
}
return false;
}
private boolean isScrollGesture(MotionEvent event, int ptrIndex, float originalX, float originalY){
float moveX = Math.abs(event.getX(ptrIndex) - originalX);
float moveY = Math.abs(event.getY(ptrIndex) - originalY);
if (moveX > mViewScaledTouchSlop || moveY > mViewScaledTouchSlop) {
return true;
}
return false;
}
编辑2:您可以使用使用ScaleGestureDetector的OnTouchListener在Android中检测到捏合手势。 了解更多信息:https://developer.android.com/training/gestures/scale.html
static class MyPinchListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d("TAG", "PINCH! OUCH!");
return true;
}
}
/* Using it */
ScaleGestureDetector mScaleDetector =
new ScaleGestureDetector(this, new MyPinchListener());
mGestureView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
return true;
}
});