在android中定义缩放图像上的可触摸区域

发布于 2024-12-27 06:12:03 字数 1310 浏览 2 评论 0原文

我之前曾问过与此相关的问题,但我认为我错误地设定了我的目标。

我所拥有的:一个自定义 ImageView,它显示图形并将多个可触摸区域定义为图像内的矩形。我的问题是缩放。我想根据位图文件中的实际分辨率定义图像的可触摸区域,但转换这些坐标,以便矩形覆盖缩放图像上的相同区域。

这是我到目前为止所得到的: 创建视图后,计算实际尺寸与缩放尺寸的比率

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Drawable pic=this.getDrawable();
int realHeight= pic.getIntrinsicHeight();   
int realWidth=pic.getIntrinsicWidth();
int scaledHeight=this.getHeight();
int scaleWidth=this.getWidth();
heightRatio=(float)realHeight/scaledHeight;
widthRatio=(float)realWidth/scaleWidth;
}

现在我想获取在原始(未缩放)图像上定义矩形的坐标 并将该矩形绘制到图像的同一区域 - 但考虑到比例:

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p=new Paint();
p.setStrokeWidth(1);
p.setColor(Color.BLUE);
for (HotSpot h: spots)
{
//Each hotspot has a rectangle defined in reference to the actual size of the image
Rect old=h.getRect();
float offsetLeft=old.left+(old.left*widthRatio);
float offsetTop=old.top+(old.top*heightRatio);
float offsetRight=old.right+(old.right*heightRatio);
float offsetBottom=old.bottom+(old.bottom*widthRatio);
RectF nRect=new RectF(offsetLeft,offsetTop,offsetRight,offsetBottom);
canvas.drawRect(nRect,p);

 } 

结果“在大致范围内”,但不太准确。任何帮助表示赞赏。

I have asked questions related to this before, but I think I was framing my objective incorrectly.

What I have: a custom ImageView that displays a graphic and defines multiple touchable areas as rectangles within the image. My problem is scaling. I want to define the touchable area of the image based on it's actual resolution in the bitmap file, but translate those coordinates so the the rectangle covers the same area on the scaled image.

This is what I've got so far:
When the view is created, calculate the ratio of the actual to scaled sizes

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Drawable pic=this.getDrawable();
int realHeight= pic.getIntrinsicHeight();   
int realWidth=pic.getIntrinsicWidth();
int scaledHeight=this.getHeight();
int scaleWidth=this.getWidth();
heightRatio=(float)realHeight/scaledHeight;
widthRatio=(float)realWidth/scaleWidth;
}

Now I want to take the coordinates that define rectangle(s) on the original (un-scaled) image
and draw that rectangle(s) to the same area of the image -- but accounting for scale:

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p=new Paint();
p.setStrokeWidth(1);
p.setColor(Color.BLUE);
for (HotSpot h: spots)
{
//Each hotspot has a rectangle defined in reference to the actual size of the image
Rect old=h.getRect();
float offsetLeft=old.left+(old.left*widthRatio);
float offsetTop=old.top+(old.top*heightRatio);
float offsetRight=old.right+(old.right*heightRatio);
float offsetBottom=old.bottom+(old.bottom*widthRatio);
RectF nRect=new RectF(offsetLeft,offsetTop,offsetRight,offsetBottom);
canvas.drawRect(nRect,p);

 } 

The results are "in the ball park" but not quite accurate. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕烟庭风 2025-01-03 06:12:03

您可以尝试以下解决方案:

  • 获取屏幕密度
  • 获取图像高度(或宽度)
  • 将高度(或宽度)除以密度,从而得到以英寸为单位的长度
  • 将坐标除以以英寸为单位的长度,从而得到一个关系坐标与图像之间的距离,与图像有效尺寸无关
  • 当您必须在不同缩放的图像上绘制相同的点时,请将最后的结果乘以第二个图像的长度(以英寸为单位)(您可以使用列出的相同操作获得该长度)上面)

示例:
在第一张图像上:

float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float scaledXCenter = xCenter / inchesLength;

在具有不同比例的同一图像上:

float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float restoredXCenter = scaledXCenter * inchesLength;

You can try this solution:

  • Get the screen density
  • Get the image height (or width)
  • Divide the height (or width) by the density, so you get the length in inches
  • Divide the coordinate by the length in inches, so you get a relationship between the coordinate and the image which is independent by the image effective size
  • When you have to draw the same point on a differently scaled image, multiply the last result for the length in inches of the second image (which you obtain using the same operations listed above)

Example:
On the first image:

float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float scaledXCenter = xCenter / inchesLength;

On the same image with a different scale:

float density = getResources().getDisplayMetrics().density;
int width = getWidth();
float inchesLength = width/density;
float restoredXCenter = scaledXCenter * inchesLength;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文