1

Hello I am new in android programing so I want to know:

How to use drawable to compatible with all screen sizes (idpi, mdpi, hdpi, xhdpi, xxhdpi)

Actually I am confused about drawable screen sizes compatibility with all screen sizes

so I want to know that:

If I want my app to compatible with all screen size do I need to put images in every drawable folder

for example: background image

  • • drawable-ldpi ( 240x320 for QVGA Device 2.7 ) put image with this resolution
  • • drawable-mdpi ( 320x480 for HVGA Device 3.2 ) put image with this resolution
  • • drawable-hdpi ( 480x800 for WVGA Device 4.0 ) put image with this resolution
  • • drawable-xhdpi ( 1280x720 for WxVGA Device 4.7) put image with this resolution
  • • drawable-xxhdpi ( 1080x1920 for Nexus 5 Device ) put image with this resolution

or I can use a single same image for all screen sizes.

AbinZZ
  • 795
  • 7
  • 19
user3441251
  • 27
  • 2
  • 7
  • 3
    You have to make different images and put each in a different folder. All images will have the same name, but different sizes and resolutions. You can also make (but I **discourage you** to) a unique xxhdpi folder, with 480 dpi graphics in it. I discourage you, because Android scaling is 1: more expensive in **CPU usage** terms, 2: it leads to **worst results** – Phantômaxx Mar 20 '14 at 10:29

5 Answers5

6

You do not need to create an image in every possible drawable folder. It's enough to provide one image only and put it into drawable folder. This image will be scaled automatically, depending on your usage.

This will not provide good quality on all screens though and may be more expensive to compute (scale). It is recommended that you can provide separate images for different screen densities (e.g. drawable-mdpi, drawable-hdpi, etc.) and the idea is that they should have different resolutions matching the screen densities.

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • 1
    Incorrect!! Providing drawables for every dpi is the only way to get crisp images. – Mark Buikema Mar 20 '14 at 10:49
  • 1
    @MarkBuikema The question is about whether it's possible to use one image, not what is recommended. Anyway, I edited my question to make clear to show what's better approach. – Szymon Mar 20 '14 at 10:52
2

Your images will only look sharp and crisp if you provide drawables for every dpi. Ldpi is no longer supported so you can ignore ldpi.

Also, for your launcher icon, provide an xxxhdpi image. Tablets running on Kit Kat with full hd screens use these images in the launcher.

Creating 1 drawable and putting it in the drawable folder is bad practice! Your images will look blurry.

Mark Buikema
  • 2,483
  • 30
  • 53
1

I have got a very good method for this situation works like a charm. You only need to create one hd image for you and than method works-out everything.

Here is the method:

/**
 * Get DRAWABLE with original size screen resolution independent
 * @param is Input stream of the drawable
 * @param fileName File name of the drawable
 * @param density Density value of the drawable
 * @param context Current application context
 * @return Drawable rearranged with its original values for all
 * different types of resolutions.
 */
public static Drawable getDrawable(InputStream is, String fileName, int density, Context context) {
    Options opts = new BitmapFactory.Options();
    opts.inDensity = density;
    opts.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
    return Drawable.createFromResourceStream(context.getResources(), null, is, fileName, opts);
}

Here, you must prepare the inputstrem for your image file and set a density that is good for your screen at any usage area of yours. The smaller density is the lower quality, solve out by changing the value. Here are some examples on using the method:

1) Open an asset from the assets folder:

getDrawable(assetManager.open("image.png"), "any_title", 250, context)

2) Open a drawable from the drawables folder: Here first, you must provide your inputstream with this method: a) method:

/**
 * Get InputStream from a drawable
 * @param context Current application context
 * @param drawableId Id of the file inside drawable folder
 * @return InputStream of the given drawable
 */
public static ByteArrayInputStream getDrawableAsInputStream(Context context, int drawableId) {
    Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(drawableId)).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    return new ByteArrayInputStream(imageInByte);
}

and the usage: b) usage:

getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)

I hope it is useful.

Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
  • This appears to be scaling the image, which apparently is what the OS does if the image is in the root of the **'drawable'** folder. Have you compared resulting rendering quality and speed to the builtin scaling? – samus May 31 '17 at 18:30
  • Yeah, but if you are not using the root of the 'drawable' folder you can use my method – Bahadir Tasdemir Jun 01 '17 at 11:31
0

you have to create an image in several sizes. for example: Let's say that you have ImageView with 48dp x 48dp. Base on this size you have to create image with sizes:

  1. 48x48px(100% - mdpi)
  2. 64x64px(150% - hdpi)
  3. 96x96px(200% - xhdpi)

    and so on, if you have to support more.

NOTE: there is no need to create ldpi (75%) resolution image, because your hdpi will simply scale down twice.

If you use one size for several dpi, then there can be poor quality on some resolutions.

Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • This is not correct, it may be recommended to create different images for each folder but is not mandatory. – Szymon Mar 20 '14 at 10:54
  • if you don't care about display quality, then yes, it's not – Autocrab Mar 20 '14 at 10:54
  • @Autocrab: I have images in drawable, drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi in ratio of .75:1:1.5:2:3 respectively. For most of the emulators, images are appearing perfectly well, but, for some of them, images are not appearing in proper proportion. Like images appears well in 3.2" HVGA slider(ADPI) emulator- 320x480 mdpi, but, images do not appear well in 5.1" WVGA emulator- 480x800 mdpi. This is despite the fact that both are mdpi. Could you please help? – user1903022 May 25 '15 at 19:13
0

You can also use the Android Asset Studio, where you can upload an image and it will generate all of the types of resolution images you need. It can even be used to create animations and icons for your application. Here is the link: https://romannurik.github.io/AndroidAssetStudio/index.html

RW6
  • 59
  • 1
  • 14