Ninepatch is a useful PNG image format that can be scaled with text size in a specific area. As follows:
As you can see, the middle area of the background image is scaled as the text is resized. The background image is a ninepatch picture. Ninepatch pictures can be made using Android's own Draw9patch tool, which is in the tools directory of the SDK installation path.
The source code is as follows:
1 PackageCom.example.day22_02ninepatchdemo;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 6 Public classMainactivityextendsActivity {7 8 @Override9 protected voidonCreate (Bundle savedinstancestate) {Ten Super. OnCreate (savedinstancestate); One Setcontentview (r.layout.activity_main); A } -}
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.example.day22_02ninepatchdemo." Mainactivity "Ten android:orientation= "vertical" > One A <TextView - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= " Hello hello hello you hello hello hello hello you hello hello hi hello hello hi hello hello hi hello hi you you hi - Android:background= "@drawable/chatfrom_bg"/> - - <TextView + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:text= "Hello, hello, hello, Hi, Hi, hello. Hello, hello, hello, hello. Hi, hello, how are you? Hello, hello, hello, hi, how are you? Hello Hello hi hello you hello A Android:background= "@drawable/mymsgbg2"/> at - </LinearLayout>
: The first is an unprocessed background image, the second is a processed background image.
You can see that two pictures open with Windows Photo Viewer is not the same, and the suffix is not the same, the former is Xxx.png, the latter is xxx.9.png.
022_02android's nine Patch pictures