For using images in my android application, I will use this technique :
I put all of my images into "drawable-xhdpi" folder to let the application scale the images down for various resolutions.
I wondering does it make sense ? What is your experiences ?
Thanks for sharing...
UPDATE:
Maybe this method is a nice one, But an another question raises here which is :
Does this technique causes a memory leak for ldpi devices? I don't want to solve the problem by using svg images, Suppose there are bit-mapped images which are not vector based and there is no choice or alternative to replace. using a bitmap is the only choice.however using this method has a cons which will affect the overall layout and in some devices the position of relevant controls will be changed.
While this will work, it's not ideal and not recommended. Please look at the official documentation for Supporting Multiple Screens:
Although the system performs scaling and resizing to make your application work on different screens, you should make the effort to optimize your application for different screen sizes and densities. In doing so, you maximize the user experience for all devices and your users believe that your application was actually designed for their devices—rather than simply stretched to fit the screen on their devices.
And:
Provide different bitmap drawables for different screen densities
By default, Android scales your bitmap drawables (.png, .jpg, and .gif files) and Nine-Patch drawables (.9.png files) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen. This scaling can cause artifacts in the bitmaps. To ensure your bitmaps look their best, you should include alternative versions at different resolutions for different screen densities.
You can use the File / New / Image Asset to automatically generate the scaled assets.
Best way is use SVG files instead of PNG and import them to project in Androis studio: File -> New -> Vector Asset or use icon fonts like https://github.com/mikepenz/Android-Iconics
SVG file is inserted to drawable and converted to PNG files while compile time for back compatibility, but you don't need to maintain a pack of files, but just one vector file.
I use inkscape to create svg drawings and also to convert to png I think the conversion has better quality and supports all svg functions.
With inkscape and a script it is possible to do batch conversion, I have not tried it yet, until now I convert them one by one
Related
Is it Ok to provide image resources for all densities in Android (LDPI, MDPI, HDPI...) and that the system can choose the appropriate one for each device, or it is more efficient to provide only high resolution ones and downscale them to devices with lower resolution? In the last case, can the system itself do the downsampling, or will it cause a lot of OutOfMemory errors and slow performance. On the other hand, you can do downsample the images using the right classes that provide the Android SDK, but will you have to downsample each time you load the app? Any help will be appreciated.
Instead of using a raw image format, you can use a Vector Graphics instead
Your default android application's icon is actually a vector resource
More guidelines about this can be found here:
Add Multi-Density Vector Graphics
Yes, providing different scales would be the more appropriate way. Yes again as downscaling an image isn't efficient as it slows down the app and also gives memory exception. But that's setting the resource directly using ImageView. As other open sources like Picasso can be used to fit the resource in the view (still slow). Now, having separate scales for a particular image is best. As you don't have to programmatically get the user's device's resolution and adjust scaling because the app will automatically choose the best scale from the folders (faster way). You don't even have to worry about memory exception. It's like no work. You just have to keep necessary scales of the resource in your drawable and you're good to go. You can always find online converters, they rescale your resource to all scales and upload it in a . zip file. Good luck!
You can also use single Vector image for resources to reduce your apk size
I am currently working on a 2D game for Android. All of the images that I render were created as SVG's and then exported to PNG's which I have placed in my assets directory. These images are also all sprites that I render using a sprite batcher.
My question is around using different versions of the sprites for devices with different densities. I know that when developing a traditional android app you could place separate directories for separate density levels inside of res and then place the correctly sized versions of the images in question in these sub-directories of res.
What is a good approach when dealing with sprite images in the asset directory? Should I just have one copy of the sprites at a really high density / resolution that I use for everything and I just render them on a coordinate system that is independent of screen size or should I resize the image based on screen density before I process it to create all my texture regions? Or is there another solution entirely?
Currently I have one high resolution set of sprites that I use for everything, it just seems wasteful and inefficient for the lower density devices.
the best answer(s) that I got to this question are on gamedev.stackexchange: https://gamedev.stackexchange.com/questions/69970/recommended-way-to-handle-sprite-assets-for-different-screen-densities-android
If an asset is present in the drawable-xhdpi directory, do its equivalents need to be present in the drawable-hdpi/mdpi/ldpi directories as well in order to support those devices? Or, will Android downscale the xhdpi asset? If Android does downscale the asset, is there any benefit to using the lower dpi folders?
One benefit to using the lower folder is that you get control over how the image is downscaled. High resolution images may not necessarily look the way you want when they are reduced, it depends on the filtering algorithm used and the content of the image. If the high precision image has too many small details it may look odd or grainy at small resolutions.
By placing your own image in the ldpi folder, you get artistic control over exactly how the image looks. You may wish to use a simpler image with less high frequency content in the smaller image, so that it is clearer and easier to see and understand.
In addition to my comment on your question, I have a few reasons why you may want to include drawables in all folders:
If you have a drawable in xhdpi that's very high-res and has a lot of "activity" going on in it, it may look terrible and clustered when scaled down. Thus, you could create smaller, similar versions of that icon (named similarly across these density folders) that contain less "cluster" as you move down in size.
Different size layouts may want different size icons, and the scaling may not occur as you personally want it, so making your own scaled drawables would (mostly) prevent this.
Lint will yell at you if you don't put drawables in all the density folders.
If I think of any more reasons I'll add them.
All I do is scale image using GIMP and put them into hdpi, ldpi and mdpi. Is there a good reason for bundling icons of all different sizes into the package, since I do not correct the pixels after resizing?
In android we have a tool called NinePatch here we use a single image for all the type of screen resolution. just you need to create an image using Ninepatch tool. The NinePatch image will have an extension .9.png
In Android we have phones of different resloution and a single app/code we develop supports multi resolution in order to maintain in the image clarity we need to have images in hdpi,mdpi,ldpi
Even if you don't export your vector images into your icon files directly to have the best quality, resizing them in Gimp is still better than just using one icon in your project, because the scaling of a dedicated image processor (like Gimp2) will always be better than what Android can do natively for you.
If you scale down from a higher resolution then it's ok.
But if you scale up from a lower resolution then it will just look bad on those devices.
I was wondering if there is a way for using one set of images for all the resolutions. I have an application, and I want to port it on all the android resolutions but I don't want to store 3 sets of images, for every resolution category. What would be the best practice to use as less images as possible.
Thank you!
Sure, you can provide only one set of images, in the base drawable directory, and Android will automatically resize as required.
This may result in aliasing and images that are not as clear as would be possible if you were to provide images sized properly.
You probably mean density, not resolution (in Android, applications do not work directly with resolution).
Actually, you don't need to support all 3 densities. Quote from developer.android.com:
This pre-scaling mechanism works independently of the source. For instance, an application targeted for a high-density screen may have bitmaps only in the res/drawable-hdpi/ directory. If one of the bitmaps is a 240x240 icon and is loaded on a medium-density screen, the resulting bitmap will measure 160x160.
I've done simple test: removed ldpi and mdpi density images, launched my app on mdpi skin and it scaled hdpi images nicely. It's not the best practice, but android's prescaling does its part as described.
Please read this for more details.
I wrote a tool which makes this all a lot easier to manage. Although the images are stored in separate directories - the tool shows all resolutions together, and lets you auto-scale from one to another.
you can get it here: http://hobbyistsoftware.com/AndroidImageViewer