Android Learning Series (4) -- App adaptive draw9patch without distortion background

Source: Internet
Author: User

If you are a human being, you need to be generous, haina baichuan, and smooth in your work. This reminds me that programming is also the case. You need to expand the code and adapt the interface.
This article is a must-have knowledge for android Developers. It is specially compiled and summarized for everyone. It is not perfect but useful.

1. The problem of Adaptive Background and non-Distortion
Making Adaptive Background images is a wide range of issues in UI development and also a problem that the interface designer is eager to solve. I believe we have a deep understanding of each other.
For example, the background image of the list must be set, but the height of the list changes with the list of data items. The background of the title bar can be automatically filled with either the landscape screen or the portrait screen with high or low resolution, and no distortion.
Based on past experience, we generally adopt the practice of cutting the graph first and then piecing it together. I would like to introduce this practice here. In fact, it is useful sometimes, however, it will be difficult to say, so I will not mention this non-important point. If you really want to introduce it, I will explain it in the reply.
Android has made a special. 9. PNG format to solve this problem.

2.9.PNG format.
I don't want to discuss the definition of PNG format too much here. However. 9. PNG is indeed a standard PNG format, but an additional 1px border is added in the outermost circle. This 1px border is used to define the scalable and static areas of the image. In particular, the cross part of the left and top borders is the Extensible part, and the unselected part is the static area part. The cross part of the right and bottom borders is the content part (in disguise, it is equivalent to defining an padding, which is similar to the padding function. I will introduce it separately later). This parameter is optional, for example.
In Android, images in the 9. PNG format have no background, and therefore can be stretched without distortion. For example, the system Button is a typical example.
In fact, both left and top, right and bottom divide the image into nine blocks (the four corners cannot be scaled, and the other four blocks can be scaled ), so it is called 9.png.

3. Use Draw9Patch. jar to create a 9. PNG Image to define the stretch area.
9. PNG format. Below we use the Draw9Patch provided by Google (run the Draw9Patch under the android-sdk-windowsools directory. bat. 9. PNG image.
Step 1: Prepare the image to be stretched.

A very small image. I want to fill in the content of the article in the middle of the background.
Step 2: Create a. 9. PNG Image.
Open Draw9Patch and drag the image into it, as shown below:

The default stretch is the overall stretch. In fact, we don't want to stretch the border part. Well, Let's define the stretch area by ourselves, for example:

Click the filepath to export content.9.png.
Step 3: Use the. 9. PNG Image in the layout file.
Create a project Draw9Patch. The default main Activity is Draw9PatchActivity. java:

Html # viewSource "commandName =" viewSource "highlighterId =" highlighter_804224 "> view sourceprint?
1 @Override
2 public void onCreate(Bundle savedInstanceState)
3 {
4     super.onCreate(savedInstanceState);
5     setContentView(R.layout.main);
6 }
Copy the content.9.png file to the/res/drawable folder and Open main. xml in the/res/layout directory. The declaration is as follows:
View sourceprint?
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="
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.