例:我要把這個圖檔當作我的背景圖,裡面放文字用。紫色區塊需要隨著裡面的字變多而延展。

做成像下圖的效果:

作法很簡單:
在android studio,右鍵點擊要建立NinePatch的png圖檔,然後點擊 Create 9-path file。
點擊9-patch檔案,在android studio 打開,會出現如下畫面
在4個邊邊的灰白相間的線,點一下加上黑線,這個黑線代表可延展區塊
要取消黑線的話,按住shift再點一下黑線就可以了。
勾選下方的Show patchs,可以看到粉紅色的區塊為可延展區塊,綠色區塊為不可延展區塊。

另外如果你build 看到這樣的訊息:some file crunching failed ,代表你的 9-patch 檔案有問題
開啟9-patch檔案,勾選 Show bad patches,就可以看到錯誤在哪裡了
完整程式碼:https://github.com/evanchen76/NinePatchSample
參考:https://developer.android.com/studio/write/draw9patch.html
沒有留言:
張貼留言