Android:如何确定屏幕上 ImageView 的尺寸(以英寸为单位)

Android: How to determine size of an ImageView on the screen in inches

这里有一些背景信息。我正在制作一个 Android 应用程序,它要求 ImageView 以相同的物理尺寸(xInches x yInches)显示在屏幕上,而不管图像显示在什么设备上。我遇到的问题是,默认情况下,ImageView 的大小会根据当前屏幕分辨率和屏幕大小而变化。

我需要的是一个函数:

我是 Android 开发的新手,所以我不是 100% 熟悉不同的坐标系,例如 px 与 dpi,但我正在寻找的只是一种方法将这些坐标系与物理坐标对应起来。

顺便说一句,我只使用英寸,因为我住在 U.S。然而,我的意思是说任何在 物理 space 而不是 屏幕 space.

感谢您的帮助!

对于这样的任务,您可能需要找到设备的 dpi,然后根据它调整图像的大小。所以像

String inchesToDp(int x, int y){

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    //int densityDpi = metrics.densityDpi;  used for general purpose
    int xInch = x * metrics.xdpi;     //1 inch in dpi is just the amount of dpi
    int yInch = y * metrics.ydpi;     //x and ydpi give exact lcd dpi for horizontal and vertical but you could just use x * densityDpi if it doesn't matter

    return "x in dp: " + xInch + "y in dp: " + yInch;

}

据我了解你的问题:

要在 android 中使用图像视图,首先您应该使用像 linearlayout 或 relativelayout 这样的视图组,然后将图像视图放入其中。用于高度和宽度的 imageviews 的常用属性是匹配父级。通过这样做,如果您的屏幕发生变化,您的 imageview 会根据您的屏幕发生变化。