1. Create ninepatchdrawable
A ninepatch is also a PNG image, but the difference is that a scalable area can be defined for images in this format. When the content of a view exceeds the normal size, images in this format are automatically stretched to fit different sizes. Generally, this image is used as the background of the view, and the view must have at least one size (layout_width or layout_height) set to "warp_content ". When the view grows to adapt to the content, the nine-patch Format Image is scaled accordingly to match the view size.
Ninepatchdrawable is created in almost the same way as bitmapdrawable, and there is no big difference in usage. Only when XML is created, there are not so many attributes.
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="" android:dither=""/>
It only has these three attributes.
Create an image in .9.png format
In the tools directory of androidsdk, there is such a tool draw9patch. bat. You can use this tool to quickly create a .9.png image.
Images in this format can be adjusted automatically in the android environment.
(1) developers are allowed to define extensible areas. When you need to extend an image to fill a region larger than the image itself, the content of the expanded area is extended.
(2) allow developers to define content display areas for displaying text or other content
As shown in:
The black lines on the left and above can be expanded. The black line on the right and bottom is the content display area. It can be used to partially stretch images to achieve perfect application of images on Android systems.
The image is named mask.png.
We have produced a scaled-down mask1.9.png image. And mask2.9.png images with limited and stretched content.
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <! -- Buttons with a normal image as the background, with less content and no stretching --> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/mask" Android: text = "1111"/> <! -- A button with a normal image as the background. If there are too many buttons, the image will be stretched and blurred. --> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/mask" Android: text = "111111111111111111111"/> <! -- A button with a background of .9.png with a stretch setting. If there are too many buttons, the stretch part will be stretched and the image will not be deformed. --> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/mask1" Android: text = "111111111111111111111" type = "codeph" text = "codeph"/> <! -- Buttons with .9.png as the background with limited content and stretched settings. If there are too many buttons, the stretched part will be stretched and the text content location will change --> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "@ drawable/mask2" Android: text = "1111111111"/> </linearlayout>
: