我在做一个应用程序,其中我想对图像的接触点。我已经使用这个链接尝试:
Android: how to detect touch location on ImageView if the image view is scaled by matrix?
但是,我没能得到正确的接触点。
我做错了什么?
任何帮助将不胜感激。
这是我的代码:
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
}
最佳答案
正确地说,我已经成功地使用了您所链接问题的答案。
我刚刚用getRawX()和getawaY()更改了getX()和getY()
尝试使用此代码示例
// calculate inverse matrix
Matrix inverse = new Matrix();
matrix.invert(inverse);
// map touch point from ImageView to image
float[] touchPoint = new float[] {event.getRawX(), event.getRawY()};
inverse.mapPoints(touchPoint);
// touchPoint now contains x and y in image's coordinate system
Log.d(TAG, "touch coords="+touchPoint[0] + " - " + touchPoint[1]);