Android 9-patch

1.   9-patch介紹    

9-patch繪畫的是一個可以伸縮的位圖圖像,Android會自動調整大小來容納顯示的內容。一個例子就是NinePatch爲背景,使用標準的Android按鈕,按鈕必須伸縮來容納長度變化的字符。

       9-patch是一個標準的PNG圖像,它包括額外的1個像素的邊界,你必須保存它後綴爲.9.png,並且保存到工程的res/drawable目錄中。如果你是從APK解壓後得到的*.9.png文件,注意它是已將周圍的空白像素去掉的,在使用時必須再加上,不然eclipse會報錯。

2.   使用9-patch流程說明

 

1.      (1)From a terminal, launch the draw9patch application from your SDK /tools directory.

2.      (2)Drag your PNG image into the Draw 9-patch window (or File > Open 9-patch... to locate the file). Your workspace will now open.The left pane is your drawing area, in which you can edit the lines for the stretchable patches and content area. The right pane is the preview area, where you can preview your graphic when stretched.

3.      (3)Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines.

4.      (4)When done, select File > Save 9-patch...

Your image will be saved with the .9.png file name.

 

Note: A normal PNG file (*.png) will be loaded with an empty one-pixel border added around the image, in which you can draw the stretchable patches and content area. A previously saved 9-patch file (*.9.png) will be loaded as-is, with no drawing area added, because it already exists.

Android  9-patch - 貓君 - 貓羣博客
 

Optional controls include:

·         (1)Zoom: Adjust the zoom level of the graphic in the drawing area.

·         (2)Patch scale: Adjust the scale of the images in the preview area.

·         (3)Show lock: Visualize the non-drawable area of the graphic on mouse-over.

·         (4)Show patches: Preview the stretchable patches in the drawing area (pink is a stretchable patch).

·         (5)Show content: Highlight the content area in the preview images (purple is the area in which content is allowed).

·         (6)Show bad patches: Adds a red border around patch areas that may produce artifacts in the graphic when stretched. Visual coherence of your stretched image will be maintained if you eliminate all bad patches.

 

3.   使用9-patch問題及解決方法

1. draw9patch.bat不能運行的一個解決方案,下載jar包,地址是:
http://netmite.com/android/mydroid/1.6/prebuilt/common/swing-worker/swing-worker-1.1.jar
下好的jar包放到:$ANDROID_SDK/tools/lib 文件裏,再運行就可以。

 

2.Android項目中使用Nine Patch資源,時候可能出現比如ERROR: 9-patch image android123.9.png malformed. No marked region found along edge.以及 Found along top edge.的錯誤主要是因爲該圖片沒有四周的定位像素


參考:http://developer.android.com/guide/topics/graphics/2d-graphics.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章