Android authoritative Programming Guide-notes (21st. XML drawable)

Source: Internet
Author: User
Tags transparent color xml reader home screen

In the Andorid world, everything to be drawn on the screen can be called drawable, such as abstract graphics, drawable subclasses, bitmap graphics, etc., we used to encapsulate the image of the bitmapdrawable is a kind of drawable.

We'll also see more drawable:state list drawable, Shape drawable, and Layer list drawable in this chapter.

These three drawable are defined in an XML file and can be categorized as a class, collectively referred to as XML drawable.

Shape drawable

With Shapedrawable, you can turn the button programming round, XML drawable and screen pixel density independent, so you don't have to consider creating a specific pixel density directory, just put it into the default drawable folder.

Create a circular drawable under the Res/drawable directory. A file named Button_beat_box_normal.xml with the following code:

<xmlns:android= "http://schemas.android.com/apk/res/android"       Android : Shape= "Oval">    <solid        android:color= "@color/drak_blue"/>    </shape>

The XML file defines a circle with a dark blue background color. You can also use shape drawable to customize other graphics.

Finally, you can modify the button background in styles.

State list drawable

Depending on the status of the button, the state list drawable can toggle to a different drawable, which changes the drawable used depending on the state of the button.

Create a State list drawable

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:drawable= "@drawable/button_beat_box_pressed"android:state_pressed= "true"/>    <Itemandroid:drawable= "@drawable/button_beat_box_normal"/></selector>

Use button_beat_box_pressed as the background when the button is pressed. Use Button_beat_box_normal as background when not pressed.

In addition to pressing the state, the state list drawable also supports disabling, focusing, and activating.

Layer List drawable

The layer list drawable allows two XML drawable to be combined to add a dark-colored ring for the pressed state button.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <layer-listxmlns:android= "Http://schemas.android.com/apk/res/android">3     <Item>4         <Shapexmlns:android= "Http://schemas.android.com/apk/res/android"5 Android:shape= "Oval">6 7             <Solid8                 Android:color= "@color/red"/>9         </Shape>Ten     </Item> One  A     <Item> -         <Shape -             Android:shape= "Oval"> the             <Stroke -                 Android:width= "4DP" - Android:color= "@color/drak_red"/> -         </Shape> +     </Item> -  + </layer-list>

Why do you use XML drawable

XML reader is easy to use and flexible, not only a variety of usage, but also easy to update maintenance, with the use of shape drawable and layer list drawable can make complex background map, even the image editor is saved, change the color of beatbox application is more simple, Just modify the color in the XML drawable.

XML reader is independent of the screen pixel density, they are directly defined in the drawable directory, do not need to add screen density resource modifier, if it is a normal image, you need to prepare multiple versions to fit different screen pixel density devices, and XML drawable as long as the definition, Can perform well on the screen of any device.

Using 9-patch images

9-patch image can solve the image stretching problem, 9-patch image is a specially processed file, let Android know which parts of the image can be stretched, which can not . With proper handling, you can ensure that the corners of the background map are aligned with the original image. Why do you call it 9-patch? The 9-patch image is divided into a 3x3 grid, which is a grid of 9 or 9 patches. The corners of the grid are not scaled, the 4 patches of the edge portion are scaled by only one dimension, while the middle part is scaled by two dimensions:

9-patch images are very similar to ordinary PNG images in two different ways: the 9-patch image file name ends with. 9.png, and the edge of the image has a border of 1 pixels wide. This border specifies the middle position of the 9-patch image. The border pixel is drawn as a black line to indicate the middle position, while the edge part is represented by a transparent color.

Using Mipmap images

Resource modifiers and drawable are easy to use. To use the image, you can prepare different sizes of pictures for different device sizes, and then put them into folders such as DRAWABLE-MDPI and drawable-hdpi. They are then referenced by name. The rest is given to Android, which calls the image according to the screen density of the current device.

One question has to be mentioned. The APK file published to the Google store contains all the images in the project's drawable directory, even if they are never used. It's a burden.

Someone wanted to customize the apk for the device, such as MDPI apk one, hdpi apk one, and so on. But the problem was not solved thoroughly. What if you want to keep the start icon for each screen pixel density?

The Android launcher is a resident Home screen app (see Chapter 22nd). Press the device's home key to return to the Launcher application screen. Some new versions of the launcher display large app icons. To make large icons look good, the launcher needs to use a higher-resolution icon. For hdpi devices, to display large icons, the launcher uses the xhdpi icon. If you can't find it, you can only use the low-resolution icon. It is conceivable that the enlarged drawing after the icon must be very bad. The ultimate solution for Android is to use the Mipmap directory.

The mipmap resource is all included in the APK file when the APK is split. Once and for all, the recommended practice is to place the app Launcher icon in the Mipmap directory, and the other images in the drawable directory.

Android authoritative Programming Guide-notes (21st. XML drawable)

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.