2

我正在制作一个应用程序,我想在其中获得 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
            }
4

2 回答 2

2

为了明白这一点,我已经成功地使用了您所链接问题的答案。我刚刚用 getRawX() 和 getRawY() 改变了 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]);
于 2013-02-03T16:17:34.143 回答
0

在 ImageView 上应用 setOnTouchLinstener 并获取它的 x 和 y 位置。

imageView.setOnTouchListener((v, event) -> {
 finalX = event.getX();
 finalY = event.getY();
}
于 2021-04-14T07:13:44.800 回答