9-patch - 1px border is blurred - android

I'm testing on HTC Desire which is a hdpi device. The problem is clear when you look at the image:
And here is how my 9-patch (made with standard 9-patch tool) looks (zoomed):
The left image is my photoshop file, and the right one is a screenshot from my device. As you can see there is a huge difference. Let's say that the shadow is not THAT important, but the blurred border looks bad.
Is my 9-patch png wrong? What can I do to achieve a nice crisp 1px solid border?

You should use 9-path tool for each png in different drawable folder. Sometimes problem is this the Android try to convert the png and blur the black lines, what cause later problem with properly displaying.

To circumvent this problem simply drop the 9 patch inside the drawable-no-dpi folder.
This allows to have one image for all densities. And android won't touch the image.
This is mostly what I do for my projects, unless I really want to have a difference between the densities. In that case, I create one 9-patch per density.
Alternate solution:
Shrink your top and left markers by 1 pixel on each side. This will ensure that scalable part does not overlap the grey border.

Related

Nine-patch versus Vector graphics

I am learning about Android UI and am unclear why people use nine-patch, when you could use vector graphics, because those are scalable without any pixel degradation. I am a beginner in Android, so I hope I am not missing anything here, but it seems like it would be easier to build vector graphics and use those. You would not need the special editor to build them.
Can anyone explain the advantages to using nine-patch over vector? (Don't just explain advantages of nine-patch, as that is already done on StackO., but rather the advantages that vector does not have). Because it seems like Android recommends nine-patch. Thanks.
In vector graphics all side are scale or stretch when we set it to any background whereas in 9-patch we can define which sides can scale or strech so at runtime only those side scale which we set it to scale in 9-patch tool.
from this
-> The advantage of using 9-patch images is that when using it as a background, for instance, the image won't stretch and loose proportions in different screen sizes. the center 'patch' will remain as is and the 'borders' patches will be stretched to fit the screen/view size.
let say you have this image.
and a button with fill parent width. if you set this image to button background it will scale completely and your image gets blur (mean t will expand to button width)and it will not look good. so what 9 - path tool do that you define that online scale some part of image let say if width is fill parent. dont scale whole image . let say we set that after t (in image). scale whole area, so t will not get blur. so this will make good your button.hope you got my point..:)
after making your image 9 patch and setting to button background. your button look like this.
instead of t(in image). whole area expand and fill the buttons width.
Imagine a button with rounded corners. How do you scale it? If you scale it only horizontally, you will have elliptic corners, which would be ugly.
This is what 9-patch is for.
i hate 9patches. i am quite attentive when doing my artwork. i don't even use photoshop. i go with illustrator.
i do everything right when exporting my artwork, i used to do my 9 patches with insane zoom on, maybe check afther that in photoshop for misplaced pixels...
** sad trumpet ** when put on a view, if i used some subtle round corners like 6px or 10px and a stroke everything looks awful at mdpi.
So I solved my problems by marrying the two. I wrote my own 9patch which uses vectors. :)
Everything looks like it's been touched by baby Jesus. Perfect corners, strokes and, best of all, you can use one asset for all the screen sizes, densities while, of course, no more transparent borders, wicked errors because 9patch won't stretch inward, so on and so forth.
I use vectors for icons too. While there sometimes are issues with various effects, these are minimal and easily avoided if you do some reading on how to avoid them.
Best of luck to all you guys!
This is a very basic example.
You can do whatever strikes your fancy. Because of the performance impact of svg's on an app, when first run [or when the user changes appearance options, i like to save the newly generated bitmaps as pngs, if possible.
You don't get any more "best of both worlds" than this.
You can define content are on a 9-patch image which means (for example) text will always be placed in that specific area, I think you cannot do this in Vector image. Android has built-in support for 9-path but for vectors you have to use a library.
9-patch rendering is easy to implement and efficient. If you have an image that can be scaled by stretching horizontal or vertical lines (e.g. buttons or rectangular icons), then use a 9-patch. If you have some icons that don't scale well, then create multiple versions at different resolutions and use Android's resource management to handle it. Both of these approaches are much easier and more efficient than vector graphics.
If you have large images or scenes to render, and you don't want to take up a ton of space with bitmaps, thats when you start thinking about vector graphics.

9patch (nine-patch) image does not stretch /scale properly

Dear Fellow Developers,
I've ran into the problem shown in the attached picture. We have been trying to tweak the dropdown menu's 9patch image provided by a graphics designer, but could not get it to work. The black stretch markers are not being hidden, and Android does not stretch it the way we would expect.
The device is a Nexus 7. We have tried several various image sizes, but none seemed to work. I am also attaching a copy of the 9patch.
What could be wrong?!
This happens:
Here is the 9patch
If the image has been created in a normal image editor rather than the android 9 patch tool, it could be just that its not done exactly right.
The image must have a 1px transparent border other than where the black lines are. Make sure its completely transparent and is only 1 px thick
Also I don't think you can have split lines (like you have down the left and right side of the image, I think they must be one solid line on each side)

drawable-nodpi stretching 9-patch

I'm trying to use a 9 patch image on a button in Android. I have it in the drawable-nodpi folder so it should use the same image for any resolution and not stretch it. If that's the case I'm totally mystified as to why this would happen:
Looks fine in the GUI in Eclipse
But in the simulator...
It seems to be stretching the image even though its in the drawable-nodpi, any idea why this is happening?
This is my image
If you look closely at the bottom of your image, it looks as if there is some drop shadow in the same line as the bottom 9-patch black line.
This will cause your 9-patch not to work.
Did you create the 9 patch in Android's 9 patch creator? This will make sure such things will not happen

Android 9Patch resizing in photoshop

I have some large icons, which are 9patch, and have to create a smaller set for a smaller screen. If I use something like photoshop to do the resize, are they still 9patch?
I just uploaded on code.google.com a tool that does the resizing for you :https://github.com/redwarp/9-Patch-Resizer
It's really simple : just drag n drop a xhdpi 9 patch, and it'll create the lower densities (ldpi, mdpi and hdpi).
Hope it helps !
Edit : moved to github
Nine-patch image is usual .png image with 1-pixel black border (link). I think if you may edit it PhotoShop. Note that border can't be broken.
Another way is: edit source image in PhotoShop and use Draw 9-patch tool to create 1-pixel border. It includes in standard Android SDK and it is free.
No, they are no more 9patch.
Say we resize 8x8 to 4x4, there 4 Pixels from the original made to 1 pixel(simplified). So you loose much information.
When resizing from 256x256 to 255x255 the loss of information is so small, that there is a good chance for the borders to stay 9patch-compatible.
The only method to resize these images(by very small factors) is "Nearest neighbor", because all the others use interpolation of colour-values with the surrounding pixels(the black border becomes gray).
In your case of app development, i would take the time and use "the other way" from "Dmitriy_Boichenko"'s answer and do the 9patch-thing from ground up, because graphics bugs are unsightly.
you can edit 9-patch inside photoshop of course.
In fact, you can edit in any editor supporting png.
It's a simple PNG file in the end.
There is only 2 very important rules:
File extension must be .9.png
The border around the image must be one pixel wide and contain only pure black or fully transparent pixel.
There is no need to create different density if you do it well, unless you don't like the look once it is scaled up or down.
Now since you mentioned icons, I wouldn't do Icons using 9-patch.
I would create 3 different density for each Icon.
Follow the guidelines here: http://developer.android.com/guide/practices/ui_guidelines/icon_design.html

Can we use Photoshop/GIMP to create black lines in nine-patch (9.png) images?

Android SDK tool draw9patch is soooo slow when I have to create a nine-patch from a full image (image for backgrounds) because it does not allow quick drawing of black line from top to bottom (like we are used in Photoshop or Gimp), but I have to paint each pixel. So waste of time. I think you know what I am talking about.
So, has anyone tried to draw black lines in Photoshop/GIMP taking care that you do not fill the end pixels? Will Android SDK treat them as a regular 9 PNG images?
9.png is just .png - You can use any of you favorite tool just 1px border must be transparent or filled with black and extension must be set to .9.png
There's this web tool - Simple Nine-patch Generator - which I find quite handy:
http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html
I use Better9patch tool then I test them SDK's 9patch tool. But if you create 9patch with 1px border you should retouch them with PS or etc.. tools

Categories

Resources