Android bitmap: resource size to display 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 (regardless of density). But what should be the size of my resources (.png)?

I thought about this (calculation based on density coefficients):

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

Or one png file in the "/ res / drawables" folder, which has a width of 50 pixels?

+4
source share
2 answers

You will need to create 4 images. mdpi will be the base, mdpi is 100%. Others follow this formula:

drawables

Put each image in the corresponding resources folder, drawable-ldpi , drawable-mdpi , drawable-hdpi , drawable-xhdpi . Android will choose the appropriate option depending on the current device.

Then in your layout just wrap_content your width and height, there is no need to specify a fixed size:

 <ImageView android:src="@drawable/my_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
+11
source

It is not enough to have only one image. You must put another image in each resource folder (drawables-ldpi, drawables-mdpi, drawables-hdpi and drawables-xhdpi).

Note that 50dp not equal to 50px

You can use this formula:

px = dp * (dpi / 160)

You can find other helpful tips here .

+3
source

Source: https://habr.com/ru/post/1389369/


All Articles