Issue
I have a drawable resource (a png image) for mdpi, hdpi and xhdpi. This image is used as background and based on its width I resize another bitmap and draw it over.
Everything works fine for mdpi, hdpi and xhdpi because my drawable folder for each density has the image. The problem occurs on different densities, for instance running on a Galaxy S4 with 1080p screen
Here is how I do it:
//Get the bitmap width
BitmapFactory.Options dimensions = new BitmapFactory.Options();
dimensions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.ic_marker_void, dimensions);
int width = dimensions.outWidth;
What I have found on testing is that width is 80 pixels when running on a 720p screen BUT when running on a 1080p device the width is also 80 pixels. This is the same size as the png from xhdpi folder.
BUT actually the image is rendered scaled and correctly when I display it on the 1080p screen. So it does take the xhdpi image but it scales it before displaying it. As a sidenote the image is used as a map marker background.
My question is: how can I obtain the width of the image exactly how it will be rendered on the screen, not the size of it from the xhdpi folder ?
Solution
Maybe this is what you looking for:
This assumes that, as you said, your code works fine for mdpi, hdpi and xhdpi and returns xhdpi width when greater than xhdpi.
float width;
BitmapFactory.Options dimensions = new BitmapFactory.Options();
dimensions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.ic_marker_void, dimensions);
float densityFactor = this.getResources().getDisplayMetrics().density;
if(densityFactor > 2) // 2 -> xhdpi
width = dimensions.outWidth / 2 * densityFactor;
else
width = dimensions.outWidth;
Answered By - ramaral
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.