I have a an image of a text field that I want to put as the background of my text field. I made a nine slice image so I can change the size of the image but retain the integrity of the corners. I have this image in my drawable xhdpi and I want to use the same image in my hdpi and mdpi. I need to do this for multiple images in my app so I do not want to have to make a nine slice image for each pixel density. When I drag my nine slice into photo shop to change the size of the image the black lines above and to the sid of the text box are now in the image, not a pixel above/left of my image. Is there any way that I can scale my nine slice? Or do I have to make a nine slice for each pixel density?
Rule of Thumb: When you scale your images for each density, watch out for aliasing problems even when the height and width are in integer multiples.
That is: make a nine slice for each pixel density to be safe as your black dots/lines may get cut out.
If you want to use the same image for all of them, just create a new folder in res called drawable-nodpi, and it'll use that same image for all densities with no scaling. That's what I do for the majority of my 9-patches if the borders are pretty thin. If you have borders intended to be thick on an xhdpi device, keep in mind they'll appear much thicker on an mdpi device if you use the same one. If that's not an issue, go for the nodpi approach, in my opinion.
Scaling an image with Photoshop will either remove or add pixels to the image. When it does that it will change the scale/fill guides so they are no longer 1 pixel wide and solid black. To avoid this crop your image to remove the guides before you scale the image then add them back in.
Or you can convert 9-patch
images to the other resolutions using this Google 9-patch tool.
Related
how to use vector images which scale up and down according to the screen sizes?
I m using a vector image in my project but it is not scaling in big screens.
what you mean by being scaled ? maybe you are misunderstanding.
if you mean the image size would be changed in various screen sizes such thing wont happen. the image size is what you provide in your imageview (or any otherview using the image).
the only different between vector assets and normal asset is that vector asset are redrawn every time so you wont have a low quality image no matter how big it is and which device it is being shown in. so you wont need to save various size of image for various size of screens.
if you want to change the size of image just change the imageView height and width. you can use dp (density independant) unit.
you dont have to do anything for support vector images for diffrent screens its already scale for the relevant dp, Can you give more details about your problem when you say "it is not scaling in big screens?"
maybe you want the image on tablets will be bigger?
I have a 9-patch image with a 1px gray border around it as follows:
http://i.imgur.com/9DWQyCj.png
I was under the impression that I could stick this in any single density folder without issue. Now I know that doing so will result in tiny (less round) corners on high density screens, and large (more round) corners on low density screens.
Problem is, when I stick it in a single density folder, it seems like Android is scaling some of the non-scalable areas. For example, when I stick it in the hdpi folder and run it on an xhdpi or xxhdpi phone, the 1px gray border becomes 2 or 3 pixels wide.
Why is it scaling the border? Note that the problem is fixed when I distribute the image to every density folder. But I'm still confused because I thought 9-patch is only supposed to scale the areas in the bounds of the black lines, no matter which density folder it resides in.
The purpose of the 9-patch isn't to handle scaling; it's to handle multiple sized buttons or whatnot. The non-stretch portions of your 9-patch image will still be scaled according to the normal scaling rules. To make your image look right, you should provide prescaled versions and put them into the various density folders.
I am using a nine patch image as a drawable for a selector's item element.
The nine patch image is here:
The image that finally gets rendered is this (emulator & device screenshot, both device have resolutions > mdpi):
As you can see, it has been scaled outside the stretchable area (as defined in the 9 patch file), the darker green bar is much thicker as it should be.
Why is that and can it be avoided? The draw9patch tool scales this image correctly. Does it have to do with Android trying to preserve physical dimensions on high resolution devices?
Do you only provide a mdpi version of the image?
Android scales all images that aren't available in the correct pixel density. This happens before the image is scaled via 9patch.
This would mean that your image gets scaled up to fit hdpi and then scaled to fit the content.
To fix this, you have to provide hdpi and xhdpi versions of your image. 9patch isn't supposed to target different pixel densities and rather different content/screen sizes.
I checked out your 9 patch:
Why did you color your not used bounds white? It should be transparent.
Because of this it is not recognized as a 9Patch:
When 9patch png images are used, do we need to provide them in xhdpi, hdpi and mdpi format also or only one resolution is enough? Because either way they will resize on resolution... or am I thinking something wrong?
You should use different sizes, otherwise a border on a LDPI device would look much thicker than on an XHDPI device
For the Nine patch image only one resolution is enough because 9-patch image scales the image text according to the layouts...
Nine-patches are widely used for background kind of images: most typically it would be some sort of border and a fill. You should provide only one resolution of 9patch, Android would scale it appropriately.
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