0

Although Android drawables support for multiple screens is quite a powerful tool, especially its drawable-sw???dp selector, I lack one quite important feature and that is support of screen size in pixels selector (It could be implemented as drawable-sw???px).

Imagine you have an application where you'd like to put some graphics element scaled to N% of screen size. If you use directories based on screen size in dpi, your images will be blurry on hdpi screens, and if you use directories based on dpi, they will be blurry on large screens.

Of course you can put those drawables to i.e. drawables-sw320dp-hdpi, drawables-sw320dp-mdpi, etc. but this produces explosion of various image versions provided (and APK size) and testing environments needed.

Currently I'm workarounding this issue by putting such drawables in assets folder and selecting them programatically but this is certainly not an ideal solution and loading drawables from assets is not working flawlesly everywhere.

How do YOU address this issue, please?

Blackhex
  • 1,694
  • 3
  • 21
  • 45
  • 1
    My question is practically duplicate of http://stackoverflow.com/questions/16171390/screen-pixel-resolution-drawable-selector (sorry, searching didn't show it) but I think it explains it better :-). – Blackhex Apr 23 '13 at 14:14
  • You put link to current page – Borzh Apr 08 '15 at 20:16
  • You are right. Unfortunatelly I don't know what the link should be now. – Blackhex Apr 10 '15 at 20:40

1 Answers1

0

scale your graphics before you put them on project, and then put them to their convenient folder. e.g assume you have a bg.jpg, make 4 versions of it 700x100, 600x80, 500x60, 400x50 and put them drawable-xhdpi, drawable-hdpi, drawable-mdpi, drawable-ldpi respectvely. If it doesn't work, try to use vector graphics, their pixels will be precomputed every time they're scale. And finally you can scale using Matrix classes, but they can be hard to learn and use.

Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • Please, read my question carrefully, you are not answering on the actual topic. – Blackhex Apr 23 '13 at 14:08
  • ok the below answer is how i currently solving it, either you will get bigger apk size or process it within your application, as far as i know there is no other option – Onur A. Apr 23 '13 at 14:13
  • Ok, the fact that you are putting the resources to drawable-?dpi folders confused me because this produces the issue not solves it. The resources must be in assets so it could be loaded programatically. – Blackhex Apr 23 '13 at 14:16
  • you can load the resources programmatically from drawable folders. For images try to use, Bitmap bm=BitmapFactory.decodeResource(context.getResources(), R.drawable.yourresourceename); and it loads the image from proper drawable folder, for example if your device is hdpi then this code above loads image from hdpi folder and so on. – Onur A. Apr 23 '13 at 15:33
  • This still does not solve the issue because you don't want to load resource from directory according to screen DPI but the actual pixel resolution. – Blackhex Apr 23 '13 at 15:44
  • ok then create a drawable folder which name should be just 'drawable' no prefix or suffix, and put your all stuff with highest resolution in it, and then load your resources as i show above. And adjust your resources in your code by scaling them with matrixes or something else. Btw you can create px based drawable folders. – Onur A. Apr 23 '13 at 18:28
  • That is not solution either, the highest resolution drawable could cause OutOfMemory error when loaded on low cost devices to memory before scaling. – Blackhex Apr 24 '13 at 07:57
  • "Btw you can create px based drawable folders." How? – Blackhex Apr 24 '13 at 07:58
  • sorry it should be dp based, and you can programmatically convert from dp to px displayMetrics = context.getResources().getDisplayMetrics(); return (int)((dp * displayMetrics.density) + 0.5); – Onur A. Apr 24 '13 at 08:29
  • Ok, lets say you put an image pre-scaled for 640x320px screen into drawable-sw320dp folder. Now, how do you load the same file on 640x320px mdpi screen device and how do you load it on 640x320px screen ldpi device? – Blackhex Apr 24 '13 at 08:40
  • take a look http://stackoverflow.com/questions/5558534/do-i-need-14-different-layouts-to-support-all-android-devices – Onur A. Apr 24 '13 at 10:27
  • There is nothing I woudn't be aware of for a long time. [@wufoo](http://stackoverflow.com/a/5558604/685292) in his/her comment about full screen backgrounds addressed the issue but noone gave him a resolution. – Blackhex Apr 24 '13 at 10:37