Nine-Patch圖片是一種被特殊處理過的png圖片,能夠指定哪些區域可以被拉伸,哪些區域不可以。
先準備一張圖片。
3bfd56e15b6ccd4aea64849946addb99.png
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/message_left" >
</LinearLayout>
</LinearLayout>
預覽一下效果
image.png
可以看到圖片已經變形了。
下面把圖片處理成nine-patch圖片。
在Android studio中找到該圖片,右擊,選擇 create-9-patch file,然后進入界面
image.png
在圖片四周繪制四個黑色小點,就是圖片需要拉伸的區域,然后保存,刪除掉原來的圖片。這時候會生成一個原圖片名稱后面加個.9的圖片,不要改名字,然后引入圖片的地方也不需要改,再看一下效果。
image.png
已經正常了。比之前美觀多了。