如何为android中的所有分辨率获取相同的图像坐标? [英] How to get same co-ordinates for image for all resolutions in android?

查看:29
本文介绍了如何为android中的所有分辨率获取相同的图像坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持得到 x &所有分辨率的图像的 y 坐标.

I stucked with getting x & y co-ordinates of an image for all resolutions.

  1. 320,适用于具有以下屏幕配置的设备:
    • 240x320 ldpi(QVGA 手机)
    • 320x480 mdpi(手机)
    • 480x800 hdpi(高密度手机)

我创建了图像视图,图像的原始大小为 1500 x 1119.当​​我触摸图像时,我可以使用 onTouch() 获取坐标.如果在所有设备中更改坐标,我的问题.我得到了不同的 x &y 值在不同的欺骗中.我不能得到相同的 x &所有分辨率的 y 值.我该如何解决这个问题?

I have created my image view with original size of an image like 1500 x 1119. When i touched the image i can get the co-ordinates using by onTouch(). My issue if the co-ordinates are changed in all device. I am getting different x & y values in different deceives . I can't get same x & y values for all the resolutions. How can i resolve this?

推荐答案

这里有一个比您的特定布局所需的更通用的解决方案.这假设您知道源图像的尺寸并且想知道其上被点击的坐标,忽略屏幕尺寸和分辨率.尽管您的布局已经定义了与源图像匹配的宽度和高度,但这将处理宽度和高度设置为 wrap_contentmatch_parent 的布局,前提是您正在使用适合视图边界内的整个图像并保留纵横比的比例类型之一(例如 fitXY).

Here's a slightly more generic solution than your particular layout requires. This assumes you know the dimensions of the source image and want to know the coordinates on it that were tapped, ignoring screen size and resolution. Although your layout has defined width and height that appears to match the source image, this will handle layouts with width and height set to either wrap_content or match_parent, provided you're using one of the scale types that fits the whole image inside the view bounds and preserves the aspect ratio (e.g. fitXY).

我没有在 ScrollView 中尝试过,但理论上应该没问题.

I haven't tried this inside ScrollViews as you have yours but in theory it should be fine.

int sourceWidth = 1500;
int sourceHeight = 1119;

// Find view dimensions
int width = view.getWidth();
int height = view.getHeight();

// Assuming image has been scaled to the smaller dimension to fit, calculate the scale
float wScale = (float)width/(float)sourceWidth;
float hScale = (float)height/(float)sourceHeight;
float scale = Math.min(wScale, hScale);

// Find the offset in the direction the image doesn't fill the screen
// For ImageViews that wrap the content, both offsets will be 0 so this is redundant
int offsetX = 0;
int offsetY = 0;
if (wScale <= hScale) {
    offsetY = (int)((height/2) - ((scale * sourceHeight)/2));
} else {
    offsetX = (int)((width/2) - ((scale * sourceWidth)/2));
}

// Convert event coordinates to image coordinates
int sourceX = (int)((event.getX() - offsetX) / scale);
int sourceY = (int)((event.getY() - offsetY) / scale);

这篇关于如何为android中的所有分辨率获取相同的图像坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆