Android Drawable Resource学习(九)、ClipDrawable

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>
元素:
<clip>
定义这是个ClipDrawable,必须作为根元素。

属性:

xmlns:android
String类型。必须的,定义XML文件的命名空间,必须是 "http://schemas.android.com/apk/res/android".
android:drawable
Drawable资源。 必须的。表示该ClipDrawable引用的drawable资源。
android:clipOrientation
关键字。 裁剪的方向。

必须是下面两种的数值之一:

描述
horizontal水平方向裁剪
vertical垂直方向裁剪
android:gravity
关键字。指定从哪个地方裁剪。

必须是下面一个或多个值(多个值之间用“|”分隔):

描述
top将这个对象放在容器的顶部,不改变其大小。当clipOrientation 是"vertical",裁剪发生在drawable的底部(bottom)
bottom将这个对象放在容器的底部,不改变其大小。当clipOrientation 是 "vertical"裁剪发生在drawable的顶部(top)
left将这个对象放在容器的左部,不改变其大小。当clipOrientation 是 "horizontal"裁剪发生在drawable的右边(right)。默认的是这种情况。
right将这个对象放在容器的右部,不改变其大小。当clipOrientation 是 "horizontal"裁剪发生在drawable的左边(left)。
center_vertical将对象放在垂直中间,不改变其大小。裁剪的情况和”center“一样。
fill_vertical垂直方向上不发生裁剪。(除非drawable的level是 0,才会不可见,表示全部裁剪完)
center_horizontal将对象放在水平中间,不改变其大小。裁剪的情况和”center“一样。
fill_horizontal水平方向上不发生裁剪。(除非drawable的level是 0,才会不可见,表示全部裁剪完)
center将这个对象放在水平垂直坐标的中间,不改变其大小。当clipOrientation 是 "horizontal"裁剪发生在左右。当clipOrientation是"vertical",裁剪发生在上下。
fill填充整个容器,不会发生裁剪。(除非drawable的level是 0,才会不可见,表示全部裁剪完)。
clip_vertical额外的选项,它能够把它的容器的上下边界,设置为子对象的上下边缘的裁剪边界。裁剪要基于对象垂直重力设置:如果重力设置为top,则裁剪下边,如果设置为bottom,则裁剪上边,否则则上下两边都要裁剪。
clip_horizontal额外的选项,它能够把它的容器的左右边界,设置为子对象的左右边缘的裁剪边界。裁剪要基于对象垂直重力设置:如果重力设置为right,则裁剪左边,如果设置为left,则裁剪右边,否则则左右两边都要裁剪。
示例:
文件保存在  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,图片相当于没裁剪,完全可见。

参考:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值