Unable to understand the drawable structure inside res folder - android

I am newbie as far as android application development is considered.
I am little bit confused about drawable folders.
In the tutorials available on the android developer site they have mentioned about the drawable folder. But when I create android 2.3.3 application I see three folders drawable-hdpi, drawable-mdpi and drawable-ldpi.
I know that this for screen resolutions. If I have an image as a resource I have to create equivalent three images with different sizes and put these images into these three folders.
But for example, I am working on a localization (German) with images. Should I create folder drawable-hdpi-de-rDE similarly drawable-mdpi-de-rDE and drawable-ldpi-de-rDE or should drawable-de-rDE be sufficient?
If drawable-de-rDE is sufficient what will happen if it is run in mdpi or ldpi?
Please clear my doubts regarding this.
Thanks
Pankaj

Should i create folder
drawable-hdpi-de-rDE similarly
drawable-mdpi-de-rDE &
drawable-ldpi-de-rDE
If you want to make your pictures (at the right sizes) yourself, yes, you'll have to create directories that are specific to both the culture and pixel density. However, the culture should come before the dpi, or the project will not compile. For example, the following would compile correctly:
drawable-de-rDE-hdpi
drawable-de-rDE-mdpi
drawable-de-rDE-ldpi
should drawable-de-rDE will be
sufficient?
If you don't feel the need to draw your pictures at the right scale yourself, that should work : android will scale the images to the right size, as needed.
But note that you might get better graphics doing that scaling yourself -- and, instead of just scale up/down, you could re-think your images a little bit, so they fit better in different sizes.

http://developer.android.com/guide/topics/resources/localization.html

Related

Android - Supporting Different Screens

I'm programming for a little game in android, but i'm stuck at understanding how the scaling of drawables work or should be done.
I'm drawing a Tilemap in a View on a Canvas with Canvas.drawBitmap()
Main problem is that i don't know how i should manage bitmaps to occupy the same space independent of screen resolution.
My Tile is 64x64 png file and it's placed in drawables folder. Then if i want it to occupy the same space in a xxhdpi screen what should i do?
Should i place in drawables-xxhdpi folder the same tile but as a 192x192 file? Or should i convert px to dps for the drawBitmap() method? Or is there something else?
I'm getting really confused trying out things, and i see myself not getting anywhere.
Thanks in advance!
You're right. You should keep a bitmap file with different resolutions in different drawables folder. That allows the application to fetch the required files automatically on the basis of the size of device the user uses.

Mipmap drawables for icons

Since Android 4.3 (Jelly Bean) we can now make use of the res/mipmap folders to store "mipmap" images.
For example, Chrome for Android stores its icons in these folders instead of the more normal res/drawable folders.
How are these mipmap images different from the other familiar drawable images?
I see that in my manifest, we use the #mipmap/ qualifier, instead of #drawable/, which makes sense given the resource folder name:
<activity
android:name=".MipmapDemp"
android:icon="#mipmap/ic_launcher" />
References:
The Android 4.3 APIs document has the following to say:
Using a mipmap as the source for your bitmap or drawable is a simple
way to provide a quality image and various image scales, which can be
particularly useful if you expect your image to be scaled during an
animation.
Android 4.2 (API level 17) added support for mipmaps in the Bitmap
class—Android swaps the mip images in your Bitmap when you've supplied
a mipmap source and have enabled setHasMipMap(). Now in Android 4.3,
you can enable mipmaps for a BitmapDrawable object as well, by
providing a mipmap asset and setting the android:mipMap attribute in a
bitmap resource file or by calling hasMipMap().
I don't see anything in there that helps me to understand.
XML Bitmap resources have an android:mipMap property:
Boolean. Enables or disables the mipmap hint. See setHasMipMap() for
more information. Default value is false.
This does not apply to launcher icons as far as I can see.
The question was raised on Google Groups (The purpose of resource name "mipmap"?!), to which Romain Guy replied:
It's useful to provide an image at a larger resolution that would
normally be computed (for instance, on an mdpi device, Launcher might
want the larger hdpi icon to display large app shortcuts.)
I feel like this almost makes sense of it, but not quite.
I'm still inclined to go with Randy Sugianto's follow up:
What are the advantages of this? Is there any guide how to use
mipmaps, probably for better launcher icons?
Of course, Wikipedia has a page for "Mipmap", which refers to an older technique invented in 1983, that I can't quite relate to the current Android implementation.
Should we be storing all our app icons in res/mipmap folders these days, and what are the guidelines for these mipmap images?
Update #1
Here's a blog post that tries to explain it a bit.
Mipmapping for drawables in Android 4.3
But the image used in that blog post shows what looks like one file with many logos in it. This is not what I see in Chrome's mipmap folder.
Chrome's mipmap-hdpi folder contains three images. One is the Chrome logo, on its own.
Strangely, it is 72x72, not 48x48 which I would expect to see.
Perhaps that is all there is to this - we just need to keep bigger icons in the mipmap folders?
Update #2
The Android Developers Blog post of 23/10/2014 again confirms the idea of using the mipmap folders for application icons:
Getting Your Apps Ready for Nexus 6 and Nexus 9
When talking about the Nexus 6 screen density, the author writes:
It’s best practice to place your app icons in mipmap- folders (not the
drawable- folders) because they are used at resolutions different from
the device’s current density. For example, an xxxhdpi app icon can be
used on the launcher for an xxhdpi device.
Update #3
Note that Android Studio creates the ic_launcher.png icons in the mipmap... folders rather than the drawable... folders that Eclipse used to create them in.
There are two distinct uses of mipmaps:
For launcher icons when building density specific APKs. Some developers build separate APKs for every density, to keep the APK size down. However some launchers (shipped with some devices, or available on the Play Store) use larger icon sizes than the standard 48dp. Launchers use getDrawableForDensity and scale down if needed, rather than up, so the icons are high quality. For example on an hdpi tablet the launcher might load the xhdpi icon. By placing your launcher icon in the mipmap-xhdpi directory, it will not be stripped the way a drawable-xhdpi directory is when building an APK for hdpi devices. If you're building a single APK for all devices, then this doesn't really matter as the launcher can access the drawable resources for the desired density.
The actual mipmap API from 4.3. I haven't used this and am not familiar with it. It's not used by the Android Open Source Project launchers and I'm not aware of any other launcher using.
It seems Google have updated their docs since all these answers, so hopefully this will help someone else in future :) Just came across this question myself, while creating a new (new new) project.
TL;DR: drawables may be stripped out as part of dp-specific resource optimisation. Mipmaps will not be stripped.
Different home screen launcher apps on different devices show app launcher icons at various resolutions. When app resource optimization techniques remove resources for unused screen densities, launcher icons can wind up looking fuzzy because the launcher app has to upscale a lower-resolution icon for display. To avoid these display issues, apps should use the mipmap/ resource folders for launcher icons. The Android system preserves these resources regardless of density stripping, and ensures that launcher apps can pick icons with the best resolution for display.
(from http://developer.android.com/tools/projects/index.html#mipmap)
How are these mipmap images different from the other familiar drawable images?
Here is my two cents in trying to explain the difference. There are two cases you deal with when working with images in Android:
You want to load an image for your device density and you are going to use it "as is", without changing its actual size. In this case you should work with drawables and Android will give you the best fitting image.
You want to load an image for your device density, but this image is going to be scaled up or down. For instance this is needed when you want to show a bigger launcher icon, or you have an animation, which increases image's size. In such cases, to ensure best image quality, you should put your image into mipmap folder. What Android will do is, it will try to pick up the image from a higher density bucket instead of scaling it up. This will increase sharpness (quality) of the image.
Thus, the rule of thumb to decide where to put your image into would be:
Launcher icons always go into mipmap folder.
Images, which are often scaled up (or extremely scaled down) and whose quality is critical for the app, go into mipmap folder as well.
All other images are usual drawables.
The Android implementation of mipmaps in 4.3 is exactly the technique from 1983 explained in the Wikipedia article :)
Each bitmap image of the mipmap set is a downsized duplicate of the
main texture, but at a certain reduced level of detail. Although the
main texture would still be used when the view is sufficient to render
it in full detail, the renderer will switch to a suitable mipmap image
(...) when the texture is viewed from a distance or at a small size.
Although this is described as a technique for 3D graphics (as it mentions "viewing from a distance"), it applies just as well to 2D (translated as "drawn is a smaller space", i.e. "downscaled").
For a concrete Android example, imagine you have a View with a certain background drawable (in particular, a BitmapDrawable). You now use an animation to scale it to 0.15 of its original size. Normally, this would require downscaling the background bitmap for each frame. This "extreme" downscaling, however, may produce visual artifacts.
You can, however, provide a mipmap, which means that the image is already pre-rendered for a few specific scales (let's say 1.0, 0.5, and 0.25). Whenever the animation "crosses" the 0.5 threshold, instead of continuing to downscale the original, 1.0-sized image, it will switch to the 0.5 image and downscale it, which should provide a better result. And so forth as the animation continues.
This is a bit theoretical, since it's actually done by the renderer. According to the source of the Bitmap class, it's just a hint, and the renderer may or may not honor it.
/**
* Set a hint for the renderer responsible for drawing this bitmap
* indicating that it should attempt to use mipmaps when this bitmap
* is drawn scaled down.
*
* If you know that you are going to draw this bitmap at less than
* 50% of its original size, you may be able to obtain a higher
* quality by turning this property on.
*
* Note that if the renderer respects this hint it might have to
* allocate extra memory to hold the mipmap levels for this bitmap.
*
* This property is only a suggestion that can be ignored by the
* renderer. It is not guaranteed to have any effect.
*
* #param hasMipMap indicates whether the renderer should attempt
* to use mipmaps
*
* #see #hasMipMap()
*/
public final void setHasMipMap(boolean hasMipMap) {
nativeSetHasMipMap(mNativeBitmap, hasMipMap);
}
I'm not quite sure why this would be especially suitable for application icons, though. Although Android on tablets, as well as some launchers (e.g. GEL), request an icon "one density higher" to show it bigger, this is supposed to be done using the regular mechanism (i.e. drawable-xxxhdpi, &c).
One thing I mentioned in another thread that is worth pointing out -- if you are building different versions of your app for different densities, you should know about the "mipmap" resource directory. This is exactly like "drawable" resources, except it does not participate in density stripping when creating the different apk targets.
https://plus.google.com/105051985738280261832/posts/QTA9McYan1L
Since I was looking for an clarifying answer to this to determine the right type for notification icons, I'd like to add this clear statement to the topic. It's from http://developer.android.com/tools/help/image-asset-studio.html#saving
Note: Launcher icon files reside in a different location from that of
other icons. They are located in the mipmap/ folder. All other icon
files reside in the drawable/ folder of your project.
There are two cases you deal with when working with images in Android:
You want to load an image for your device density and you are going to use it “as is”, without changing its actual size. In this case you
should work with drawables and Android will give you the best fitting
image.
You want to load an image for your device density, but this image is going to be scaled up or down. For instance this is needed when you
want to show a bigger launcher icon, or you have an animation, which
increases image’s size. In such cases, to ensure best image quality,
you should put your image into mipmap folder. What Android will do is,
it will try to pick up the image from a higher density bucket instead
of scaling it up.
SO
Thus, the rule of thumb to decide where to put your image into would
be:
Launcher icons always go into mipmap folder.
Images, which are often scaled up (or extremely scaled down) and whose quality is critical for the app, go into mipmap folder as
well.
All other images are usual drawables.
Citation from this article.
When building separate apks for different densities, drawable folders for other densities get stripped. This will make the icons appear blurry in devices that use launcher icons of higher density.
Since, mipmap folders do not get stripped, it’s always best to use them for including the launcher icons.
When we build separate APKs for different densities, for the APK of the particular density, the drawable folders for other densities get stripped. This will make the icons appear blurry on devices that use launcher icons of higher density. Since mipmap folders do not get stripped, it's always best to use them for including the launcher icons.
res/
mipmap-mdpi/ic_launcher.png (48x48 pixels)
mipmap-hdpi/ic_launcher.png (72x72)
mipmap-xhdpi/ic_launcher.png (96x96)
mipmap-xxhdpi/ic_launcher.png (144x144)
mipmap-xxxhdpi/ic_launcher.png (192x192)
MipMap for app icon for launcher
http://android-developers.blogspot.co.uk/2014/10/getting-your-apps-ready-for-nexus-6-and.html
https://androidbycode.wordpress.com/2015/02/14/goodbye-launcher-drawables-hello-mipmaps/
If you build an APK for a target screen resolution like HDPI, the Android asset packageing tool,AAPT,can strip out the drawables for other resolution you don’t need.But if it’s in the mipmap folder,then these assets will stay in the APK, regardless of the target resolution.
The understanding I have about mipmap is more or less like this:
When an image needs to be drawn, given we have different screen sizes are resolutions, some scaling will have to take part.
If you have an image that is ok for a low end cell phone, when you scale it to the size of a 10" tablet you have to "invent" pixels that don't actually exist. This is done with some interpolation algorithm. The more amount of pixels that have to be invented, the longer the process takes and quality starts to fail. Best quality is obtained with more complex algorithms that take longer (average of surrounding pixles vs copy the nearest pixel for example).
To reduce the number of pixels that have to be invented, with mipmap you provide different sizes/rsolutions of the same image, and the system will choose the nearest image to the resolution that has to be rendered and do the scaling from there. This should reduce the number of invented pixels saving resources to be used in calculating these pixels to provide a good quality image.
I read about this in an article explaining a performance problem in libgdx when scaling images:
http://www.badlogicgames.com/wordpress/?p=1403

Like layout-sw600dp,alternative for drawable

My question is straight forward and simple, Like there is layout-sw600dp,values-sw600dp, for layout and values respectively, what is there for drawable folder in relative to size of screen.
I read in various document that if we want to use any screen more than or equal to 7 inches we can use layout-sw600dp folder, and for any lesser screen size device, layout folder will be taken into consideration. So how we can make use of same concept for images also. Right now there are ldpi, mdpi, hdpi folder, but images are taken in accordance of screen density not with screen size.Now if I am developing an app which work both on device and phone, what should be my probable approach for images.
Thanks in advance, please guide me.
you can also use the same concept for drawable like drawable-sw600dp,drawable-sw600dp-mdpi etc. I have used this in my application .
I'm not 100% on this one, but I think you can also use the same convention with drawables. Try drawable-sw600dp-hdpi or something similar. Though I also do have a few questions you may want to answer before proceeding:
Why would you want a different image (not resolution) for a bigger screen size?
As you already know, a 7-inch device would be using the layout-sw600dp folder, so why not have a different name for a different drawable? That would look a lot cleaner, imo.

confusion about drawable-hdpi,drawable-mdpi,drawable-ldpi and drawable

I am on one really big project that was started sins the time of android 1.5.
This project evolved as android was evolving and at this point supports 2.3 and above.
Now I got this problem I have a lot of drawables in drawable-hdpi,drawable-mdpi,drawable-ldpi but also in drawable folder.
Generally the concept of this 3 folders(drawable-hdpi,drawable-mdpi,drawable-ldpi) it is clear to me but my confusion comes with the images in drawable folder.
for example I have an image named myimage.png and this image is present in drawable-hdpi,drawable-mdpi,drawable-ldpi folders with resolution 72x72,48x48,36x36 respectively but this image is also present in drawable folder.
My question is why ? when does the image from drawable folder is used ? is this image forgotten to be removed ? of it is used in some cases ? I have tried to remove this image and the application works just fine. I also have put just a green color with resolution 72x72 and this image is never drawn
so can someone clarify me the existence of the drawable folder ?
I know that if I do not have the dhpi,mdpi and ldpi folders then I need to put the images in drawable but what is the point of images in drawable when this 3 folders are present ?
Thanks
EDIT
one more question, what if I have image in drawable-hdpi but not in ldpi and not i mdpi. Let say we have phone with mdpi resolution density... What will happen ?
drawable folder is used for default images if the images are not found in the respective folders then it will look into the drawable folder.
I found this phrase in the doc:
For example, imagine you have an application icon, icon.png, and need unique version of it for different locales. However, two locales,
English-Canadian and French-Canadian, need to use the same version.
You might assume that you need to copy the same image into the
resource directory for both English-Canadian and French-Canadian, but
it's not true. Instead, you can save the image that's used for both as
icon_ca.png (any name other than icon.png) and put it in the default
res/drawable/ directory. Then create an icon.xml file in
res/drawable-en-rCA/ and res/drawable-fr-rCA/ that refers to the
icon_ca.png resource using the element. This allows you to
store just one version of the PNG file and two small XML files that
point to it. (An example XML file is shown below.)
Please refer this link for more clearity.
AFAIK you don't need drawable folder if you have all three types of folder (drawable-hdpi,drawable-mdpi,drawable-ldpi )with different resolution images.
As per I faced when you run the app on Android 1.5 and 2.0 if the images which is used in any layout is not available in folder 'drawable' then the project gives error "Resource not found" even if the image is present in other hdpi,mdpi or ldpi folder.
This is not about the densities there are other qualifiers as well, though in most of the cases we don't use them much. For list of qualifiers check here and here. You can use -finger -language ... etc qualifiers. In case you want to provide qualifier default resources[in this case drawable] you can opt for default drawable folder. Or many default folders without qualifiers added behind them
I think this page should help you understand better. Thank you for the question. I, myself, didn't know the answer. Basically you have 4 different versions and if no exactly matching version exists, the default one (without qualifier) is used.

Where to insert images in eclipse for android

I am new to Android and I need to use images in my XML file.
A tutorial says that I have to place them in drawable directory, but I can't find it as I find drawable-hdpi, etc.
drawable folder is divided into into three part according to device screen size h- high, M- Medium, L- Low because in android different size of device available in the market and android device screen divide into three type h,m,l based on density specific according to device size android pick the image from specific drawable folder h ,m ,l if you dont want to density specification in your application then add new folder with the name of drawable.
I hope it is more use full to you.
You can create the drawable folder yourself by right-clicking "res" -> "New" -> "Folder" and naming it drawable.If you do not need your images to be density-specific, you can put your images there.
you can create your own drawable folder in res directory. But remember keep the images in that folder which are common for all screen size devices. drwable-hdpi means this directory contained the images will be loaded when the device has higher dpi. similarly drawable-mdpi and drawable-ldpi are there.
Those which you found are drawable folders.. Insert the images in all three of them. So that at time of change in definition of screen images can be changed accordingly. For now, Insert same image in all three of them.
You can create your own drawable folder. But its good to use these at first then when you run your application on device you will come to know the difference.
drawable-hdpi drawable-mdpi etc are the different type of drawables . you can keep your images in these folder (any one at the initial level).
But they have some diffrence according to the resolution of the screen & density of android device. Further you can check the diffrence between them and keep the images as per need.
see this for more details: Explain the difference between drawable, drawable-ldpi, drawable-mdpi and drawable-hdpi
and Supporting Multiple Screens
you can create a drawable folder in the /res folder of your project and put your images there.
drawable-hdpi(mdpi/ldpi) are used separate different resources for different type of screen. take a look here to know more about multiple screen handling
I realize that this question is rather dated, but...it came up when I Googled the issue of inserting images into an Eclipse Android project, so....
Actually, those folders are mipmaps and they are used by the graphics subsystem to provide seamless zooming, as well. I would suggest creating proper mipmaps using an editor, as opposed to providing only one resolution choice.

Categories

Resources