How to use Nine-patch images correctly - android

I want to create some kind of row image that looks like that
but I want it to scale with the screen size and density. So reading this http://developer.android.com/guide/developing/tools/draw9patch.html and this http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch I startet creating a Nine-patch image.
But when I import it and use it in my android project it looks like that
As you can see the little black line that was drawn with the draw 9 patch tool is still visible. Why?
Here is the 9.png image

Are you using the draw9patch tool that comes with the sdk? it is quite handy.
Anyway:
It is vital that every pixel except the black ones are completely
transparent.
The black pixels should be at the absolute top/bottom/left/right of
the image.
The image should be named filename.9.png.

I have found the best and the simplest answer to make 9-patch image.
This is best link to create 9-patch image for all the resolutions - XHDPI, HDPI, MDPI, LDPI in just one click.
Let me know if you have any queries, and do upvote it, if it was helpful to you.

If you need an image which consists of a border with rounded corners, I don't have to create a 9-patch.
All you need is an XML like this stored in the res/drawable directory
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="3.0dp" />
<stroke
android:width="2dp"
android:color="#color/green" />
</shape>
Read more about drawable resources

Related

9-patch with too big resolution doesn't work

I got this strange problem with a 9-patch in Android, which i want to use as a splash-screen. I use a build-in Android Studio plugin to create a 9-patch, but it doesnt't work properly with images with too high resolution.
This is a simplified version of what I do in generator (X is the symbol of black pixels):
X X
************X
X ************X
************X
****LOGO****X
************X
X ************X
************
XXXXXXXXXX
When i try to use it as a activity background it behaves like a normal bitmap, not like a 9-patch - it is displayed in original resolution and doesn't fit the screen. This is the code of XML file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/colorPrimary"/>
<item>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/patch_logo"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
I decided to test it as a background of a layout, in that case it was the FrameLayout where I load fragments with initial settings. This image shows two previews - on the left is used a normal png, 800x800. On the right side is a 9-patch file of the same image.
previews
As you can see, left image is just stretched to fit the layout. Right image, although in preview it looks like when i was using xml drawable as a background of activity, on a real device it is displayed exactly like using normal png - it is just stretched.
When I use 500x500 image 9-patch it is a little bit too big on preview, but on my device it looks good, just the resolution is too low.
All 9-patch files are stored in drawable folder, it is not specified for any density.
This is the logo I want to use logo
I was trying to find the solution all day. What am I doing wrong?

dimension of image background for android actionbar

I want to design an image background for my android app's action bar, but have no idea what dimension I have make so that it fits in different screen resolution.
Do I need to design the same image background separately for each screen densities?
I want to design it in photoshope is that a good option?
any idea Please !
I would suggest you use a gradient which you can store in the drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:startColor="#00445a"
android:endColor="#00699a"
android:angle="90"
android:type="linear"
/>
</shape>
This will prevent all the trouble of trying to adapt to screen sizes.
Create 3 .png versions of the image, for hdpi/xhdpi/xxhdpi densities.

match a nine-patch image with simple image

I want to put two images together (a nine-patch image with icon), So that icon size does not change.
I designed nine-patch image but i don't match these images together.
Is there anyone who can help me?
You can use the android layer list drawable. This allows to stack layers on top of each other. The first layer can hold the nine patch image and the second layer can hold the icon which wont be stretched.
This is how it would look like
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/wp_tabbuttonstrip"/>
<item>
<bitmap android:src="#drawable/wp_dashboard_icon"
android:gravity="center"/>
</item>
</layer-list>
You will need to specify the size of the view, wrap content might not give the desired results.

Creating Nine (9) Patch png files with Gradients

I want to create a nine patch button background with a gradient for my android app. I was wondering how gradients work. I would think that the os would have to figure out how to apply the gradient as the button stretched and I'm guessing that information (a graphic vector equation) is not available in the 9 patch file? So is there a solution for this problem? Also, what about dithering, etc. I have created a few nine-patch button backgrounds, but none of them have a gradient. yet. Thanks in advance.
Why do you want to use a 9-patch for that? 9-patches stretch an area of an image by copying/duplicating pixels. That will not work well with gradients. Have you considered using a GradientDrawable instead?
<shape android:shape="rectangle">
<gradient android:startColor="..." android:endColor="..." />
</shape>
See shape drawables.
As to your question about dithering, I didn't address that with my previous reply.
You'll have to enable dithering from your application code I'm afraid, as far as I know there is no way to do it from XML. You can use the setDither(true) call on your drawable, as documented here.
This will make for much nicer gradients, since it mitigates the banding artifacts you see on gradient images. I haven't tried this on shape drawables though, just PNG files.
Also, if you're targeting Gingerbread it may be worth reading Bitmap quality, banding and dithering. Apparently they snuck a change into 2.3 which addresses these issues.

Android gradient emboss effect

I am trying to achieve from code the following: (can't post images unfortunatelly)
A rectangle with rounded corners, with an emboss effect (the light comes from top left corner).
In the middle there is a circle engraved in the rectangle. Imagine a water surface, and a drop of water hits the surface. It creates a dent in the surface. That circle is also painted with some linear gradient.
The problem is I could only use the EmbossMaskFilter from Android to raise the surface, to make it closer to the user eye, but I don't know how to implement the opposite.
Anyone can help me with that?
Thank you very much.
Use a composite drawable, or drawables in layers.
To create a drawable with rounded corners and a gradient within, use something like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"/>
<gradient android:startColor="#color/gradientstart" android:endColor="#color/gradientend" android:angle="315"/>
</shape>
Create two such drawables and put them on top of each other to create the required effect.
Unless you use a pretty good number of layer-list items as a drawable you probably won't get the effect you are looking for easily with XML drawables. A better solution would be to create a 9-patch image. See how it works at draw9patch.com, which is a tool to create 9 patch images from a standard images.
NinePatch documentation: here.
In case, you still really want to use xml drawables you can still achieve the effect (although performance might take a hit) using a layer-list with multiple gradients stacked ontop of each other.

Categories

Resources