Is there any way to use only two sets of drawables, one for xxxhdpi and xxhdpi and the other one for xhdpi/hdpi/mdpi/ldpi without having to create 6 drawable-{dpi} folders and duplicating the files.
You can.
Use SVG Images instead of PNG or normal extension pictures.
Then use the following link to convert the SVG to Vector.
Use vector as your drawable, your drawable (single) will support multiple devices.
Cheers!
Vector drawables are the stuff you mean. The limitation is that it is mostly used for drawing simple shapes.
You can sue it like this. Make a vector drawable for xxhdpi and it'll be used for all resolution sizes with suitable sizes automatically. This technique is currently being used to decrease apk sizes reasonably.
Related
It is common that we provides various densities drawables in the project
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
I was wondering, should we favor Vector drawable over multiple densities drawable?
I went through https://developer.android.com/training/multiscreen/screendensities#vector-graphics .
But, it doesn't mention when should we use multiple densities drawable, and when should we use Vector drawable. Is there any situation when Vector drawable is not favorable?
Vector drawables should always be favoured where it is possible to replace the image with a vector as it is completely scalable.
The only place where you would use an image with different densities is when the image cannot be represented by a vector drawable.
Vector drawables are natively supported in API 21 and up (Android 5) and you won't need to use any special library to be able to use them in your app. For example in the Image tag you can use the src attribute to load vectors just as you would use them to load bitmap images.
They can also be used in older versions of Android with the help of Google support library but there will be a big performance hit.
According to Android documentation:
An alternative to creating multiple density-specific versions of an image is to create just one vector graphic. Vector graphics create an image using XML to define paths and colors, instead of using pixel bitmaps. As such, vector graphics can scale to any size without scaling artifacts, though they're usually best for illustrations such as icons, not photographs.
So vector drawables are always a better choice then using multiple densities.
This documentation will help you further:
https://developer.android.com/studio/write/vector-asset-studio
https://developer.android.com/guide/topics/graphics/vector-drawable-resources
I made all the drawables for my android app, and it is in xxhdpi folder. How can I make sure that the app will downsize the drawables according to the resolution phone?
you can use this link
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html for converting your image to the 9 patch images and down load it in .rar format. Extract it and paste into resource folder.by using 9 patch images are chooses according to the resolution of the device..
and if you want to take the icons you can use vector drawable . it will help to reduce your app size and best for all resolution devices.
for taking Vector Drawable
Right click on Drawable --> New --> Vector Assest .
If you do not want to scale the drawables manually and put in the right folder (e.g. -mdpi, -ldpi, ...), then the best solution could be use SVG format. Read more about this topic here.
Another benefit of using SVGs is that your app will use less space on the device.
What is the proper/right resolution for Image to be put using src in ImageView to avoid stretching or unscaled images?
if you are using a single color of background, u can use 9Patch images. but if you are asking about icons then you need different images for different resolutions in drawable folders like drawable-hdpi for hdpi devices drawable-xxhdpi for xxhdpi devices.
for more detailshttp://developer.android.com/training/basics/supporting-devices/screens.html
To understand the image size check out the following link image size (drawable-hdpi/ldpi/mdpi/xhdpi) . To avoid stretching use "android:scaleType="fitcenter/fitxy/centercrop"" for more detail check this http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Actually it totally depends on your placeholder or your needs, rather than single image you can make different sizes of image for different dp like small,large,normal,x-large .
probably https://romannurik.github.io/AndroidAssetStudio will save your time or new Android studio 2's built in plugin.
More resource
http://developer.android.com/training/basics/supporting-devices/screens.html
I am creating an android application in which I want multiple screen support.For these I have used 9 patch images.
But my question is this whether using 9 patch images will be enough for different-2 density's devices or I will need to create different-2 9 patch images for varying densities(like mdpi,ldpi,hdpi).
Thanks in advance
I recently found out myself in the same situation so let me pitch in and expand on what has been said already...
Yes, 9-patch images will scale, that's what they exist for. But if you should use a 9-patch image for all screen densities, that depends on the image really. 9-patch images are more commonly used, for instance, buttons. You can have different sized buttons in your app and a 9-patch image will help deal with them, no matter how you size your button (as long as the 9-patch image is properly created).
But let's say your button design has some really round corners for the hdpi version. You create your 9-patch image without messing the corners but when you look at it in the ldpi version, you'll realize your corners are too big for that low resolution. For this situation, you'll need a different 9-patch image with less round corners, that look better on that resolution.
So, the final answer is, it really depends on your image. If you can create a 9-patch image that looks good in all densities, than fine, use it, as you only need one image to handle all densities. But if it doesn't look good, because of corners, gradients, or whatever, than you'll need one 9-patch image for each screen density.
Hopefully it's clearer now.
From documentation: nine patch
A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension .9.png, and saved into the res/drawable/ directory of your project.
the answer is no. you nine patch will scale between different screen size
Short answer is YES.
Check this:
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.
from Android Developer Official Doc
I have a texture.png which I would like to tile, to create a textured background for my window.
Will I run into any problems with different screen resolutions?
If so, what is the best way to support a textured background for multiple devices?
Should I have 1 image instead? How large should it be?
You should create 3 different versions of your texture and place them in the ldpi, mdpi and hdpi folders appropriately. You can find more on the different densities here. In order to create a background drawable, create an XML file like it is described here: http://developer.android.com/resources/articles/window-bg-speed.html
The attribute android:tileMode="repeat" will do the magic stuff for you. :-)