要在缩放后从ImageView获取原始X和Y坐标,需要先将ImageView中的坐标转换为实际的位图像素坐标。可以使用以下方法:
int[] imageViewLocation = new int[2]; imageView.getLocationOnScreen(imageViewLocation); int imageViewX = imageViewLocation[0] + imageView.getWidth() / 2; int imageViewY = imageViewLocation[1] + imageView.getHeight() / 2;
float scaleFactor = imageView.getScaleX();
int drawableWidth = imageView.getDrawable().getIntrinsicWidth(); int drawableHeight = imageView.getDrawable().getIntrinsicHeight(); int bitmapWidth = imageView.getDrawable().getBounds().width(); int bitmapHeight = imageView.getDrawable().getBounds().height();
float actualImageWidth = drawableWidth * scaleFactor; float actualImageHeight = drawableHeight * scaleFactor;
float xPercentage = (imageViewX - imageViewLocation[0]) / (float)imageView.getWidth(); float yPercentage = (imageViewY - imageViewLocation[1]) / (float)imageView.getHeight();
float xInImage = xPercentage * actualImageWidth; float yInImage = yPercentage * actualImageHeight;
float[] point = new float[] {xInImage, yInImage}; Matrix matrix = imageView.getImageMatrix(); matrix.invert(matrix); matrix.mapPoints(point);
现在,point数组将包含任何特定点在图像中的像素坐标。