ClipDrawable

public class

ClipDrawable

extends Drawable
implements Drawable.Callback
java.lang.Object
? android.graphics.drawable.Drawable
? android.graphics.drawable.ClipDrawable
ClassOverview

A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars, by increasing the drawable's level withsetLevel().

Note:The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000.

It can be defined in an XML file with the<clip>element. For more information, see the guide toDrawable Resources.

ClipDrawable是Drawable的子类,我通过它可以设置Drawable的clip.

<wbr style="line-height:25px">构造函数</wbr><wbr style="line-height:25px">: <div 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/drawable/ClipDrawable.html#ClipDrawable(android.graphics.drawable.Drawable,%20int,%20int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ClipDrawable</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Drawable</a>drawable, int gravity, int orientation)<br style="line-height:21px"><span style="line-height:21px"><span style="color:#993300; line-height:21px">参数</span></span><br style="line-height:21px"><span style="color:#ff9900; line-height:21px">orientation</span> Bitwise-or of HORIZONTAL and/or VERTICAL</nobr> </td> </tr> </tbody></table> <span style="line-height:25px"><wbr style="line-height:25px">主要函数有:<br style="line-height:25px"> publicfinalbooleansetLevel(intlevel)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Since:APILevel1<br style="line-height:25px"> Specifythelevelforthedrawable.Thisallowsadrawabletovaryitsimagerybasedonacontinuouscontroller,<br style="line-height:25px"> forexampletoshowprogressorvolumelevel.<br style="line-height:25px"> IfthenewlevelyouaresupplyingcausestheappearanceoftheDrawabletochange,thenitisresponsibleforcallinginvalidateSelf()inordertohaveitselfredrawn,andtruewillbereturnedfromthisfunction.<br style="line-height:25px"> Parameters<br style="line-height:25px"> levelThenewlevel,from0(minimum)to10000(maximum).<br style="line-height:25px"> Returns<br style="line-height:25px"><br style="line-height:25px"> *ReturnstrueifthischangeinlevelhascausedtheappearanceoftheDrawabletochange(hencerequiringaninvalidate),otherwisereturnsfalse.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意</wbr></span><wbr style="line-height:25px">:必须使用setLevel来设置cliping,注意Thenewlevel,from0(minimum)to10000(maximum).</wbr></wbr> </div> <div style="line-height:25px">该方法继承于Drawable。<br style="line-height:25px"><span style="line-height:25px">ClipDrawable的使用</span><br style="line-height:25px"> AdrawabledefinedinXMLthatclipsanotherdrawablebasedonthisDrawable'scurrentlevel.Youcancontrolhowmuchthechilddrawablegetsclippedinwidthandheightbasedonthelevel,aswellasagravitytocontrolwhereitisplacedinitsoverallcontainer.Mostoftenusedtoimplementthingslikeprogressbars.<br style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">filelocation</span>:<br style="line-height:25px"> res/drawable/filename.xml<br style="line-height:25px"> ThefilenameisusedastheresourceID.<br style="line-height:25px"> compiledresource<span style="color:#993300; line-height:25px">datatype</span>:<br style="line-height:25px"> ResourcepointertoaClipDrawable.<br style="line-height:25px"><span style="color:#993300; line-height:25px">resourcereference</span>:<br style="line-height:25px"> InJava:R.drawable.filename<br style="line-height:25px"> InXML:@[package:]drawable/filename<br style="line-height:25px"><span style="color:#993300; line-height:25px">syntax</span>:<br style="line-height:25px"><br style="line-height:25px"> &lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">&lt;</span><span style="color:#ff00ff; line-height:25px">clip</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">xmlns:android</span><span style="color:#0000ff; line-height:25px">="http://schemas.android.com/apk/res/android"</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">android:drawable</span><span style="color:#0000ff; line-height:25px">="@drawable/drawable_resource"</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">android:clipOrientation</span><span style="color:#0000ff; line-height:25px">=["horizontal"|"vertical"]</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">android:gravity</span><span style="color:#0000ff; line-height:25px">=["top"|"bottom"|"left"|"right"|"center_vertical"|</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">"fill_vertical"|"center_horizontal"|"fill_horizontal"|</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">"center"|"fill"|"clip_vertical"|"clip_horizontal"]/&gt;</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"> attributes:<br style="line-height:25px"><span style="color:#993300; line-height:25px">xmlns:android</span><br style="line-height:25px"> String.Required.DefinestheXMLnamespace,whichmustbe"http://schemas.android.com/apk/res/android".<br style="line-height:25px"> 名字空间是固定的"<span style="color:#0000ff; line-height:25px">http://schemas.android.com/apk/res/android</span>"<br style="line-height:25px"><span style="color:#993300; line-height:25px">android:drawable</span><br style="line-height:25px"> Drawableresource.Required.Referencetoadrawableresourcetobeclipped.<br style="line-height:25px"> clipped所操作的drawable资源<br style="line-height:25px"><span style="color:#993300; line-height:25px">android:clipOrientation</span><br style="line-height:25px"> Keyword.Theorientationfortheclip.<br style="line-height:25px"><br style="line-height:25px"> Mustbeoneofthefollowingconstantvalues:<br style="line-height:25px"> ValueDescription<br style="line-height:25px"><span style="color:#993300; line-height:25px">horizontal</span>Clipthedrawablehorizontally.<br style="line-height:25px"><span style="color:#993300; line-height:25px">vertical</span>Clipthedrawablevertically.<br style="line-height:25px"><span style="color:#000080; line-height:25px">clip区域可变化的方向horizontally或vertically</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">android:gravity</span><br style="line-height:25px"> Keyword.Specifieswheretoclipwithinthedrawable.<br style="line-height:25px"> Mustbeoneormore(separatedby'|')ofthefollowingconstantvalues:<br style="line-height:25px"><span style="color:#993300; line-height:25px">top</span>Puttheobjectatthetopofitscontainer,notchangingitssize.<br style="line-height:25px"> WhenclipOrientationis"vertical",clippingoccursatthebottomofthedrawable.<br style="line-height:25px"><span style="color:#000080; line-height:25px">Drawable采用上对齐的方式。Drawable的大小不变。如果clipOrientation为"vertical"的话,则clipping只能向下</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">bottom</span>Puttheobjectatthebottomofitscontainer,notchangingitssize.<br style="line-height:25px"> WhenclipOrientationis"vertical",clippingoccursatthetopofthedrawable.<br style="line-height:25px"><span style="color:#000080; line-height:25px">Drawable采用下对齐的方式。Drawable的大小不变。如果clipOrientation为"vertical"的话,则clipping只能向上</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">left</span>Puttheobjectattheleftedgeofitscontainer,notchangingitssize.Thisisthedefault.<br style="line-height:25px"> WhenclipOrientationis"horizontal",clippingoccursattherightsideofthedrawable.<br style="line-height:25px"> Thisisthedefault.<br style="line-height:25px"><span style="color:#000080; line-height:25px">Drawable采用左对齐的方式。Drawable的大小不变。如果clipOrientation为"horizontal"的话,则clipping只能向右。它是默认的对齐方式。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">right</span>Puttheobjectattherightedgeofitscontainer,notchangingitssize.<br style="line-height:25px"> WhenclipOrientationis"horizontal",clippingoccursattheleftsideofthedrawable.<br style="line-height:25px"><span style="color:#000080; line-height:25px">Drawable采用右对齐的方式。Drawable的大小不变。如果clipOrientation为"horizontal"的话,则clipping只能向左。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">center_vertical</span>Placeobjectintheverticalcenterofitscontainer,notchangingitssize.<br style="line-height:25px"> Clippingbehavesthesameaswhengravityis"center".<br style="line-height:25px"><span style="color:#000080; line-height:25px">Drawable的vertical方向居中对齐。Drawable的大小不变。<br style="line-height:25px"> Clipping行为和"center"在clipOrientation时"vertical"一样。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">fill_vertical</span>Growtheverticalsizeoftheobjectifneededsoitcompletelyfillsitscontainer.<br style="line-height:25px"> WhenclipOrientationis"vertical",noclippingoccursbecausethedrawablefillstheverticalspace<br style="line-height:25px"> (unlessthedrawablelevelis0,inwhichcaseit'snotvisible).<br style="line-height:25px"> Drawable的vertical方向居中对齐。Drawable的大小会变化,<br style="line-height:25px"><span style="color:#000080; line-height:25px">它在vertical方向要stretch,以便在vertical方向填充它所在的容器。<br style="line-height:25px"> vertical方向没Clipping行为除非thedrawablelevelis0,这时它不可见。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">center_horizontal</span>Placeobjectinthehorizontalcenterofitscontainer,notchangingitssize.<br style="line-height:25px"> Clippingbehavesthesameaswhengravityis"center".<br style="line-height:25px"><span style="color:#000080; line-height:25px">Drawable的horizontal方向居中对齐。Drawable的大小不变。<br style="line-height:25px"> Clipping行为和"center"在clipOrientation时"horizontal"一样。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">fill_horizontal</span>Growthehorizontalsizeoftheobjectifneededsoitcompletelyfillsitscontainer.<br style="line-height:25px"> WhenclipOrientationis"horizontal",noclippingoccursbecausethedrawablefillsthehorizontalspace<br style="line-height:25px"> (unlessthedrawablelevelis0,inwhichcaseit'snotvisible).<br style="line-height:25px"> Drawable的horizontal方向居中对齐。Drawable的大小会变化,<br style="line-height:25px"><span style="color:#000080; line-height:25px">它在horizontal方向要stretch,以便在vertical方向填充它所在的容器。<br style="line-height:25px"> horizontal方向没Clipping行为除非thedrawablelevelis0,这时它不可见。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">center</span>Placetheobjectinthecenterofitscontainerinboththeverticalandhorizontalaxis,notchangingitssize.<br style="line-height:25px"> WhenclipOrientationis"horizontal",clippingoccursontheleftandright.<br style="line-height:25px"> WhenclipOrientationis"vertical",clippingoccursonthetopandbottom.<br style="line-height:25px"> Drawable在horizontal和vertical方向都居中对齐。<br style="line-height:25px"><span style="color:#000080; line-height:25px">当clipOrientation是"horizontal"时,clipping发生在左右方向。<br style="line-height:25px"> 当clipOrientation是"vertica"时,clipping发生在上下方向。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">fill</span>Growthehorizontalandverticalsizeoftheobjectifneededsoitcompletelyfillsitscontainer.<br style="line-height:25px"> Noclippingoccursbecausethedrawablefillsthehorizontalandverticalspace<br style="line-height:25px"> (unlessthedrawablelevelis0,inwhichcaseit'snotvisible).<br style="line-height:25px"> Drawable在horizontal和vertical方向都就进行stretch以便填充它所在的容器。它没有clipping行为。<br style="line-height:25px"><span style="color:#993300; line-height:25px">clip_vertical</span>Additionaloptionthatcanbesettohavethetopand/orbottomedgesof<br style="line-height:25px"> thechildclippedtoitscontainer'sbounds.<br style="line-height:25px"> Theclipisbasedontheverticalgravity:atopgravityclipsthebottomedge,<br style="line-height:25px"> abottomgravityclipsthetopedge,andneitherclipsbothedges.<br style="line-height:25px"> 附加选项<br style="line-height:25px"><span style="color:#993300; line-height:25px">clip_horizonta</span>lAdditionaloptionthatcanbesettohavetheleftand/orrightedgesof<br style="line-height:25px"> thechildclippedtoitscontainer'sbounds.Theclipisbasedonthehorizontalgravity:<br style="line-height:25px"> aleftgravityclipstherightedge,arightgravityclipstheleftedge,andneitherclipsbothedges.<br style="line-height:25px"> 附加选项<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:在程序中使用fill_vertical和center_vertical,发现他们居然没差别,无论手动的画,还通过控件画。<br style="line-height:25px"> fill_horizontal和center_horizontal同理。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:上面关于ClipDrawable的大小的讨论(来自文档)在实际使用发现<span style="line-height:25px"><wbr style="line-height:25px">毫无意义</wbr></span><wbr style="line-height:25px">,<br style="line-height:25px"><span style="color:#000080; line-height:25px">在实际的程序中,系统是先把Drawable发到它的Bounds或容器大小时,才来讨论的clipping问题</span><br style="line-height:25px"> example:<br style="line-height:25px"> XMLfilesavedatres/drawable/clip.xml:<br style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;</span><span style="color:#ff6600; line-height:25px">clip</span><span style="color:#3366ff; line-height:25px">xmlns:android="http://schemas.android.com/apk/res/android"<br style="line-height:25px"> android:drawable="@drawable/android"<br style="line-height:25px"> android:clipOrientation="horizontal"<br style="line-height:25px"> android:gravity="left"&gt;<br style="line-height:25px"> &lt;/</span><span style="color:#ff6600; line-height:25px">clip</span><span style="color:#3366ff; line-height:25px">&gt;</span><br style="line-height:25px"> 在文档中居然是用的"shape",而非"clip".经验证"clip"才对,“shape”是错误的。<br style="line-height:25px"> 文档居然也有种低级错误。<br style="line-height:25px"> ThefollowinglayoutXMLappliestheclipdrawabletoaView:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;ImageView<br style="line-height:25px"> android:id="@+id/image"<br style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">android:background</span><span style="color:#3366ff; line-height:25px">="</span><span style="color:#808000; line-height:25px">@drawable/clip</span><span style="color:#3366ff; line-height:25px">"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:layout_width="wrap_content"/&gt;<br style="line-height:25px"></span><br style="line-height:25px"> Thefollowingcodegetsthedrawableandincreasestheamountofclippinginordertoprogressivelyrevealtheimage:<br style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ImageViewimageview=(ImageView)findViewById(R.id.image);<br style="line-height:25px"> ClipDrawabledrawable=(ClipDrawable)imageview.getDrawable();<br style="line-height:25px"> drawable.</span><span style="color:#ff6600; line-height:25px">setLevel</span><span style="color:#3366ff; line-height:25px">(drawable.getLevel()+1000);</span><br style="line-height:25px"> Increasingthelevelreducestheamountofclippingandslowlyrevealstheimage.Hereitisatalevelof7000:<br style="line-height:25px"><span style="line-height:25px">示例2</span>:<br style="line-height:25px"> loading.xml文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;</span><span style="color:#ff6600; line-height:25px">clip</span><span style="color:#3366ff; line-height:25px">xmlns:android="http://schemas.android.com/apk/res/android"<br style="line-height:25px"> android:drawable="@drawable/load"<br style="line-height:25px"> android:clipOrientation="horizontal"<br style="line-height:25px"> android:gravity="fill_vertical"&gt;<br style="line-height:25px"> &lt;/</span><span style="color:#ff6600; line-height:25px">clip</span><span style="color:#3366ff; line-height:25px">&gt;</span><br style="line-height:25px"> 代码1:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ClipDrawableloadDrawable;<br style="line-height:25px"> intloadDrawableLevel=0;<br style="line-height:25px"> finalstaticintlevelMax=10000;</span><br style="line-height:25px"> 代码2:<br style="line-height:25px"> l<span style="color:#3366ff; line-height:25px">oadDrawable=(</span><span style="color:#ff6600; line-height:25px">ClipDrawable</span><span style="color:#3366ff; line-height:25px">)context.getResources().getDrawable(R.drawable.loading);</span><br style="line-height:25px"> 代码3:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Rectbounds=null;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(loadDrawableLevel&lt;levelMax)<br style="line-height:25px"> {<br style="line-height:25px"> loadDrawableLevel+=10;;<br style="line-height:25px"> loadDrawable.setLevel(loadDrawableLevel);<br style="line-height:25px"> bounds=newRect(0,0,Game.width,Game.height);<br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px">loadDrawable.setBounds(bounds);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">loadDrawableLevel=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">loadDrawable.draw(canvas);</span><br style="line-height:25px"> 参考资料:http://developer.android.com/guide/topics/resources/drawable-resource.html</wbr></wbr></wbr></wbr> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值