Android:在ImageView上获取触摸位置点

时间:2012-10-02 09:14:45

标签: android android-canvas android-imageview ontouchevent

我正在创建一个应用程序,其中我想获得Image上的触摸点。我尝试过使用此链接:

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
            }

2 个答案:

答案 0 :(得分: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]);

答案 1 :(得分:0)

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

imageView.setOnTouchListener((v, event) -> {
 finalX = event.getX();
 finalY = event.getY();
}