Button button1 = (button) findviewbyid (R. Id. bt );
Inputstream stream = NULL;
Try {
Stream = getassets (). Open ("button_compiled.9.png ");
} Catch (ioexception E1)
{
E1.printstacktrace ();
}
Bitmap bitmap = bitmapfactory. decodestream (Stream );
Byte [] chunk = bitmap. getninepatchchunk ();
Boolean bresult = ninepatch. isninepatchchunk (chunk );
Ninepatchdrawable patchy = new ninepatchdrawable (bitmap, Chunk, new rect (), null );
Button1.setbackgrounddrawable (patchy );
:
The .9.png image used in assetsmust be compiled. Otherwise, it cannot be used normally. For example, the second button in is distorted.
Compilation .9.png
.9.png can be created using the draw9patch. Bat tool. This type of PNG can be opened using the image browsing tool to see that there are black pixels around the image. This is drawn using the draw9patch. Bat tool. After Android compilation, we extract .9.png from the APK package. In this case, we can use the image browsing tool to open it and then we can see that there is a black pixel around the image that disappears. The edited .9.png.
Compilation .9.png: Images
Create a Java project, put Android. jar and androidmanifest. XML in the project directory, and create an image directory Res/drawable
Projects such:
Use the aapt command:
Aapt p-m e: \ workspace \ compileninepatch \ androidmanifest. XML-s e: \ workspace \ compileninepatch \ res-I E: \ workspace \ compileninepatch \ android. jar-f c: \ Users \ liuyt \ Desktop \ temp.zip
Generate the compiled zip package to the desktop
After decompression, you can see the compiled .9.png