Drawable Resource类型

Bitmap File

A bitmap graphic file (.png.jpg, or .gif). Creates a BitmapDrawable.

一般用于ImageView:src或以图片资源作背景

Nine-Patch File

A PNG file with stretchable regions to allow image resizing based oncontent (.9.png). Creates aNinePatchDrawable.

适用于图片平铺

Layer List

A Drawable that manages an array of other Drawables. These are drawnin array order, so the element with the largest index is be drawn on top.Creates a LayerDrawable.

图层的叠加


 

State List

An XML file that references different bitmap graphics for differentstates (for example, to use a different image when a button is pressed).Creates a StateListDrawable.

在按钮样式中用到比较普遍,相应动作。可结合Shape使用

Level List

An XML file that defines a drawable that manages a number of alternateDrawables, each assigned a maximum numerical value. Creates a LevelListDrawable.

貌似用于显示不同的drawable resource

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns: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>

Once this is applied to a View, the level can be changedwith setLevel() or setImageLevel().

Transition Drawable

An XML file that defines a drawable that can cross-fade between twodrawable resources. Creates aTransitionDrawable.

用于图片切换

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>

Andthe following code performs a 500ms transition from the first item to thesecond:

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable =(TransitionDrawable) button.getDrawable();
drawable.startTransition(500);


Inset Drawable

An XML file that defines a drawable that insets another drawable by aspecified distance. This is useful when a View needs a background drawble thatis smaller than the View's actual bounds.

用于在固定的bound里设置比该bound尺寸小的drawable resource 的位置

Clip Drawable

A drawable defined in XML that clips another drawable based on thisDrawable's current level. You can control how much the child drawable getsclipped in width and height based on the level, as well as a gravity to controlwhere it is placed in its overall container. Most often used to implementthings likeprogress bars.

EXAMPLE:

XML file saved at res/drawable/clip.xml:

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

The followinglayout XML applies the clip drawable to a View:

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

The followingcode gets the drawable and increases the amount of clipping in order toprogressively reveal the image:

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

Increasing thelevel reduces the amount of clipping and slowly reveals the image. Here it isat a level of 7000:


Note: Thedefault level is 0, which is fully clipped so the image is not visible. Whenthe level is 10,000, the image is not clipped and completely visible.

 

Scale Drawable

A drawable defined in XML that changes the size of another drawablebased on its current level.

Shape Drawable

An XML file that defines a geometric shape, including colors andgradients. Creates a ShapeDrawable.

常用于边框设置,比如圆角,也可用于虚线的绘制

<?xmlversion="1.0" encoding="utf-8"?>
<!-- 带圆角 白色背景 黄色色边框  长方体 -->
<layer-listxmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solidandroid:color="#FFFFFF" />
            <corners
               android:topLeftRadius="5dp"
               android:topRightRadius="5dp"
               android:bottomRightRadius="5dp"
               android:bottomLeftRadius="5dp" />
            <strokeandroid:width="2dp" android:color="@color/theme_color"/>
            <padding
                android:left="2dp"
                android:top="2dp"
                android:right="2dp"
                android:bottom="2dp"/>
        </shape>
    </item>
</layer-list>
<?xmlversion="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
   <!--显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线-->
    <stroke
        android:dashGap="3dp"
        android:dashWidth="3dp"
        android:width="0.5dp"
       android:color="@color/split_line" />
    <!-- 虚线的高度 -->
    <size android:height="0.5dp"/>
</shape>

Note: A color resource can also be used as a drawable inXML. For example, when creating a state list drawable, you can reference a color resource forthe android:drawable attribute (android:drawable="@color/green").

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值