Stralo
Stralo

Reputation: 474

How do you calculate the size of a loaded image

I have a an image image.png in the drawable folder (not any of the other drawable-hdpi, etc. folders).

This image's size is 50x50. But when I load it from my application like so: BitmapFactory.decodeResource(getResources(), R.drawable.image)

and then I check the size with getWidth() and getHeight(), I get 150x150.

Can anyone please explain how to calculate this 150x150 loaded size? (Not programmatically, I just want to know if there's a formula I could have used to tell that the image would be loaded to 150x150)

Also, is this 150x150 in pixels?

Thanks!

Upvotes: 0

Views: 549

Answers (3)

Android
Android

Reputation: 535

in android studio we use image size like this combinations.

Upvotes: 0

Neil
Neil

Reputation: 664

  • Yes, the value is in pixel unit, make use of the java doc my friend~
  • If you want the image size to stay unchanged, put it into drawable-nodpi
  • If you put the image in the drawable, it is the same as in the drawable-mdpi, as the first Android-powered device, the T-Mobile G1 is a normal size and mdpi (medium) density :)
  • And the no magic formula :

    ldpi (low) ~120dpi
    mdpi (medium) ~160dpi
    hdpi (high) ~240dpi
    xhdpi (extra-high) ~320dpi
    xxhdpi (extra-extra-high) ~480dpi
    xxxhdpi (extra-extra-extra-high) ~640dpi

    That is, in your case, likely developing on a xxhdpi device, 480/160*50=150, remember android is designed to be visualize all the ui elements at the same size on all screens, so the higher density , the larger scaled image .

Upvotes: 1

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

If you have Drawable image and you want to calculate the size of a loaded image

Drawable d = getResources().getDrawable(R.drawable.image);
int h = d.getIntrinsicHeight(); 
int w = d.getIntrinsicWidth();  

Android Documentation says:

public int getIntrinsicHeight ()

Return the intrinsic height of the underlying drawable object. Returns -1 if it has no intrinsic height, such as with a solid color.

public int getIntrinsicWidth ()

Return the intrinsic width of the underlying drawable object.Returns -1 if it has no intrinsic width, such as with a solid color.

Upvotes: 0

Related Questions