Android 9 patch (very good, quite comprehensive: Turn)

Source: Internet
Author: User
Tags xml attribute

The following are two reference articles!

Note: Let's clarify these two different lines. The lines on the left and top 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.

The key is to learn how to use ninepatch, some precautions, and how to use it in projects.

 

 

1: Introduction

Reference: http://blog.sina.com.cn/s/blog_5033827f0100r4dm.html

The ninepatch image ends with * .9.png. The difference between a ninepatch image and a normal image is that there is an extra border around it (as shown in ):

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.

 

 


Ninepatch is a "extensible" PNG neural program. Ninepatch example is used to create a picture of "printable text size", for example, interval 1.


Tip 1: The text background can be highlighted in the text size

Ninepatch is a very useful batch chip format. It can be used as the "background batch" of widgets 」. For example, in the example of Example 1, the program design is as follows:

  • The text part uses the textview component.
  • Use the XML Attribute of textview to set the text size.
  • Use the XML Attribute of textview to set a background
  • Using ninepatch as the background watermark, the background watermark can be hashed in the text size.

First, the first job is "creating as ninepatch example". The method is as follows.

Step 1. Upload a batch of original PNG images, such as limit 2.


Protocol 2: original png1_png (arrow.png)

Step 2. Activate the draw9patch tool provided by Android, and directly upload the draw9patch line under Android SDK tools/projects, such as example 3.


Release 3: The draw9patch tool provided by the android SDK (Click here to see the original release)

Step 3. Open the original PNG file, such as Example 4.


Release 4: Start your initial failover attempts (Click here to see the original release)

How to publish ninepatch logs

The indexing method of ninepatch logs is "two-condition", and two "black lines" are displayed on the top and left of the original logs 」, the cross-region of the blacklist is the "extensible" region. The following is the pink region of the region.


Option 5: Define extended area

"Extensible area" is the area in which the android framework uses text. In short sentences, text (textview) is only placed in the pink color area, in addition, the original principle is "the central point of the primary region", that is, the upper, lower, and left are also in the middle. Non-"extensible Area", that is, the highlighted area, does not contain the text size. Therefore:

1. The colored area is a fixed area.
2. The pink-colored areas are eXtensible areas and text fields.

How did the "two black lines" appear in the comment? The method is as follows.

Step 4. Adjust the zoom ratio of the mobile device so that the size of the Spotted Horse can be reduced, such as limit 6.


Limit 6: Percentage

Step 7:

Banma County is a region used to create a blacklist. How can this problem be solved? Click the mouse and then click the mouse. How can I remove a hacker? Hold down SHIFT and try again. This is not easy, so if you don't want to intentionally tune your mouse skills and test your eyesight, you can use the zoom function to help the hacker.

Check "show patches" and select "Examples" to show the pink color areas, such as area 7.


7: indicates the Extensible area.

In the right pane of draw9patch, the labels are displayed.

Step 8: ninepatch completed successfully

After the ninepatch logs are saved, draw9patch automatically saves the secondary names of these logs as * .9.png. After ninepatch has been completed, you can start the program.

 

Start programming: helloninepatch

The example of helloninepatch is as follows.

Step 1. Create a new Android example named helloninepatch.

Step 2. Export arrow.9.png to RES/drawable in helloninepatch cases. For example, lifecycle 1.


Release 1: arrow.9.png is added to RES/drawable Resource

Step 3. Modify the UI (RES/layout/Main. XML) and design the faces 2 in the previous tutorial (#30. The content of Main. XML is as follows.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="small world"
android:textSize="12sp"
android:background="@drawable/arrow"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="big world"
android:textSize="24sp"
android:background="@drawable/arrow"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="super world"
android:textSize="48sp"
android:background="@drawable/arrow"
/>
</LinearLayout>

In this case, place the button component on the UI and set the text and size on the button. According to the preset "Android: background", we set the background of the button to "@ drawable/arrow 」, that is to say, "The Arrow under the drawable Resource (under the drawable/Project) has been found, and the androidframework has been found in the arrow.9.png tutorial.

Because arrow.9.png is a ninepatch variable, the text size on the button is extended.

Step 4: complete hellninepatch

You do not need to make any changes to the program supervisor. Just click helloninepatch testing.

 

Try again!

 

 

1. What is "9 sister" (9 patch )?
It is a tool for processing PNG images. It can generate a "* .9.png" image for us;
2. What is "* .9.png "?
The so-called "* .9.png" is a special image format supported by Android OS. It can be used for partial stretching. This image has been specially processed by "9 Sisters, if it is not processed, the direct use of the PNG image will be distorted and the stretching will not work properly.
3. What is its purpose?
When it comes to usage, I have read related articles on the Internet for PNG images in this special format, but they all use an adaptive button for example! (For example) Clear plagiarism ..--,
(We ignore this instance directly. In the future, I will instill examples in the game)
In this example, when the font size on the button changes, the PNG Image under the text will automatically adapt to the text.
This seems to indicate that Android apps often use some components;
4. How can I use it in the game? Under what conditions?
.... Of course, as a game player, I must use "9 Sisters" in our game. Otherwise, I would not have studied it in vain. After thinking about it, I suddenly thought of some situations, and found that "9 Sisters" really occupy a certain amount of weight in game development! Next, let's familiarize ourselves with the "9 Mei" tool, and then let us give you an example to describe its usage. After all, there is a picture of the truth ~
Start 9 Sisters:
In your android SDK path X: \ Android SDK \ tools, you will find a [draw9patch. Bat]. That's right. This is ninepatch, the official name;

Prompt to import a PNG image, and then enter the "9 Mei" operation interface (for example): (figure 1)

Column ①: the area around the stretch area is displayed with a red border, which may cause deformation of the stretched image. If this content is completely eliminated, the image is not deformed after being stretched, that is to say, no matter how you scale the image to display it, 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. In addition, you only need to consider this setting if the color difference is not too large .)
Sequence 2: The region is the imported image and the operable region.
Column ③: Here, Zoom: the bar is used to zoom in and out the imported graph. Here, zoom in and out is only used to make it easier for users to operate. After all, the operation on the pixel is eye-catching, the following Patch scale is a preview operation after stretching of the three forms in Area 4 of the sequence. You can see the effect of stretching the image after the operation.
Sequence 4: area from top to bottom: vertical stretch effect preview, horizontal stretch effect preview, and overall stretch effect Preview
Sequence 5: If you select this option, a lock chart is displayed when you place your cursor in area 2 and the current position is not available;
Sequence 6: select this option. In area 4, you will see the relative position and effect of the currently operated pixels in the stretch preview image.
Sequence 7: display the stretched area of the image in the editing area;
Operation:
Click the left mouse button to select the desired pixel; Shift + left mouse button to cancel the current pixel.
Operation region:

We can see that the imported PNG Image has an additional pixel by default, that is, This pixel is our operable area. However, the lower-right and lower-right operational areas are optional, so ignore them. Pay attention to the left and top operation areas;
A row of pixels in the top operation area, indicating the point of horizontal stretching;
A row of pixels in the left operation area, indicating the point of vertical stretching;
My operations on images:

 

From: http://meiyitianabc.blog.163.com/blog/static/10502212720115354948909/

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.