ClipDrawable 是对一个Drawable进行剪切操作,可以控制这个drawable的剪切区域,以及相相对于容器的对齐方式,android中的进度条就是使用一个ClipDrawable实现效果的,它根据level的属性值,决定剪切区域的大小。
需要注意的是ClipDrawable是根据level的大小控制图片剪切操作的,官方文档的note中提到:The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000。也就是level的大小从0到10000,level为0时完全不显示,为10000时完全显示。是用Drawable提供的setLevel(int level)方法来设置剪切区域。
-
文件位置:
-
res/drawable/filename.xml
文件名即资源ID
编译资源类型:
-
指向
ClipDrawable
的指针
资源引用:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
语法
-
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">clip</a><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@drawable/</span><em>drawable_resource</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:clipOrientation</span><span class="pun">=</span><span class="atv">["horizontal"</span><span class="pln"> | </span><span class="atv">"vertical"</span><span class="pln">] </span><span class="atn">android:gravity</span><span class="pun">=</span><span class="atv">["top"</span><span class="pln"> | </span><span class="atv">"bottom"</span><span class="pln"> | </span><span class="atv">"left"</span><span class="pln"> | </span><span class="atv">"right"</span><span class="pln"> | </span><span class="atv">"center_vertical"</span><span class="pln"> | </span><span class="atv">"fill_vertical"</span><span class="pln"> | </span><span class="atv">"center_horizontal"</span><span class="pln"> | </span><span class="atv">"fill_horizontal"</span><span class="pln"> | </span><span class="atv">"center"</span><span class="pln"> | </span><span class="atv">"fill"</span><span class="pln"> | </span><span class="atv">"clip_vertical"</span><span class="pln"> | </span><span class="atv">"clip_horizontal"</span><span class="pln">] </span><span class="tag">/></span>
元素:
- 示例:
-
文件保存在
res/drawable/clip.xml
:<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><clip</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@drawable/android"</span><span class="pln"> </span><span class="atn">android:clipOrientation</span><span class="pun">=</span><span class="atv">"horizontal"</span><span class="pln"> </span><span class="atn">android:gravity</span><span class="pun">=</span><span class="atv">"left"</span><span class="pln"> </span><span class="tag">/></span>
在layout xml文件中使用:
<span class="tag"><ImageView</span><span class="pln"> </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/image"</span><span class="pln"> </span><span class="atn">android:background</span><span class="pun">=</span><span class="atv">"@drawable/clip"</span><span class="pln"> </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln"> </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln"> </span><span class="tag">/></span>
下面的代码或得到clipDrawable对象,并且增加裁剪区逐步显示图片:
<span class="typ">ImageView</span><span class="pln"> imageview </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ImageView</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">image</span><span class="pun">);</span><span class="pln"> </span><span class="typ">ClipDrawable</span><span class="pln"> drawable </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ClipDrawable</span><span class="pun">)</span><span class="pln"> imageview</span><span class="pun">.</span><span class="pln">getDrawable</span><span class="pun">();</span><span class="pln"> drawable</span><span class="pun">.</span><span class="pln">setLevel</span><span class="pun">(</span><span class="pln">drawable</span><span class="pun">.</span><span class="pln">getLevel</span><span class="pun">()</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1000</span><span class="pun">);</span>
逐步增加裁剪区域,逐步显示图片。这个裁剪区是7000:
注意:默认的level是0,表示全部裁剪掉了,图片不可见,但是显示的时候,依然会占据位置。当level是10,000,图片相当于没裁剪,完全可见。
参考: