NinePatchDrawable<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">A</wbr></span><span style="color:#ff00ff; line-height:25px">NinePatchDrawable</span><span style="color:#000080; line-height:25px">graphicisastretchablebitmapimage,<br style="line-height:25px"> whichAndroidwillautomaticallyresizetoaccommodatethecontentsoftheViewinwhichyouhaveplaceditasthebackground.</span><wbr style="line-height:25px"><br style="line-height:25px"> AnexampleuseofaNinePatchisthebackgroundsusedbystandardAndroidbuttons—<br style="line-height:25px"> buttonsmuststretchtoaccommodatestringsofvariouslengths.<br style="line-height:25px"> ANinePatchdrawableisastandardPNGimagethatincl?sanextra1-pixel-wideborder.<br style="line-height:25px"> Itmustbesavedwiththeextension.9.png,andsavedintotheres/drawable/directoryofyourproject.<br style="line-height:25px"><br style="line-height:25px"> Theborderisusedtodefinethestretchableandstaticareasoftheimage.<br style="line-height:25px"> Youindicateastretchablesectionbydrawingone(ormore)1-pixel-wideblackline(s)intheleftandtoppartoftheborder.<br style="line-height:25px"> (Youcanhaveasmanystretchablesectionsasyouwant.)Therelativesizeofthestretchablesectionsstaysthesame,<br style="line-height:25px"> sothelargestsectionsalwaysremainthelargest.<br style="line-height:25px"><br style="line-height:25px"> Youcanalsodefineanoptionaldrawablesectionoftheimage(effectively,thepaddinglines)<br style="line-height:25px"> bydrawingalineontherightandbottomlines.IfaViewobjectsetstheNinePatchasitsbackground<br style="line-height:25px"> andthenspecifiestheView'stext,itwillstretchitselfsothatallthetextfitsinsideonly<br style="line-height:25px"> theareadesignatedbytherightandbottomlines(ifincl?d).Ifthepaddinglinesarenotincl?d,<br style="line-height:25px"> Androidusestheleftandtoplinestodefinethisdrawablearea.<br style="line-height:25px"><br style="line-height:25px"> Toclarifythedifferencebetweenthedifferentlines,<br style="line-height:25px"> theleftandtoplinesdefinewhichpixelsoftheimageareallowedtobereplicatedinordertostretchtheimage.<br style="line-height:25px"> ThebottomandrightlinesdefinetherelativeareawithintheimagethatthecontentsoftheViewareallowedtoliewithin.<br style="line-height:25px"><span style="color:#003366; line-height:25px"><wbr style="line-height:25px">A</wbr></span><span style="color:#ff00ff; line-height:25px">NinePatchDrawable</span><span style="color:#003366; line-height:25px">相对于</span><span style="color:#ff9900; line-height:25px">Bitmap Drawable</span><span style="color:#003366; line-height:25px">主要有增加了两个属性。他主要定义了图片的两类可区域。<br style="line-height:25px"> 一类是一般的可扩展(放大)区域区域,他主要用来填充控件的空白区域。<br style="line-height:25px"> 另一类区域主要用于填充内容区域(比如对于button来说内容区域就是其文本区域)<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 注意:<br style="line-height:25px"> ThisNinePatchdefinesonestretchableareawiththeleft<br style="line-height:25px"> andtoplinesandthedrawableareawiththebottomandrightlines.<br style="line-height:25px"> Inthetopimage,thedottedgreylinesidentifytheregionsoftheimagethatwillbereplicatedinordertostretchtheimage.<br style="line-height:25px"> ThepinkrectangleinthebottomimageidentifiestheregioninwhichthecontentsoftheViewareallowed.<br style="line-height:25px"> Ifthecontentsdon'tfitinthisregion,thentheimagewillbestretchedsothattheydo.<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">使用</span><span style="color:#ff6600; line-height:25px">Draw9-patch</span><span style="color:#000080; line-height:25px">创建</span><span style="color:#ff00ff; line-height:25px">NinePatchDrawable</span><span style="color:#000080; line-height:25px">形式的图(.9.png)</span><wbr style="line-height:25px"><br style="line-height:25px"><br style="line-height:25px"> theDraw9-patchtoolallowsyoutoeasilycreateaNinePatchgraphicusingaWYSIWYGeditor.<br style="line-height:25px"> Foranintrod tiontoNine-patchgraphicsandhowtheywork,<br style="line-height:25px"> pleasereadthesectiononNine-patchintheNinepatchImagestopic.<br style="line-height:25px"> Here'saquickguidetocreateaNine-patchgraphicusingtheDraw9-patchtool.You'llneedthePNGimagewithwhichyou'dliketocreateaNinePatch.<br style="line-height:25px"><br style="line-height:25px"> 1.Fromaterminal,launchthedraw9patchapplicationfromyour<span style="color:#000080; line-height:25px">SDK/toolsdirectory</span>.<br style="line-height:25px"> 2.DragyourPNGimageintotheDraw9-patchwindow(orFile>Open9-patch...tolocatethefile).Yourworkspacewillnowopen.<br style="line-height:25px"> Theleftpaneisyourdrawingarea,inwhichyoucaneditthelinesforthestretchablepatchesandcontentarea.<br style="line-height:25px"> Therightpaneisthepreviewarea,whereyoucanpreviewyourgraphicwhenstretched.<br style="line-height:25px"> 3.Clickwithinthe1-pixelperimetertodrawthelinesthatdefinethestretchablepatchesand(optional)contentarea.<br style="line-height:25px"> Right-click(orholdShiftandclick,onMac)toerasepreviouslydrawnlines.<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">可以在图片的框的四边上,按下鼠标左键并拖动可以画黑线,按下shift键的同时按鼠标左键并拖动可清除画的黑线。</span><wbr style="line-height:25px"><br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">A,</span><span style="color:#003366; line-height:25px">左边那条黑色线代表图片垂直拉伸的区域,上边的那条黑色线代表水平拉伸区域。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px">他们的交集就区域代表就垂直拉伸和水平拉伸都可以进行的区域。</span><br style="line-height:25px"> 注意:我开始因为它是表达的只是他们的交集区域。<br style="line-height:25px"> 其实不是,它要表达的垂直拉伸和水平拉伸区域,不能只是交集区域。<br style="line-height:25px"> 因为如果只是交集区域的话,图片一放大,就不是长方形了。<br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">B,</span><span style="color:#000080; line-height:25px">右边的黑色线代表内容绘制的垂直区域,下边的黑色线代表内容绘制的水平区域</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px">他们交集的区域就是内容所要绘制到的区域。<br style="line-height:25px"> 对于button来说就是文本的区域,这样就可以控制文本显示的区域。<br style="line-height:25px"><wbr style="line-height:25px">如果</wbr></span><span style="color:#0000ff; line-height:25px">下边</span><span style="color:#000080; line-height:25px">和</span><span style="color:#0000ff; line-height:25px">右边</span><span style="color:#000080; line-height:25px">的黑线不是连续的,那它按</span><span style="color:#0000ff; line-height:25px">最左端(上端)和最右端(下端)</span><span style="color:#000080; line-height:25px">组成的线段来算内容区域<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 注意:右边和下边的线是<span style="line-height:25px"><wbr style="line-height:25px">可选</wbr></span><wbr style="line-height:25px">的,左边和上边的线<span style="line-height:25px"><wbr style="line-height:25px">不能省略</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"><br style="line-height:25px"> 4.Whendone,selectFile>Save9-patch...<br style="line-height:25px"> Yourimagewillbesavedwiththe.9.pngfilename.<br style="line-height:25px"> Draw9-patch的使用,<br style="line-height:25px"> NinePatchDrawable的使用和一般的Drawable一样,也是放在目录drawable下,<br style="line-height:25px"> xml中引用如<span style="color:#0000ff; line-height:25px">android:background="@drawable/test"</span>的形式,其中"test"为文件名<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">参考资料:</wbr></span><a target="_blank" rel="nofollow" href="http://www.coolapk.com/docs/guide/developing/tools/draw9patch.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://www.coolapk.com/docs/guide/developing/tools/draw9patch.html</a><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"><br style="line-height:25px"> NinePatch</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> TheNinePatchclasspermitsdrawingabitmapinninesections.<br style="line-height:25px"> Thefourcornersareunscaled;thefouredgesarescaledinoneaxis,<br style="line-height:25px"> andthemiddleisscaledinbothaxes.Normally,<br style="line-height:25px"> themiddleistransparentsothatthepatchcanprovideaselectionaboutarectangle.<br style="line-height:25px"> Essentially,itallowsthecreationofcustomgraphicsthatwillscalethewaythatyoudefine,<br style="line-height:25px"> whencontentaddedwithintheimageexceedsthenormalboundsofthegraphic.<br style="line-height:25px"> ForathoroughexplanationofaNinePatchimage,readthediscussioninthe2DGraphicsdocument.<br style="line-height:25px"> TheDraw9-PatchtooloffersanextremelyhandywaytocreateyourNinePatchimages,usingaWYSIWYGgraphicseditor.<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">他可以让我们在运行时定制stretchable的区域和区域的填充方式<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 它的构造函数只有一个。<br style="line-height:25px"><span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="pubctors" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Public Constructors</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/graphics/NinePatch.html#NinePatch(android.graphics.Bitmap,%20byte%5B%5D,%20java.lang.String)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">NinePatch</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/Bitmap.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Bitmap</a>bitmap, byte[] chunk,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>srcName)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Create a drawable projection from a bitmap to nine patches.</div> </td> </tr> </tbody></table></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
它的jni函数在
frameworks\base\core\jni\android\graphics\NinePatch.cpp
中
这里的byte[]chunk数据的格式一直不明白。
最后参看Android源码总算明白了。
chunk在jni(NinePatch.cpp)中主要用来构造Res_png_9patch这个结构体
chunk的至少有个sizeof(Res_png_9patch)大,即32个字节
Res_png_9patch的申明在
frameworks\base\incl?\utils\ResourceTypes.h
网上地址:http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=incl?/utils/ResourceTypes.h
Res_png_9patch的定义在frameworks\base\libs\utils\ResourceTypes.cpp中。
/**
44*Thischunkspecifieshowtosplitanimageintosegmentsfor
45*scaling.
46*
47*ThereareJhorizontalandKverticalsegments.Thesesegmentsdivide
48*theimageintoJ*Kregionsasfollows(whereJ=4andK=3):
49*
50*F0S0F1S1
51*+-----+----+------+-------+
52*S2|0|1|2|3|
53*+-----+----+------+-------+
54*|||||
55*|||||
56*F2|4|5|6|7|
57*|||||
58*|||||
59*+-----+----+------+-------+
60*S3|8|9|10|11|
61*+-----+----+------+-------+
62*
63*Eachhorizontalandverticalsegmentisconsideredtobyeither
64*stretchable(markedbytheSxlabels)orfixed(markedbytheFy
65*labels),inthehorizontalorverticalaxis,respectively.Inthe
66*aboveexample,thefirstishorizontalsegment(F0)isfixed,the
67*nextisstretchableandthentheycontintoalternate.Notethat
68*thesegmentlistforeachaxiscanbeginorendwithastretchable
69*orfixedsegment.
70*
71*Therelativesizesofthestretchysegmentsindicatestherelative
72*amountofstretchinessoftheregionsborderedbythesegments.For
73*example,regions3,7and11abovewilltakeupmorehorizontalspace
74*thanregions1,5and9sincethehorizontalsegmentassociatedwith
75*thefirstsetofregionsislargerthantheothersetofregions.The
76*ratiosoftheamountofhorizontal(orvertical)spacetakenbyany
77*twostretchableslicesisexactlytheratiooftheircorresponding
78*segmentlengths.
79*
80*xDivsandyDivspointtoarraysofhorizontalandverticalpixel
81*indices.ThefirstpairofDivs(ineitherarray)indicatethe
82*startingandendingpointsofthefirststretchablesegmentinthat
83*axis.Thenextpairspecifiesthenextstretchablesegment,etc.So
84*intheaboveexamplexDiv[0]andxDiv[1]specifythehorizontal
85*coordinatesfortheregionslabeled1,5and9.xDiv[2]and
86*xDiv[3]specifythecoordinatesforregions3,7and11.Notethat
87*theleftmostslicesalwaysstartatx=0andtherightmostslices
88*alwaysendattheendoftheimage.So,forexample,theregions0,
89*4and8(whicharefixedalongtheXaxis)startatxval0and
90*gotoxDiv[0]andslices2,6and10startatxDiv[1]andendat
91*xDiv[2].
92*
93*Thearraypointedtobythecolorsfieldlistscontainshintsfor
94*eachoftheregions.Theyareorderedaccordingleft-to-rightand
95*top-to-bottomasindicatedabove.Foreachsegmentthatisasolid
96*colorthearrayentrywillcontainthatcolorval;otherwiseit
97*willcontainNO_COLOR.Segmentsthatarecompletelytransparent
98*willalwayshavethevalTRANSPARENT_COLOR.
99*
100*ThePNGchunktypeis"npTc".
101*/
102str tRes_png_9patch
103{
104Res_png_9patch():wasDeserialized(false),xDivs(NULL),
105yDivs(NULL),colors(NULL){}
106
107int8_twasDeserialized;
108int8_tnumXDivs;
109int8_tnumYDivs;
110int8_tnumColors;
111
112//Thesetellwherethenextsectionofapatchstarts.
113//Forexample,thefirstpatchincl?sthepixelsfrom
114//0toxDivs[0]-1andthesecondpatchincl?sthepixels
115//fromxDivs[0]toxDivs[1]-1.
116//Note:allocation/freeofthesepointersislefttothecaller.
117int32_t*xDivs;
118int32_t*yDivs;
119
120int32_tpaddingLeft,paddingRight;
121int32_tpaddingTop,paddingBottom;
122
123enum{
124//The9patchsegmentisnotasolidcolor.
125 <wbr style="line-height:25px">NO_COLOR=0x00000001,</wbr><wbr style="line-height:25px"><br style="line-height:25px"> 126127//The9patchsegmentiscompletelytransparent.<br style="line-height:25px"> 128<span style="line-height:25px"><wbr style="line-height:25px">TRANSPARENT_COLOR=0x00000000</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 129};<br style="line-height:25px"> 130//Note:allocation/freeofthispointerislefttothecaller.<br style="line-height:25px"> 131uint32_t*colors;<br style="line-height:25px"> 132<br style="line-height:25px"> 133//ConvertdatafromdevicerepresentationtoPNGfilerepresentation.<br style="line-height:25px"> 134voiddeviceToFile();<br style="line-height:25px"> 135//ConvertdatafromPNGfilerepresentationtodevicerepresentation.<br style="line-height:25px"> 136voidfileToDevice();<br style="line-height:25px"> 137//Serialize/Marshallthepatchdataintoanewlymalloc-edblock<br style="line-height:25px"> 138void*serialize();<br style="line-height:25px"> 139//Serialize/Marshallthepatchdata<br style="line-height:25px"> 140voidserialize(void*outData);<br style="line-height:25px"> 141//Deserialize/Unmarshallthepatchdata<br style="line-height:25px"> 142staticRes_png_9patch*deserialize(constvoid*data);<br style="line-height:25px"> 143//Computethesizeoftheserializeddatastr ture<br style="line-height:25px"> 144size_tserializedSize();<br style="line-height:25px"> 145};<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:Res_png_9patch的大小为32<br style="line-height:25px"> //Thesizeofthisstr tis32bytesonthe32-bittargetsystem<br style="line-height:25px"> //4*int8_t<br style="line-height:25px"> //4*int32_t<br style="line-height:25px"> //3*pointer<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:xDivs和yDivs用于确定可stretchable区域。<br style="line-height:25px"> 比如:<br style="line-height:25px"> xDivs[0]和xDivs[1]确定一块可stretchable区域,<br style="line-height:25px"> 然后xDivs[2]和xDivs[3]又确定一块可stretchable区域<br style="line-height:25px"> yDivs和xDivs一样。<br style="line-height:25px"> 注意2:colors指明了每个区域的填充颜色。<br style="line-height:25px"> 这里的区域并不区分非stretchable区域和stretchable区域<br style="line-height:25px"> 其中NO_COLOR=0x00000001,表示用图片的填充<br style="line-height:25px"> TRANSPARENT_COLOR=0x00000000表示完全透明。<br style="line-height:25px"> 区域的顺序是从左到右,再从下到下。<br style="line-height:25px"><span style="color:#003366; line-height:25px">下<wbr style="line-height:25px">面详细说明byte[]chunk的和Res_png_9patch中变量的对应关系</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"><span style="color:#000080; line-height:25px">第0个字节wasDeserialized备注<br style="line-height:25px"> 第1个字节numXDivsX方向可stretchable的区域数量<br style="line-height:25px"> 第2个字节numYDivsY方向可stretchable的区域数量<br style="line-height:25px"> 第3个字节numColorscolor数(每个区域对应一个color)<br style="line-height:25px"> 第4,5,6,7字节xDivsz指针在chunk中没意义<br style="line-height:25px"> 第8,9,10,11字节yDivsz指针在chunk中没意义<br style="line-height:25px"> 第12,13,14,15字节paddingLeft没发现有什么用<br style="line-height:25px"> 第16,17,18,19字节paddingRight没发现有什么用<br style="line-height:25px"> 第20,21,22,23字节paddingTop没发现有什么用<br style="line-height:25px"> 第24,25,26,27字节paddingBottom没发现有什么用<br style="line-height:25px"> 第28,29,30,31字节colors指针在chunk中没意义</span><br style="line-height:25px"> 从sizeof(Res_png_9patch)开始的数据(即第32个字节开始):<br style="line-height:25px"> 首先是xDivsz指针指向的内容,其大小为numXDivs个int32_t<br style="line-height:25px"> 然后是yDivs指针指向的内容,其大小为numYDivs个int32_t<br style="line-height:25px"> 最后是colors指针指向的内容,其大小为numColors个int32_t(ARGB格式)<br style="line-height:25px"> 注意:对于int32_t这种多字节的类型是高字节在内存的高位<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><wbr style="line-height:25px">:<span style="line-height:25px"><wbr style="line-height:25px"><br style="line-height:25px"> 代码1</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BitmapDrawablebitmapDrawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.icon);<br style="line-height:25px"> Bitmapbitmap=bitmapDrawable.getBitmap();<br style="line-height:25px"> intwidth=bitmap.getWidth();<br style="line-height:25px"> System.out.println("width:"+width);<br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px">bytenumXDivs=4;<br style="line-height:25px"> bytenumYDivs=4;<br style="line-height:25px"> bytenumColors=numXDivs;<br style="line-height:25px"> intpaddingLeft=10;<br style="line-height:25px"> intoffset=0;<br style="line-height:25px"> numColors=(byte)((numColors+1)*(numYDivs+1));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">int</span><span style="color:#3366ff; line-height:25px">size=32+(numXDivs+numYDivs+numColors)*4;<br style="line-height:25px"> bytechunk[]=newbyte[size];<br style="line-height:25px"> chunk[0]=0;<br style="line-height:25px"> chunk[1]=numXDivs;<br style="line-height:25px"> chunk[2]=numYDivs;<br style="line-height:25px"> chunk[3]=numColors;<br style="line-height:25px"> bytepVal=0;<br style="line-height:25px"> chunk[4]=pVal;<br style="line-height:25px"> chunk[5]=pVal;<br style="line-height:25px"> chunk[6]=pVal;<br style="line-height:25px"> chunk[7]=pVal;<br style="line-height:25px"><br style="line-height:25px"> chunk[8]=pVal;<br style="line-height:25px"> chunk[9]=pVal;<br style="line-height:25px"> chunk[10]=pVal;<br style="line-height:25px"> chunk[11]=pVal;<br style="line-height:25px"> offset=12;<br style="line-height:25px"> putInt(chunk,offset,paddingLeft);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> offset=32;<br style="line-height:25px"> putInt(chunk,offset,10);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,30);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,40);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,width);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"><br style="line-height:25px"> putInt(chunk,offset,11);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,18);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,30);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,56);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> intcolor=0x00000F;<br style="line-height:25px"> intcolor2=0xFFFFF0;<br style="line-height:25px"> intcolorInc=(color2-color)/numColors;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(inti=0;i<numColors;i++)<br style="line-height:25px"> {<br style="line-height:25px"> putInt(chunk,offset,0x2F000000|color);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> color+=colorInc;<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px">img9Patch=newNinePatch(bitmap,chunk,null);</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">代码2</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">putInt</span><span style="color:#3366ff; line-height:25px">(bytearray[],intoffset,intval)<br style="line-height:25px"> {<br style="line-height:25px"> array[offset]=(byte)(val&0xFF);<br style="line-height:25px"> val=val>>8;<br style="line-height:25px"> array[offset+1]=(byte)(val&0xFF);<br style="line-height:25px"> val=val>>8;<br style="line-height:25px"> array[offset+2]=(byte)(val&0xFF);<br style="line-height:25px"> val=val>>8;<br style="line-height:25px"> array[offset+3]=(byte)(val&0xFF);<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">代码3</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">RectFrect=newRectF(0,0,Game.width,Game.height);<br style="line-height:25px"> img9Patch.draw(canvas,rect);</span><br style="line-height:25px"><span style="line-height:25px">注意1</span>:rect是要把img9Patch画到的区域。 </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
这里的byte[]chunk数据的格式一直不明白。
最后参看Android源码总算明白了。
chunk在jni(NinePatch.cpp)中主要用来构造Res_png_9patch这个结构体
chunk的至少有个sizeof(Res_png_9patch)大,即32个字节
Res_png_9patch的申明在
frameworks\base\incl?\utils\ResourceTypes.h
网上地址:http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=incl?/utils/ResourceTypes.h
Res_png_9patch的定义在frameworks\base\libs\utils\ResourceTypes.cpp中。
/**
44*Thischunkspecifieshowtosplitanimageintosegmentsfor
45*scaling.
46*
47*ThereareJhorizontalandKverticalsegments.Thesesegmentsdivide
48*theimageintoJ*Kregionsasfollows(whereJ=4andK=3):
49*
50*F0S0F1S1
51*+-----+----+------+-------+
52*S2|0|1|2|3|
53*+-----+----+------+-------+
54*|||||
55*|||||
56*F2|4|5|6|7|
57*|||||
58*|||||
59*+-----+----+------+-------+
60*S3|8|9|10|11|
61*+-----+----+------+-------+
62*
63*Eachhorizontalandverticalsegmentisconsideredtobyeither
64*stretchable(markedbytheSxlabels)orfixed(markedbytheFy
65*labels),inthehorizontalorverticalaxis,respectively.Inthe
66*aboveexample,thefirstishorizontalsegment(F0)isfixed,the
67*nextisstretchableandthentheycontintoalternate.Notethat
68*thesegmentlistforeachaxiscanbeginorendwithastretchable
69*orfixedsegment.
70*
71*Therelativesizesofthestretchysegmentsindicatestherelative
72*amountofstretchinessoftheregionsborderedbythesegments.For
73*example,regions3,7and11abovewilltakeupmorehorizontalspace
74*thanregions1,5and9sincethehorizontalsegmentassociatedwith
75*thefirstsetofregionsislargerthantheothersetofregions.The
76*ratiosoftheamountofhorizontal(orvertical)spacetakenbyany
77*twostretchableslicesisexactlytheratiooftheircorresponding
78*segmentlengths.
79*
80*xDivsandyDivspointtoarraysofhorizontalandverticalpixel
81*indices.ThefirstpairofDivs(ineitherarray)indicatethe
82*startingandendingpointsofthefirststretchablesegmentinthat
83*axis.Thenextpairspecifiesthenextstretchablesegment,etc.So
84*intheaboveexamplexDiv[0]andxDiv[1]specifythehorizontal
85*coordinatesfortheregionslabeled1,5and9.xDiv[2]and
86*xDiv[3]specifythecoordinatesforregions3,7and11.Notethat
87*theleftmostslicesalwaysstartatx=0andtherightmostslices
88*alwaysendattheendoftheimage.So,forexample,theregions0,
89*4and8(whicharefixedalongtheXaxis)startatxval0and
90*gotoxDiv[0]andslices2,6and10startatxDiv[1]andendat
91*xDiv[2].
92*
93*Thearraypointedtobythecolorsfieldlistscontainshintsfor
94*eachoftheregions.Theyareorderedaccordingleft-to-rightand
95*top-to-bottomasindicatedabove.Foreachsegmentthatisasolid
96*colorthearrayentrywillcontainthatcolorval;otherwiseit
97*willcontainNO_COLOR.Segmentsthatarecompletelytransparent
98*willalwayshavethevalTRANSPARENT_COLOR.
99*
100*ThePNGchunktypeis"npTc".
101*/
102str tRes_png_9patch
103{
104Res_png_9patch():wasDeserialized(false),xDivs(NULL),
105yDivs(NULL),colors(NULL){}
106
107int8_twasDeserialized;
108int8_tnumXDivs;
109int8_tnumYDivs;
110int8_tnumColors;
111
112//Thesetellwherethenextsectionofapatchstarts.
113//Forexample,thefirstpatchincl?sthepixelsfrom
114//0toxDivs[0]-1andthesecondpatchincl?sthepixels
115//fromxDivs[0]toxDivs[1]-1.
116//Note:allocation/freeofthesepointersislefttothecaller.
117int32_t*xDivs;
118int32_t*yDivs;
119
120int32_tpaddingLeft,paddingRight;
121int32_tpaddingTop,paddingBottom;
122
123enum{
124//The9patchsegmentisnotasolidcolor.
125 <wbr style="line-height:25px">NO_COLOR=0x00000001,</wbr><wbr style="line-height:25px"><br style="line-height:25px"> 126127//The9patchsegmentiscompletelytransparent.<br style="line-height:25px"> 128<span style="line-height:25px"><wbr style="line-height:25px">TRANSPARENT_COLOR=0x00000000</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 129};<br style="line-height:25px"> 130//Note:allocation/freeofthispointerislefttothecaller.<br style="line-height:25px"> 131uint32_t*colors;<br style="line-height:25px"> 132<br style="line-height:25px"> 133//ConvertdatafromdevicerepresentationtoPNGfilerepresentation.<br style="line-height:25px"> 134voiddeviceToFile();<br style="line-height:25px"> 135//ConvertdatafromPNGfilerepresentationtodevicerepresentation.<br style="line-height:25px"> 136voidfileToDevice();<br style="line-height:25px"> 137//Serialize/Marshallthepatchdataintoanewlymalloc-edblock<br style="line-height:25px"> 138void*serialize();<br style="line-height:25px"> 139//Serialize/Marshallthepatchdata<br style="line-height:25px"> 140voidserialize(void*outData);<br style="line-height:25px"> 141//Deserialize/Unmarshallthepatchdata<br style="line-height:25px"> 142staticRes_png_9patch*deserialize(constvoid*data);<br style="line-height:25px"> 143//Computethesizeoftheserializeddatastr ture<br style="line-height:25px"> 144size_tserializedSize();<br style="line-height:25px"> 145};<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:Res_png_9patch的大小为32<br style="line-height:25px"> //Thesizeofthisstr tis32bytesonthe32-bittargetsystem<br style="line-height:25px"> //4*int8_t<br style="line-height:25px"> //4*int32_t<br style="line-height:25px"> //3*pointer<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:xDivs和yDivs用于确定可stretchable区域。<br style="line-height:25px"> 比如:<br style="line-height:25px"> xDivs[0]和xDivs[1]确定一块可stretchable区域,<br style="line-height:25px"> 然后xDivs[2]和xDivs[3]又确定一块可stretchable区域<br style="line-height:25px"> yDivs和xDivs一样。<br style="line-height:25px"> 注意2:colors指明了每个区域的填充颜色。<br style="line-height:25px"> 这里的区域并不区分非stretchable区域和stretchable区域<br style="line-height:25px"> 其中NO_COLOR=0x00000001,表示用图片的填充<br style="line-height:25px"> TRANSPARENT_COLOR=0x00000000表示完全透明。<br style="line-height:25px"> 区域的顺序是从左到右,再从下到下。<br style="line-height:25px"><span style="color:#003366; line-height:25px">下<wbr style="line-height:25px">面详细说明byte[]chunk的和Res_png_9patch中变量的对应关系</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"><span style="color:#000080; line-height:25px">第0个字节wasDeserialized备注<br style="line-height:25px"> 第1个字节numXDivsX方向可stretchable的区域数量<br style="line-height:25px"> 第2个字节numYDivsY方向可stretchable的区域数量<br style="line-height:25px"> 第3个字节numColorscolor数(每个区域对应一个color)<br style="line-height:25px"> 第4,5,6,7字节xDivsz指针在chunk中没意义<br style="line-height:25px"> 第8,9,10,11字节yDivsz指针在chunk中没意义<br style="line-height:25px"> 第12,13,14,15字节paddingLeft没发现有什么用<br style="line-height:25px"> 第16,17,18,19字节paddingRight没发现有什么用<br style="line-height:25px"> 第20,21,22,23字节paddingTop没发现有什么用<br style="line-height:25px"> 第24,25,26,27字节paddingBottom没发现有什么用<br style="line-height:25px"> 第28,29,30,31字节colors指针在chunk中没意义</span><br style="line-height:25px"> 从sizeof(Res_png_9patch)开始的数据(即第32个字节开始):<br style="line-height:25px"> 首先是xDivsz指针指向的内容,其大小为numXDivs个int32_t<br style="line-height:25px"> 然后是yDivs指针指向的内容,其大小为numYDivs个int32_t<br style="line-height:25px"> 最后是colors指针指向的内容,其大小为numColors个int32_t(ARGB格式)<br style="line-height:25px"> 注意:对于int32_t这种多字节的类型是高字节在内存的高位<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><wbr style="line-height:25px">:<span style="line-height:25px"><wbr style="line-height:25px"><br style="line-height:25px"> 代码1</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BitmapDrawablebitmapDrawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.icon);<br style="line-height:25px"> Bitmapbitmap=bitmapDrawable.getBitmap();<br style="line-height:25px"> intwidth=bitmap.getWidth();<br style="line-height:25px"> System.out.println("width:"+width);<br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px">bytenumXDivs=4;<br style="line-height:25px"> bytenumYDivs=4;<br style="line-height:25px"> bytenumColors=numXDivs;<br style="line-height:25px"> intpaddingLeft=10;<br style="line-height:25px"> intoffset=0;<br style="line-height:25px"> numColors=(byte)((numColors+1)*(numYDivs+1));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">int</span><span style="color:#3366ff; line-height:25px">size=32+(numXDivs+numYDivs+numColors)*4;<br style="line-height:25px"> bytechunk[]=newbyte[size];<br style="line-height:25px"> chunk[0]=0;<br style="line-height:25px"> chunk[1]=numXDivs;<br style="line-height:25px"> chunk[2]=numYDivs;<br style="line-height:25px"> chunk[3]=numColors;<br style="line-height:25px"> bytepVal=0;<br style="line-height:25px"> chunk[4]=pVal;<br style="line-height:25px"> chunk[5]=pVal;<br style="line-height:25px"> chunk[6]=pVal;<br style="line-height:25px"> chunk[7]=pVal;<br style="line-height:25px"><br style="line-height:25px"> chunk[8]=pVal;<br style="line-height:25px"> chunk[9]=pVal;<br style="line-height:25px"> chunk[10]=pVal;<br style="line-height:25px"> chunk[11]=pVal;<br style="line-height:25px"> offset=12;<br style="line-height:25px"> putInt(chunk,offset,paddingLeft);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> offset=32;<br style="line-height:25px"> putInt(chunk,offset,10);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,30);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,40);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,width);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"><br style="line-height:25px"> putInt(chunk,offset,11);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,18);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,30);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> putInt(chunk,offset,56);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> intcolor=0x00000F;<br style="line-height:25px"> intcolor2=0xFFFFF0;<br style="line-height:25px"> intcolorInc=(color2-color)/numColors;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(inti=0;i<numColors;i++)<br style="line-height:25px"> {<br style="line-height:25px"> putInt(chunk,offset,0x2F000000|color);<br style="line-height:25px"> offset+=4;<br style="line-height:25px"> color+=colorInc;<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px">img9Patch=newNinePatch(bitmap,chunk,null);</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">代码2</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">putInt</span><span style="color:#3366ff; line-height:25px">(bytearray[],intoffset,intval)<br style="line-height:25px"> {<br style="line-height:25px"> array[offset]=(byte)(val&0xFF);<br style="line-height:25px"> val=val>>8;<br style="line-height:25px"> array[offset+1]=(byte)(val&0xFF);<br style="line-height:25px"> val=val>>8;<br style="line-height:25px"> array[offset+2]=(byte)(val&0xFF);<br style="line-height:25px"> val=val>>8;<br style="line-height:25px"> array[offset+3]=(byte)(val&0xFF);<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">代码3</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">RectFrect=newRectF(0,0,Game.width,Game.height);<br style="line-height:25px"> img9Patch.draw(canvas,rect);</span><br style="line-height:25px"><span style="line-height:25px">注意1</span>:rect是要把img9Patch画到的区域。 </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>