我正在制作一个应用程序,我想在其中获得 Image 上的接触点。我试过使用这个链接:
Android:如果图像视图按矩阵缩放,如何检测 ImageView 上的触摸位置?
但是,我无法获得正确的接触点。
我做错了什么?
任何帮助将不胜感激。
这是我的代码:
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
EventX=event.getX();
EventY=event.getY();
Log.d("ImageX"+event.getX(), "ImageY"+event.getY());
start.set(last);
mode = DRAG;
PosterActivity.isTouched=false;
break;
case MotionEvent.ACTION_MOVE:
// Log.i("onTouch", "origWidth : " + origWidth +
// " origHeight : " + origHeight);
if (mode == DRAG) {
deltaX= curr.x - last.x;
Log.d("deltaX",""+deltaX);
deltaY = curr.y - last.y;
Log.d("deltay",""+deltaY);
scaleWidth = Math.round(origWidth * saveScale);
scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
PosterActivity.isTouched=true;
}
break;
case MotionEvent.ACTION_UP: {
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK) {
Log.v("ACTION_UP", "ACTION_UP");
mPosX = curr.x;
mPosY = curr.y;
PosterActivity.isTouched=true;
performClick();
}
//setImageMatrix(matrix);
//invalidate();
break;
}
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}