我的自定义视图中有一个背景图像作为可绘制对象。这个drawable可以被缩放或移动。
目前我需要一个在图像上绘制的绿点相对于屏幕是静止的。也就是说,它应该始终与销位于相同的位置,如下所示。(当然,图钉只是一个 ImageView,根本不动!)
当后面的地图在我的自定义视图 MapView 中按如下方式移动时,我已成功使其相对于屏幕静止:
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: { // triggered as long as finger movers
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
// update the starting point if the 'Start' button is not yet pressed
// to ensure the screen center (i.e. the pin) is always the starting point
if (!isStarted) {
Constant.setInitialX(Constant.INITIAL_X - dx);
Constant.setInitialY(Constant.INITIAL_Y - dy);
if ((historyXSeries.size() > 0) && (historyYSeries.size() > 0)) {
// new initial starting point
historyXSeries.set(0, Constant.INITIAL_X);
historyYSeries.set(0, Constant.INITIAL_Y);
}
}
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
通过上面的操作,当背景图像移动时,我的绿点会停留在那里。
但是当背景图像被缩放时,我在试图让它保持在那里时遇到了问题。
本质上,我并不真正了解其canvas.scale(mScaleFactor, mScaleFactor)
工作原理,因此我无法像在简单移动案例中所做的那样相应地移动绿点。
我认为应该在下面的比例监听器处理程序中添加一些东西,有人可以帮我填写那部分吗?
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(1f, Math.min(mScaleFactor, 10.0f)); // 1 ~ 10
// HOW TO MOVE THE GREEN DOT HERE??
invalidate();
return true;
}
或者请至少解释一下是如何canvas.scale(mScaleFactor, mScaleFactor)
工作的,我该如何相应地移动绿点?