Willyanto Halim
Willyanto Halim

Reputation: 413

Android : set width and height of bitmap base on screen size programmatically

let say I have Bitmap A with the dimensions 165x15.7 which is displayed correctly on 320x240 screen size with 0.75 density. if i want to show the bitmap images on different screen size like 480x320 with 1.0 density, how to set the bitmap width and height dynamically on different screen size? i want to do it programmatically,I don't want to use xml/ multiple layouts/ multiple dimensions. any help would be appreciated. thank you.

here is for the example

320x240 screen dimension

480x320 screen dimension

enter image description here

as you can see the screenshot above, the example logo look smaller on 480x320 screen dimension

Upvotes: 0

Views: 3017

Answers (1)

zihadrizkyef
zihadrizkyef

Reputation: 1941

You can get the scale factor of device screen to scale the image size with this code :

getResources().getDisplayMetrics().density;

This will give you:

ldpi = 0.75

mdpi = 1.0

hdpi = 1.5

xhdpi = 2.0

xxhdpi = 3.0

xxxhdpi = 4.0

source : https://stackoverflow.com/a/10948031/6217595

Upvotes: 2

Related Questions