How to change 9patch image color Android?
I don't understand how to create a 9patch image, but I found an image that works on my snippet. The problem is that the color outside the border is not the background color. I tried to change the pixel color from the image to the background color, but the resulting image doesn't work anymore. This is an image that works but is in the wrong color:
http://i.stack.imgur.com/cJBfV.png
How do I change the color of the pixels outside the border, or how can I create a new 9patch image that looks like this?
source to share
you can use DrawableCompat
with supprot v4.
now, i will show you how to change the Toast color using this method. as you know the toast background is a 9patch Drawable named toast_frame.9.png
(you can find it in the sdk directory)
this code:
Toast toast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
toastView.findViewById(android.R.id.message);
View toastView = toast.getView();
Drawable toastBg = toastView.getBackground();
Drawable drawable = tintDrawable(toastBg, ColorStateList.valueOf(Color.RED));
toastView.setBackground(drawable);
toast.setView(toastView);
toast.show();
public Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}
if you want to know more, click here: http://www.race604.com/tint-drawable/
source to share
You can edit patch 9 using the Draw 9-patch tool that comes with the Android SDK. However, since your image already includes 9 stretchable patch areas, you can simply edit the colors in an image editor like GIMP or Photoshop. Make sure you rename the image using the extension .9.png
so that it is recognized as 9 patch images.
source to share
Nine-patch are regular png files. They are simply interpreted differently by any software capable of displaying nine patches. In other words, you can edit them with any png editor.
There are also some specialized tools (for example in android sdk / tools / draw9patch) which display the result in different sizes.
How are they interpreted then?
The first and last rows + first and last columns of pixels in the nine-patch image contain only black or transparent pixels (and those first / last rows / columns are not displayed).
(intersection) of black pixels at the left and top edges defines stretchable regions. (you can have more than one)
Black pixels (intersection) at the right and bottom edges define the content area. (you can only have one)
source to share