Use of .9.png in Android assets

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.