我有这个应用程序,其中有 2 个 ImageView,一个充当背景,另一个是位于背景图像上的小图像,充当地图上的标记,需要保持与背景图像相关的位置.
因此,在拖动时实现这一点很容易,但我需要的是在用户缩放背景图像时保持小图像与背景图像的相对位置,这是到目前为止的代码(lampMatrix 和 lampStart 是矩阵和 PointF用于较小的图像):
public boolean onTouch(View v, MotionEvent rawEvent) {
WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
// ...
ImageView view = (ImageView) v;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
savedLampMatrix.set(lampMatrix);
lampStart.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
lampMatrix.set(savedLampMatrix);
lampMatrix.postTranslate(event.getX() - lampStart.x, event.getY() - lampStart.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
((ImageView)findViewById(R.id.imageView1)).setImageMatrix(lampMatrix);
return true; // indicate event was handled
}