android中drawable资源的解释及例子

文章总结的不错,转自:http://blog.csdn.net/wode_dream/article/details/38584693
drawable资源共有10种,包括Bitmap文件、Nine-Path文件、Layer List、State List、Level list、Transition Drawable、Inset Drawable、Clip Drawable、Scale Drawable、Shape Drawable。下面分别介绍下各种文件的用法和其中主要属性的作用:

一、Bitmap文件:就是普通的jpg、png和gif图片文件;

二、Nine-Path文件:以.9.png结尾的图片文件,其中图片中有够伸缩的区域,可以根据内容改变图片大小。在android sdk的tools目录下有一个draw9patch.bat可以制作9.png图片;

三、Layer List: 可以用于把多张图片组合成一张图片,例如:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red" android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green" android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue" android:gravity="center" />
    </item>
</layer-list>

四、State List:作用是在相同的图形中展示不同的图片,比如ListView中的子项背景,可以设置点击时是一种背景,没有焦点时是另一种背景。例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] >
    <item  android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true" android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

五、Level list:可以通过程序imageView.getDrawable().setImageLevel(value)来设置需要在ImageView中显示的图片(在xml中声明的图片)。例子:

<?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>

可以在程序中设置imageView.getDrawable().setImageLevel(0)或imageView.getDrawable().setImageLevel(1)来切换图片。
六、Transition Drawable:可以通过调用startTransition()和reverseTransition()实现两张图片的切换。例子:
XML文件存放在res/drawable/transition.xml

<?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>

在XML中的引用:

<ImageButton  android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/transition" />

在程序中的使用

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

七、Inset Drawable:用于通过指定的间距把图片插入到XML中,它在View需要比自身小的背景时常用。有些像padding的作用。例子:
第一步:drawable文件中建立inset_drawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/photo2" android:insetTop="100dp" android:insetRight="100dp" android:insetBottom="200dp" android:insetLeft="100dp" />

第二部,在xml中引用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/inset_drawable">

八、Clip Drawable:可以剪载图片显示,例如,可以通过它来做进度度。你可以选择是从水平或垂直方向剪载。其中的gravity设置从整个部件的哪里开始。例子:
第一步,在drawable文件中建立:clip_drawable.xml

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

第二步,在ImageView中引用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> 

        <ImageView android:id="@+id/clipimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/clip_drawable"/> 
</LinearLayout>

Dev Guide中在ImageView中设置的是android:background=”@drawable/clip_drawable”,但是我使用background的时,会在程序中报空指针的错误。
最后,使用程序控制:

 ImageView imageView=(ImageView)findViewById(R.id.clipimage);
        ClipDrawable clipDrawable=(ClipDrawable)imageView.getDrawable();
        clipDrawable.setLevel(5000);

level的值为0到10000 。当值为10000时图全部显示。

九、Scale Drawable:在原图的基础上改变图片的大小。例子:
第一步:drawable文件中建立scale_drawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/test_img" android:scaleGravity="center_vertical|center_horizontal" android:scaleHeight="50%" android:scaleWidth="80%" />

第二步:在xml中引用

<ImageView  android:id="@+id/scaleimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/scale_drawable"/>

第三步,在程序中设置level

ImageView scaleImage=(ImageView)findViewById(R.id.scaleimage);
        ScaleDrawable scale=(ScaleDrawable)scaleImage.getDrawable();
        scale.setLevel(10000);

这里设置level为10000表示可以整个显示图片。

十、Shape Drawable:在xml中定义图形。可以自定义一个图形,包括边框、渐变、圆角等。例子:
第一步:shape_drawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient  android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="45"/>
    <padding  android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" />
    <corners  android:radius="8dp" />
</shape>

第二步:xml中引用

<TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="shape例子" android:background="@drawable/shape_drawable"/>

你可能感兴趣的:(drawable)