I have a PNG image that I want to display in my application.
In the layout file (.xml) I set the width and height to 50dp (density-independent). But what should be the size of my resource(.png) files?
I thought about this (calculation based on density ratios):
Or is one png file in "/res/drawables" enough that is 50px wide?
You will have to create 4 images. mdpi
will be the baseline, meaning mdpi
is 100%. Others follow this formula:
Place each image in the proper resource folder, drawable-ldpi
, drawable-mdpi
, drawable-hdpi
, drawable-xhdpi
. Android will choose the proper drawable depending on the current device.
Then, in your layout simply wrap_content
your width and height, there is no need to specify any fixed size:
<ImageView
android:src="@drawable/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With