Red Taz
Red Taz

Reputation: 4179

Are density-specific graphics always necessary?

I am new to android development and I've been reading up on how to correctly include image assets in my application. Various articles (including the android developer guide) describe including multiple images to target different screen sizes by placing them in convention-based resource folders.

Although the system scales your layout and drawable resources based on the current screen configuration, you may want to make adjustments to the UI on different screen sizes and provide bitmap drawables that are optimized for different densities.

However, this implies that it is not always necessary to include multiple images targeting different screen sizes.

If I include a single image of sufficient size for a large screen then the system should scale this down for smaller screens without suffering any pixilation or loss, right? I would have thought this approach also reduces the overall payload of the application since there are less graphics files being bundled into the app.

I appreciate that there may be instances where app designers may want to tailor individual graphics per screen size but for a general case is the single-file approach a more appropriate strategy? What are the drawbacks of not providing density-specific graphics?

Upvotes: 0

Views: 186

Answers (4)

Xaver Kapeller
Xaver Kapeller

Reputation: 49817

Normally you should provide drawables for at least:

  • mdpi
  • hdpi
  • xhdpi
  • xxhdpi

ldpi is pretty much irrelevant nowadays since barely any devices exist that still fall in that category. Normally you also don't need anything above xxhdpi. It is possible to just provide the xxhdpi drawables and Android will scale them for the other densities, but that is not perfect. There might be some pixelations or generally lower image quality. If you want good results you need to provide drawables in all relevant densities.

If you were somehow unable to provide images for lower densities - which I have to emphasise again should be avoided if at all possible - you can help Android to scale the images correctly by only using sizes and in your layout that are easily divisible by 8, 4, and 2. For example instead of using 10dp you would use 8dp or 12dp. Using sizes that are only divisible by those three factors would in fact be best practice.

There are a few tools to assist you with properly scaling the drawables. Namely the image resizer tool that comes with the SDK and the Android Asset Studio.

I am sure you already read it, but here is the link to the relevant documentation.

EDIT: Since you mentioned that one can keep the APK smaller by not including the images of lower densities: You have to remember the drawables for the highest density make up the majority of the size of your APK. Not including the ones for lower densities has really not that much of an effect on APK size.

Upvotes: 2

Parth Kapoor
Parth Kapoor

Reputation: 1504

In a layout you have a imageView on which you wish to show some image. And this image is 1100X800 sized(so as to fit the max size).

Basically your approach should work, but the thing is you will need to construct your layouts so that the dimensions of the containers are well defined. I mean either you will have to provide a static size (W:H) to the image view or else use a Linear layout and weights.

Also using these images of max size may work well for image view containers as they are specifically built to handle images so have a set of properties that you can use. If you use the same images as backgrounds for relativeLayouts/LinearLayouts/Buttons, it might be more of a hectic job.

Also if your image is simply a texture or a color to be used, then this approach is fine. But in case you have text or Logos or some specific shapes in your images, this approach will tend to show the image in disproportionate sizes. This part will never be acceptable.

Upvotes: 1

D4ngle
D4ngle

Reputation: 21

This also depends on the usage of the image. If its just a button image ressource, you might want to use one scalable image ressource like these 9-patch ones. See this .9.png guide if thats the case: http://developer.android.com/tools/help/draw9patch.html

Upvotes: 0

Budius
Budius

Reputation: 39836

Theoretically, yes, but reality it a bit different.

As Gabe mention on the comment, a device with a 800x480 4" screen, will most likely have at least 4x less memory than a quadHD 5" screen device.

So overall performance, specially on list scroll, will be severely compromised if you only include said xxhdpi, plus lower-end (or older) devices will likely have OutOfMemory exception while dealing with your app.

If you're worried with app size, the best approach is to use the flavors that the gradle build system offers (see Android-Studio for that).

Upvotes: 0

Related Questions