Android NinePatch Image Processing

Source: Internet
Author: User

Android NinePatch Image Processing

The NinePatch image ends with * .9.png. The difference between a NinePatch image and a normal image is that there is a border around it.

As shown in, the black line on the left represents the vertical area of the image, the black line on the top represents the horizontal area, and the black line on the right represents the vertical area of the content, the black lines below represent the horizontal area of the content. The lines on the right and bottom are optional, and the lines on the left and top cannot be omitted.

Using NinePatch images as the background, the background can be stretched (reduced) with the content ). Then, how can we edit a normal PNG image as a NinePatch image? The editor draw9patch. bat is provided in the Android SDK/tools directory and can be opened by double-clicking. It is easy to use and mainly has the following options:

Using NinePatch images as the background, the background can be stretched (reduced) with the content ). Then, how can we edit a normal PNG image as a NinePatch image? The editor draw9patch. bat is provided in the Android SDK/tools directory and can be opened by double-clicking. It is easy to use and mainly has the following options:

  • ? Zoom: Used to Zoom in the left-side editing area.
  • ? Patch scale: Used to scale the preview area on the right
  • ? Show lock: displays the uneditable area when the mouse is in the image area.
  • ? Show patches: displays the stretched area of the image in the editing area (marked in pink)
  • ? Show content: displays the content area of an image in the preview area (marked in light purple)
  • ? Show bad patches, no matter how you scale the image, it is good. (In practice, we found that the NinePatch editor is based on the color value of the image to identify whether it is a bad patch. You don't need to consider this setting if the color difference is not too large .)

    Example:

    NinePatch is a useful PNG image format, which can be scaled with the text size in a specific area. As follows:


     

    As you can see, the middle area of the background image scales with the text size. The background image is a NinePatch image. NinePatch images can be created using the draw9patch tool that comes with android. This tool is located in the tools directory of the SDK installation path. Run the tool, and then click "File"-> "open 9-path" to open an original NinePatch image. Draw lines on the top and left sides of the canvas to specify the zoom area,
    Select Show patches to display the area in green.
    It is a fixed size area, and red is the zoom area. Text is placed in red.
    Region. After the preparation, click "File "? Save 9-path
    Image. The draw9patch tool automatically adds the * .9.png suffix to the image.
    Copy the created image to the res/drawable directory, and then
    Write code. As follows:

     

     

     

    2: Use

     

    In the development of the upload UI, if the background size is different, you usually need to create an image for each size, which is particularly evident in the button. Of course, we can also make a small horizontal duplicate, or vertical. In android, when there is an nine patch image (ending with 9.png) to solve the difference in the background size, only one background image is used.

    Use the built-in tools \ draw9patch. bat to open a png image. We can draw a black line with pixel width in the outermost space of the png image. On the left is the editing area. In the picture on the left, the height of the black line on the left determines the extended area during vertical pull. That is, when vertical stretch is performed, only the image in this area is stretched. Similarly, the black line length on the image determines the extended area when the horizontal pull is performed. The area on the right is vertical, vertical, water, and two directions. This tool provides the WYSIWYG nine patch png editing method.

    Save the edited png as 9.png to the res/drawabledirectory of the project directory. If your 9.png file does not contain a black line, eclipse will report an error.

    The following figure shows the effect. We can also understand why nine patch is called. It is equivalent to dividing a png image into nine parts, including four corners, four sides, and an intermediate area, the four corners are not raised, so the rounded corners can be maintained. The two flat and vertical edges only stretch horizontally and vertically, therefore, the side will not be grayed out, and only the area specified by the black line is stretched in the middle. The result is that the image is not displayed.

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.