gpt4 book ai didi

java - ImageView 内容的坐标

转载 作者:行者123 更新时间:2023-11-30 01:36:09 27 4
gpt4 key购买 nike

所以我有一个 ImageView,我将 png 设置为背景。我们假设 png 图片是一个圆形,所以它不会占用 ImageView 的所有空间。我需要捕获仅单击 ImageView(圆圈)内容而不是剩余空白区域的事件。这真的有可能吗?或者使用任何其他 Android 控件?

编辑:我仍然无法让它工作。所以更准确地说,我有这张鸡蛋图片,我将其设置为 ImageView src

enter image description here

我只想点击它。敌人的例子,如果我在鸡蛋外面稍微点击一下,我想知道这一点并阻止 ImageView 点击事件中的代码。

我试过这段代码,但我不知道这对我有什么帮助:

ImageView imageView = (ImageView)findViewById(R.id.imageview);
Drawable drawable = imageView.getDrawable();
Rect imageBounds = drawable.getBounds();

//original height and width of the bitmap
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();

//height and width of the visible (scaled) image
int scaledHeight = imageBounds.height();
int scaledWidth = imageBounds.width();

//Find the ratio of the original image to the scaled image
//Should normally be equal unless a disproportionate scaling
//(e.g. fitXY) is used.
float heightRatio = intrinsicHeight / scaledHeight;
float widthRatio = intrinsicWidth / scaledWidth;

//do whatever magic to get your touch point
//MotionEvent event;

//get the distance from the left and top of the image bounds
int scaledImageOffsetX = event.getX() - imageBounds.left;
int scaledImageOffsetY = event.getY() - imageBounds.top;

//scale these distances according to the ratio of your scaling
//For example, if the original image is 1.5x the size of the scaled
//image, and your offset is (10, 20), your original image offset
//values should be (15, 30).
int originalImageOffsetX = scaledImageOffsetX * widthRatio;
int originalImageOffsetY = scaledImageOffsetY * heightRatio;

最佳答案

这里是您将如何获得点击事件:

int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
//From this and the touch coordinates you can calculate the point inside the ImageView:

int touchX = (int) event.getX();
int touchY = (int) event.getY();

int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate

关于java - ImageView 内容的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35094921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com