Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap in Android: Size of resources for displaying as 50dp

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

  • ldpi resource: 38
  • mdpi resource: 50 (base)
  • hdpi resource: 75
  • xhdpi resource: 100

Or is one png file in "/res/drawables" enough that is 50px wide?

like image 916
caw Avatar asked Dec 07 '22 17:12

caw


1 Answers

You will have to create 4 images. mdpi will be the baseline, meaning mdpi is 100%. Others follow this formula:

drawables

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"/>
like image 52
aromero Avatar answered Dec 20 '22 20:12

aromero