Drawable资源类型之Level List和Clip Drawable

Level List

    管理若干可替代Drawables的Drawable,每个都分配一个最大的数字。采用setLevel()设置Drawable的Level值的方式,就可以装载android:maxLevel大于等于传递的level的level list中的drawable资源。

文件位置:

    res/drawable/filename.xml

这个文件名作为资源ID使用。

编译后的资源类型:

   资源指向LevelListDrawable

资源引用方式:

   在Java中,R.drawable.filename

   在XML中,@[package:]drawable/filename

语法:

<?xml version="1.0"encoding="utf-8"?>
<level-list
   xmlns:android="http://schemas.android.com/apk/res/android">
   <item
       android:drawable="@drawable/drawable_resource"
        android:maxLevel="integer"
        android:minLevel="integer"/>
</level-list>

元素:

   <level-list>

   必须作为根元素。包含一个或多个<item>元素。

   属性:

        xmlns:android

        字符串。必须指定。定义XML的命名空间,必须是

        “http://schemas.android.com/apk/res/android”.

   <item>

        属性:

        android:drawable

            Drawable资源。必须指定。引用添加的资源。

        android:maxLevel

            整数。这项允许的最大level。

       android:minLevel

            整数。这项允许的最小level。

例子:

<?xml version="1.0"encoding="utf-8"?>
<level-listxmlns:android="http://schemas.android.com/apk/res/android" >
   <item
       android:drawable="@drawable/status_off"
        android:maxLevel="0" />
   <item
       android:drawable="@drawable/status_on"
        android:maxLevel="1" />
</level-list>

一旦被应用到视图,这个level就能被setLevel()或setImageLevel()修改。

SEE ALSO

   LevelListDrawable

Clip Drawable

    在XML中定义的drawable,这个drawable会基于这个drawable当前的Level来裁剪另一个Drawable。你能基于这个Level控制这个子drawable在宽和高方面裁剪的尺寸,以及通过gravity控制它将位于容器中的哪个位置。一般我们用它来设置进度图片

文件位置:

    res/drawable/filename.xml

    这个文件名作为资源ID使用。

编译后的资源类型:

   资源指向ClipDrawable

资源引用方式:

   在Java中,R.drawable.filename

   在XML中,@[package:]drawable/filename

语法:

<?xml version="1.0"encoding="utf-8"?>
<clip
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:drawable="@drawable/drawable_resource"
   android:clipOrientation=["horizontal" | "vertical"]
   android:gravity=["top" | "bottom" | "left"| "right" | "center_vertical" |
                     "fill_vertical"| "center_horizontal" | "fill_horizontal" |
                     "center" |"fill" | "clip_vertical" | "clip_horizontal"]/>

元素:

例子:

保存在res/drawable/clip.xml中的XML:

<?xml version="1.0"encoding="utf-8"?>
<clipxmlns:android="http://schemas.android.com/apk/res/android"
   android:drawable="@drawable/android"
   android:clipOrientation="horizontal"
   android:gravity="left" />

下面是在布局中将它应用到视图上:

<ImageView
   android:id="@+id/image"
   android:background="@drawable/clip"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content" />

为了渐进地显示这张图片,下面的代码获取Drawable,然后增大裁剪值:

ImageView imageview = (ImageView)findViewById(R.id.image);
ClipDrawable drawable =(ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel()+ 1000);

增大这个Level将减少裁剪的尺寸直至完整显示图片。下方是7000的Level:


注意:默认值是0,也就是完全裁掉,即图片不可见。当这个Level10000时,这张图片就不会被裁掉,并且完全可见。

SEE ALSO

   ClipDrawable


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值