Florian Mac Langlade
Florian Mac Langlade

Reputation: 1903

Calculate drawable width

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 :

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

Maybe with density ?

Upvotes: 0

Views: 125

Answers (1)

JafarKhQ
JafarKhQ

Reputation: 8734

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);

Upvotes: 2

Related Questions