Android Development Getting Started Guide--Mobile phone adaptive draw9patch background setting specific steps

Source: Internet
Author: User

First, "Android mobile phone adaptive draw9patch true background" practical problems

Some time ago, to the Yangtze River to play a trip, took a lot of photos, but are mobile phone shots, the normal size is not clear to see the wife's face, the uncomfortable start to enlarge and small, but a certain degree of distortion of the picture. I wonder if you've all met this kind of situation? In fact, as a program developer, I am very aware that this mobile phone adaptive draw9patch not really good background, today may wish to solve the practical problems for everyone.

first of all, the background adaptive and the existence of the real problem has already given us an example, I hope that we are more in the life of observation, encountered any Android app problems, can add QQ Group: 175229978 to Exchange. Making an adaptive background image is a wide-ranging issue with UI development , such as a list of background images, but the height of the list changes with the list data items, the background of the title bar, whether horizontal or vertical, high resolution or low resolution, can be filled automatically, And not distortion and so on background problems.

Tanzhou Android Research and Development department in this case, specifically to create a convenient way---. 9.PNG format to solve this problem. the definition of PNG format is not discussed here. However, the 9.PNG is a standard PNG format, but the extra 1px border is added to the outermost circle, and this 1px border is used to define the extensible and static invariant areas of the image. In particular, the cross-section of the left and top borders is an extruded part, and the unselected part is the static area section. The cross-section of right and bottom borders is the content part (disguised equivalent definition to see an inner margin, the spirit of the padding function, the following will be described separately), this parameter is optional.

Images with 9.PNG format in Android do not have backgrounds, so you can customize the stretch without distortion, such as the system button is a typical example.
In fact, whether it is left and top, or right and bottom are to divide the picture into 9 pieces (corner Four is not scalable, the other four is allowed to scale), so called 9. Png.

Second, "Android mobile phone adaptive draw9patch background setting specific steps" key knowledge:

Already learned in the previous 9. PNG format works, below we use Google provided Draw9patch (run Android-sdk-windows\tools directory under the Draw9patch.bat) to make. 9.PNG images.
1: Prepare the picture to be stretched.
A very small picture, I would like to use this as the background, the middle part of the content of filling the article.
2: Make. 9.PNG picture.
Open the Draw9patch and drag the picture in, as follows:

The default stretch is the overall stretch, in fact the border part we do not want to stretch, OK, we ourselves to define the stretching area, such as:

Then click File and Export to Content.9.png.
3: Use the created. 9.PNG picture in the layout file.
New Project Draw9patch, the default main activity is Draw9patchactivity.java:

?

1 2 3 4 5 6 @Override Span style= "font-family: ' Microsoft Yahei ';" >public void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview (R.layout.main);

We copy the Content.9.png file to the/res/drawable folder and open the Main.xml under the/res/layout directory, stating the following:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?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"android:background= "#777"android:padding= "8dip"><textviewandroid:layout_width= "Fill_parent"android:layout_height= "Wrap_content"android:text= "Body: A ninepatchdrawable graphic is a stretchable bitmap image."android:background= "@drawable/content"android:textcolor= "#000"/></LinearLayout>

,

We modify the text,

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?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"android:background= "#777"android:padding= "8dip"><textviewandroid:layout_width= "Fill_parent"android:layout_height= "Wrap_content"android:text= "Body: A ninepatchdrawable graphic is a stretchable bitmap image, which Android would automatically resize to accommodate the contents of the View in which you have placed it as the background. A Ninepatch drawable is a standard PNG image this includes an extra 1-pixel-wide border. "android:background= "@drawable/content"android:textcolor= "#000"/></LinearLayout>

,

As you can see, the border is very clear. is not using the. 9.PNG contrast chart, and it is not the effect we want:

So far, we have basically made. 9.PNG Pictures. For the comprehensiveness and depth of the knowledge system, we continue.

4. Use Draw9patch.jar to create a definition content area for 9.PNG images.
Do you feel that the text and margins are too close, so we use the lines of right and bottom to define the content area to increase the padding.

we define a very small content area, and the rest of it automatically acts as a border, making the padding very large, at first, in order to increase the internal margin, it is easy to inertial thinking, in <TextView> affirmed android:padding= "10dip" and the like, I am here to advise friends do not do so, one is you will not be able to predict your display, two is this is more confusing, Because setting the content area is OK padding. (setting padding through the content area is more elegant and concise than defining padding in the layout XML!) )

5. Advanced techniques for making. 9.PNG.
For beginners Draw9patch, this can be considered a high-level technique, that is: the stretching area, can not be continuous, can be more than one piece, and is proportional to the length of the custom border line.
Direct Description:

How to handle 9.PNG images in 6.SDK.
SDK specifically for the definition and processing of 9.PNG, here we just do a simple process analysis, bitmap in reading the image stream data, will judge the image of the Ninepatchchunk (9Patch data block), if Ninepatchchunk is not empty, Then the ninepatchdrawable,ninepatchdrawable will be handed over to ninepatch for processing:

?

1 2 3 setninepatchstate (New Ninepatchstate(new Ninepatch (Bitmap, Bitmap.getninepatchchunk (), "XML 9-patch"), padding, dither), R);

Ninepatch the test succeeds, the local method is called and the final picture is drawn:

?

1 2 3 nativedraw (canvas.mnativecanvas, location, < span> Span style= "font-family: ' Microsoft Yahei ';" >canvas.mdensity, mbitmap.mdensity);

A large number of applications in the 7.android system are 9. PNG pictures.
by unzipping any ROM, find the inside of the framework_res.apk, which has a large number of 9. PNG format files, are widely used, such as common are:
Button:
Unlock:
Drop-down box:
Title bar:
Toast:
There are search, keyboard, zoom out control, time, and so on, I do not enumerate.

8. Finally some application cases, I hope you will see not too unfamiliar:
Drop-down button, article Head background, System head background, etc. will be used, in fact, the development of Android is very good things, we often see the realization of the time when looking for a girlfriend can also blow a great ah, haha ...

In fact, Tanzhou Android Development has a lot of similar cases, I hope you can join the study, more to QQ group: 175229978 Download Learning.

Android Development Getting Started Guide--Mobile phone adaptive draw9patch background setting specific steps

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.