0

I have a Drawable only in the hdpi folder (and can't be in others).

In my custom view, I want to calculate the ImageView width, in onMeasure().

Code is :

myImage.getDrawable().getIntrinsicWidth(); 

But, depend on the device, the IntrinsicWidth is not the same and not egal to the real width.

This code :

Log.e(TAG, "icon width="+myImage.getWidth()+" // intrinsic width="+myImage.getDrawable().getIntrinsicWidth()); 

Print these results :

  • On Nexus 4 : 180 // 187

  • On Asus transformers : 320 // 187

  • On Samsung S4 : 270 // 280

So, is there a way to get IntrinsicWidth equal to real width ?

Maybe with density ?

3
  • put it on drawable-nodpi folder otherwise it will be rescaled by the framework Commented Jun 5, 2014 at 8:55
  • It can't be moved... I have to find a way with image in hdpi folder Commented Jun 5, 2014 at 8:58
  • ok, if it really can't be moved you have one option: create custom Resource class, see how i did it here github.com/pskink/PatchworkDrawable/blob/master/… see the class extending Resources at the very bottom Commented Jun 5, 2014 at 9:07

1 Answer 1

2

While your Image in hdpi folder, Android will scale it (up or down) depending on on device density,
Put your Image in nodpi folder (drawable-nodpi) so Android will load the image with its original dimensions.

Update:
By knowing
0.75 - ldpi
1.0 - mdpi
1.5 - hdpi
2.0 - xhdpi
3.0 - xxhdpi
4.0 - xxxhdpi

And you know your image at hdpi folder so its 1.5 larger than the mdpi.

final float deviceDensity = getResources().getDisplayMetrics().density; int originalwidth = (int)((myImage.getWidth() * 1.5) / deviceDensity); 
Sign up to request clarification or add additional context in comments.

5 Comments

It can't be moved... I have to find a way with image in hdpi folder
why cant be moved? is your folder read-only :p. other wise why not just hard-coded your image width in your code, but when android load it, it will get scaled.
Because my boss don't want to :/
Then simply calculate the image width by getting the device density and multiply/divide it with the IntrinsicWidth
myImage.getWidth() = 0 in the onMeasure() method, I have to use myImage.getDrawable().getIntrinsicWidth();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.