I've been reading about the drawable-XXdpi folders, and I created my icons in all sizes.
What I want to do now, is to put a background image on my main screen. And what my assumption is at this moment, is that I don't work with DP at this moment but with PX, because my background image needs to fill all the pixels.
When this assumption is correct, where do I put my created background images?
I've created one for 480*320, 800*400 to the largest 1600x2560.
In my assumption I can't put them in the drawable-XXdpi folders because those are DPI related, and I need to fill all the pixels.
You can put your drawable that is not specific for each dpi into drawable-nodpi folder.
But basically, as Der Golem describes, you should always use dp.
I don't work with DP at this moment but with PX ... You should always use DP.
To fill all the available space, just use android:background instead of android:src.
Note that the image will be stretched (if you don't make it a 9 patch - this will also be stretched, but in a "controlled manner").
To create a background with the exact pixel size is not a good practice.
You'd end up wit oversized images.
Better practices include (not mutually exclusive - they can be stacked by using a Layer-List):
stretchables (such as 9 patches)
tiles (seamlessly tileable pictures)
drawables (which are vectorial, like SVG files)
These ones include:
a - shapes
b - gradients
c - ...
SVG (through 3rd party libraries)
...
Also consider that "the exact pixel size" might be different from what you expect.
You have to take in account the StatusBar, the ActionBar/ToolBar, the MenuBar, ...
There is no separate folder architecture for putting px files. You need to use drawable-hdpi , drawable-mdpi, drawable-xhdpi, drawable-xxhdpi.
The approximate pixel values for different dps are like this:
ldpi = 600.00px x 360.00px
mdpi = 800.00px x 480.00px
hdpi = 1200.00px x 720.00px
xhdpi = 1600.00px x 960.00px
xxhdpi = 2400.00px x 1440.00px
Check these links for more details:
http://pixplicity.com/dp-px-converter/
http://developer.android.com/guide/practices/screens_support.html
Related
I realise that Android will try it's best if it does not find an icon in the required folder but I see someplaces they suggest all of the above and in others they don't include drawable ?
So should I populate drawable as well if all the others are filled with my tab icon images?
If you use a VectorDrawable, you don't need to add a Resource for every density (mdpi, xhdpi etc). VectorDrawable is supported since API 21 (Lollipop) or with Support Library (or AndroidX).
For simple types of images (usually icons), you can avoid creating separate images for each density by using vector graphics. Because vector graphics define the illustration with geometric line paths instead of pixels, they can be drawn at any size without scaling artifacts.
For images (PNG) on the other hand, you must add proper icons for every density because Android will try to scale the images (so they can proportionally occupy same area in all devices). When scaling, the image may become blurred reducing the quality of your UI.
To provide good graphical qualities on devices with different pixel densities, you should provide multiple versions of each bitmap in your app—one for each density bucket, at a corresponding resolution. Otherwise, Android must scale your bitmap so it occupies the same visible space on each screen, resulting in scaling artifacts such as blurring.
You can read more HERE and HERE
EDIT
Maybe, you don't need to duplicate ALL icons. A lot of factors can lead to different experiences such as using wrap_content or a specific dimension to control the icon size or even using a different scaleType in your ImageView. So, maybe, you can start by adding icons for xhdpi or xxhdpi folders only and check your screen in different screen (small display, large displays, low-resolution displays, high-resolution displays etc). Then, you can "duplicate" only the necessary icons... But if your project or APK size is relatively small, don't mind to duplicate the icons.
There's even some online tools to generate the assets for every density from a single PNG such Android Asset Studio website..
If you are adding all resolution drawable icon like :
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxdpi
then you need not to add any extra icons on drawable folder
because all the device resolution covers under the above drawable folders
So far, I always created one nine-patch image and place it inside /res/drawable-hdpi.
But I saw some large projects which have multiple nine-page images, scaled and placed inside multiple res/drawable-xxx directories. Is this the wrong approach? Nine-patch was supposed to stretch across all screens, regardless of its DPI.
Also, if I am right and only one nine-patch is to be used, what is its default location - drawable-hdpi, mdpi, or some other directory inside /res?
Well, it really depends on quality.
If your image is just a square border, it can be a 72 dpi low res image put into the drawable folder and it would be enough.
If your image has rounded corners or other fancy elements that have to be scaled properly, you could make a 480 dpi version and put it in the drawable-xxhdpi folder. This will scale down (don't even think of scaling up, because of stretching/pixellating) good enough in most cases.
If you want the best quality in scaling, then make a version for each dpi drawable folder.
If you go to http://developer.android.com/guide/topics/resources/drawable-resource.html, you can see it there (citation):
file location:
res/drawable/filename.9.png
The filename is used as the resource ID.
I am going to develop new application in Android. This application should only work in portrait (even for tablet). Also the UI and layout design should be similar on phones and tablet. We can't change the layout design for tablet as it has huge area to use. We have to stretch all the images to match phones. We can use nine patch. But I am little bit confused of using images in multiple drawables.
As per my analysis (may be wrong.. : ) ) the screens are divided into density and sizes. We can use the scaling ratio of 3:4:6:8. But this ratio is based on the density. But in my case I have to stretch the entire UI to fill the Tablet screen.
So what are the drawables that can be used for a app like this which can support multiple devices. And what are the screen sizes for which we have to design.
And this application needs nearly 100 layouts. So I am planning to maintain single layout and designing the layout using weight for each layout instead of using dimension.
Also if I used multiple APKs to support different screen size what are the drawables used to support
1. Small and Normal
2. Large
3. Xlarge
I just did something very similar. To stretch the app without creating new layouts I used dimensions set in XML
res/values/dimensions.xml
res/values-sw600dp/dimensions.xml -> 7+ inches
res/values-sw720dp/dimensions.xml -> 10+ inches
Dimensions are resources files:
<dimen name="default_padding">11dp</dimen>
You can increase the dimensions by about 30% in the 600 and 720 file.
Then simply used #dimen/default_padding in your layout and it will be scaled
Regarding images, either you make sure you have all your assets in all densities, or you set fixed size to you ImageView's and appropriate scaleType
Firstly, you do NOT want to create multiple APKs to support multiple screen densities. Android provides all of the framework you need to support everything within one build, you just need to create the right resource hierarchy drawables with your desired densities.
So what exactly do you need... based on your question the following:
portrait mode: you can specify this in each Activity declared in your AndroidManifest file using the following:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation" >
...
</activity>
NOTE: Per the Android docs, if you're targeting API >= 13, and you use the android:configChanges attribute you should also use the android:screenSize attribute to help specify size changes.
dimension sizes for various screens: as touched upon, this can also be handled in resources. This is your best way to use one common layout file but configure the layout for use on numerous devices. See http://developer.android.com/guide/topics/resources/more-resources.html#Dimension for how to use dimensions if you're unfamiliar
drawables: it sounds like this is the crux of your question. As you mentioned, using nine-patches will help you reduce your app footprint and fill in extra space (see here and here for more on nine-patches). The sizes you should support and the densities needed for those sizes are discussed in great detail in Android design docs, so much detail I could not even do it justice rehashing it here. I've provided links below to as many places as I could remember that this is discussed.
Good luck!
Here are links to Android design docs that you will find useful (some of which have been mentioned):
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
http://developer.android.com/training/multiscreen/index.html
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/design/style/devices-displays.html
In addition to the pixel density specific folders, you can specify screen-size specific folders
drawable/
drawable-large/
drawable-xlarge/
drawable-hdpi/
drawable-large-hdpi/
drawable-xlarge-hdpi/
drawable-xhdpi/
drawable-large-xhdpi/
drawable-xlarge-xhdpi/
So you could design scale appropriate graphics for the various screen sizes and densities. Please note that a give screen size category (e.g. "large") will only give you a rough idea as to the actual device pixel dimensions of the device, but you'll get good guidelines for min/max dp ranges.
For example, you might have a 100x100 image you want to display on phones (screen size "normal"), you'd create image assets at 100x100, 150x150, 200x200 for drawable, drawable-hdpi, drawable-xhdpi folders respectively. But on 7" tablets, i.e. "large" screen size devices, you might display this same image at 200x200, so your "drawable-large" folder assets would be 200x200, 300x300, 400x400, and on 10" tablets, i.e. "xlarge" screens, you might display the same image at 300x300, 450x450, 600x600, so these go in "drawable-xlarge-*" folders.
All the details are here:
http://developer.android.com/guide/practices/screens_support.html
First you need all the possible screen layouts
drawable
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi // phones like s4
drawable-xlarge
drawable-tvdpi // nexus 7 etc
drawable-xlarge-xhdpi //tablet like nexus 10
layout : for phone
layout-sw600dp
layout-sw720dp
then you need to put all use 9- patch images for buttons etc ... you can also make your custom drawable it would be easy and handy to work on ..Also you can take dpi for each screen by using switch and scale it the layout accordingly.
As, in one of my project I had used this technique for showing respective thing to each resolution device .
DisplayMetrics metrics = getResources().getDisplayMetrics();
int densityDpi = (int) metrics.density ;
switch (densityDpi) {
case (int) 1.5:
break;
case (int) 2: //1.75 will be 2 in INT.
break;
default:
break;
}
also keep all the values you are going to used for margin padding etc values-sw600dp for tvdpi tablet ,value-sw720dp for tablets
Last but not least keep all thing generic as much as you can and put it in drawable ..
I have seen some ppl who used background patterns of different dpi's and put it in respective drawable .. if there is such thing like pattern make your custom drawble and repeat it accordingly
that will save your time .. hope it may help you
In order to stretch all the images to match phones you can specify the image size using the sdp size unit. This size unit is relative to the screen size so it can fulfill your requirement.
so im working on my first app and i have a question about images
first off, theres 4 different drawable folders, do i just drop an image into one of them? what is the proper way of adding a resource image?
secondly, i know theres a bunch of different resolutions for phones. should i just use one that is say, 960x720dp? or is there another appropriate way of doing this? i want to add an image as a resource into my project, then use it as the background for my app layout...
The best way is to put 4 different versions of the image in the 4 folders . The simplest way to find out what resolution each folder must have is to use mdpi image as a reference and multiply other 3 like this:
ldpi = mdpi x 0.75
mdpi = this is the reference resolution
hdpi = mdpi x 1.5
xhdpi = mdpi x 2
Example: for an image that has 100 x 80 px as the base image, the other folder sould contain images with the following res:
ldpi = 75 x 60 px
mdpi = 100 x 80 px this is the reference image
hdpi = 150 x 120 px
hhdpi = 200 x 160 px
The best way is to create the biggest image in you image editor that you will need and make smaller variants from that. you can fint you own mathematical formula for the multiplication
If you don't want to put different variants for the image then let Android OS convert the images for you The Android OS will search the reference image in mdpi folder so make sure to put the image in this folder
More info here
You just need to paste your images into drawable folders.
To address different resoulutions, you can place different images into different drawable folders with same name, say for logo.jpg into drawable-ldpi might have image logo.jpg with resolution fit to ldpi, and drawable-mdpi have image logo.jpg with resoluiion fit to mdpi. or if any image must be same for all resolutions then place a single image into any of the drawable folder.
You can do this by adding just one images in any folder but this link of supporting multiple screens and resolution of images provide you the best way to do that. Just read that
I've got a final app design made i Photoshop where everything is measured in PX.
Now I realize that Android apps are using DP for font-sizes and other things.
Is there any way I can convert PX to DP ?
On a more practical note, you have a few choices in increasing work and fidelty:
Have your resources scaled for 160 dots per inch, put them in your res/drawable directory, and let the OS scale them to look right on the device.
Make two copies of your resources: one at 160 dots per inch in your res/drawable and one at 240 dots per inch in your res/drawable-hdpi directory. Let the OS scale for the exact device starting from a pretty close number.
Decide that you don't want any scaling and have raw pixels, so put your assets in the res/drawable-nodpi directory. This means that at 320x480 (pixel) graphic might be 2 inches by 3 inches on one phone but only 1 1/3 inches by 2 inches on another screen.
Specify the exact scaling strategy for your work, using the draw9patch tool. This can be very useful for keeping the corners of boxes from getting the "jaggies" from scaling and for making full screen graphics cope with different aspect ratios.
Make separate graphics for every device you care about and fall back on scalable graphics for the rest. You will need to outrun zealots waving style guides trying to convince you not to do it this way.
Oh, and as a gotcha, specify sp for your fonts, instead of dp or pt. A 10 point font would be a 22 sp font [ =10 * (160/72) or = number of points times 2.222]. sp units scale with user preferences for small, medium, or large fonts.
From this list of the dimension units supported by Android, here's a description of DP:
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
This means that the "conversion" between pixels and DP will not be consistent -- on some devices, the ratio might be 1DP = 160px, but it could theoretically be anything. This is all well and good when you're setting the width of a button to, say, 100dp (since it will get rendered dynamically), but it presents a problem when you have images which must have a fixed size.
Read this page on "Supporting Multiple Screens" -- Android has something called resource directory qualifiers, which let you create size- and density-specific versions of your image resources. For example, for low-density screens, you could create a smaller version of your image and place it in the drawable-ldpi directory (or drawable-hdpi for high-density screens).
tl;dr You can't practically "convert from PX to DP" (since the ratio is not fixed), but you can create multiple versions of your images and tell Android which to use with resource directory qualifiers.
A pixel is PX and the DP or DIP are device independent pixels. I don't think that you need to convert these. But you can use scalable 9patch images using the draw9patch tool from the android tools.
I have same issue but now got the Solution.
Why you not use the online convertor to see which dp or px you have to for different resolution of android device ??
See this link: this link which helps me a lot and also helps you.
Enjoy coding.
This is a online DP/PX Converter tools: http://labs.rampinteractive.co.uk/android_dp_px_calculator/