Android nine patch image and button background

Source: Internet
Author: User

Ninepatchdrawable draws a scalable bitmap image. Android automatically adjusts the size to accommodate the displayed content. In one example, ninepatch is used as the background. With the standard Android button, the button must be scaled to accommodate characters with varying length.

Ninepatchdrawableis a standard PNG image with a quota of 1 RMB. Save it as .9.png and keep it in the Res/drawable directory of the project. If you extract the * .9.png file from the APK, note that it has removed the surrounding blank pixels and must be added during use.

This boundary is used to determine the scalable and static areas of an image. You can draw one or more black pixels on the left and above to point out the Scalable part (you can need a lot of scalable parts). The relative position is the same as that of the Scalable part, so the big part is always very big.

You can also draw an optional drawable area (valid, padding) on the Right and bottom of the image ). If your view object sets ninepath as the background and then specifies a special view font, it automatically scales all the text to adapt to the area designed based on the right and bottom lines (if any ), of course, the padding line is not included. Android can use the line on the left and the line above to define a drawable area.

Let's clarify the two different lines,Line on the left and top to define which pixels of the image can be copied During Scaling.The bottom line and the right line are used to define an image in a relative position, and the View content is placed in it.

AvailableTools \ draw9patch. batTool to edit * .9.png images. It can be seen that this tool intuitively shows the effects of images stretching up or down or left and right, and the position where the image content is displayed as the background.

For nine patchButton backgroundUsage:

 <Button     ID  =  "@ + ID/big" 
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"

Android: Text = "Text! "
Android: textsize = "30sp"
Android: Background="@ Drawable/my_button_background" />

And create an XML file (the content is as follows) in the drawable directory ),Android: Background="@ Drawable/(XML file name)"To display different backgrounds in different States.

 <? XML version = "1.0" encoding = "UTF-8" ?>  <  Selector   Xmlns : Android = Http://schemas.android.com/apk/res/android"  >      < Item   Android : State_focused = "True"   Android : State_pressed = "True"         Android : Drawable = "@ Drawable/button_focused"   />      <  Item   Android : State_focused = "False"   Android : State_pressed = "True"         Android : Drawable = "@ Drawable/button_focused"   />      <  Item   Android : State_focused = "True"         Android :Drawable = "@ Drawable/button_focused"   />      <  Item   Android : State_focused = "False"         Android : Drawable = "@ Drawable/button_defocused"   />  </  Selector  > 
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.