Soham
Soham

Reputation: 826

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

Here's some background information. I'm making an Android app that requires an ImageView to be shown on the screen in the same physical size (xInches by yInches) regardless of the device that the image is being shown on. The problem I'm having is that the size of the ImageView, by default, changes size depending on the current screen resolution and the screen size.

What I need is a function that:

I'm kind of new to developing for Android, so I'm not 100% familiar with the different coordinate systems, such as px vs dpi, but all I'm looking for is a way to correspond these coordinate systems with physical coordinates.

By the way, I'm only using inches because I live in the U.S. However, what I mean to say is any unit that measures in physical space rather than screen space.

Thanks for your help!

Upvotes: 0

Views: 404

Answers (2)

mohammad
mohammad

Reputation: 31

as i understand your question:

for using an imageview in android first of all you should use a viewgroup like linearlayout or relativelayout an put you imageview inside it. the usual attributes using for imageviews for height & width is match parent. by doing this if your screen changes, your imageview changes in relation to your screen.

Upvotes: 1

Alvin X.
Alvin X.

Reputation: 161

For such a task you will probably need to find the device's dpi and then resize the image based on that. So something like

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;

}

Upvotes: 1

Related Questions