原文链接:         http://blog.csdn.net/wode_dream/article/details/38584693

文章中的内容参考Dev Guide中的Drawable Resources,英文好的朋友可以直接去读英文。总结这篇文章的目的是自己在使用drawable资源遇到一些问题跟大家分享下,同时整理下自己对drawable的理解。

        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"?>
<bitmap android:src="@drawable/android_red"
</item>
</item>
</item>
</layer-list>
 
四、State List:作用是在相同的图形中展示不同的图片,比如ListView中的子项背景,可以设置点击时是一种背景,没有焦点时是另一种背景。例如:
<?xml version="1.0" encoding="utf-8"?>
<item
</selector>

<?xml version="1.0" encoding="utf-8"?>
<item android:state_focused="true"
<item android:state_hovered="true"
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
 
五、Level list:可以通过程序imageView.getDrawable().setImageLevel(value)来设置需要在ImageView中显示的图片(在xml中声明的图片)。例子:
<?xml version="1.0" encoding="utf-8"?>

android:drawable="@drawable/status_off"

<item

</level-list>

可以在程序中设置imageView.getDrawable().setImageLevel(0)或imageView.getDrawable().setImageLevel(1)来切换图片。

<?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);
drawable.startTransition(500);

 
android:drawable="@drawable/photo2"
android:insetTop="100dp"
android:insetRight="100dp"
android:insetBottom="200dp"
android:insetLeft="100dp" />
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">
        android:drawable="@drawable/test_img"

        android:layout_width="match_parent" 
                android:id="@+id/clipimage" 
</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);
        android:drawable="@drawable/test_img"

第二步:在xml中引用
<ImageView
第三步,在程序中设置level
        ImageView scaleImage=(ImageView)findViewById(R.id.scaleimage);
        ScaleDrawable scale=(ScaleDrawable)scaleImage.getDrawable();
        scale.setLevel(10000);
 
android:shape="rectangle">
<gradient
android:angle="45"/>
<padding
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"/>

[转]android中drawable资源的解释及例子的更多相关文章

  1. Android之旅十六 android中各种资源的使用

    android中各种资源的使用: 在android开发中,各种资源的合理使用应该在各自的xml中进行定义,以便反复使用; 字符串资源:strings.xml,xml中引用:@string/XXX,ja ...

  2. Android中的资源文件

    最近复习Android资源文件的内容,留下点记录以备后用. Android中的资源主要是指存放在应用程序或者Framework相应包下/res中的内容.它们可以被本地化,如果必要的话会被编译成二进制文 ...

  3. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  4. Android中的资源访问

    Android中的资源是指非代码部分,指外部文件. assets中保存的一般是原生的文件,例如MP3文件,Android程序不能直接访问,必须通过AssetManager类以二进制流的形式来读取. r ...

  5. 今天给大家分享一下Android中的资源与国际化的问题

    摘要:该文章将向大家分享Android中的资源与国际化的问题. 今天给大家分享一下Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layo ...

  6. Android中的资源与国际化!

    Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layout与values目录,layout里的xml文件的我们应用使用布局的文件,val ...

  7. Android中时间戳的详细解释

    Android中时间戳的详细解释: (1).定义: 时间戳就是根据当前系统时间生成的一组随机数字. (2).作用: 作为对数据唯一性的一种判断依据.避免了重复修改数据所带来的错误! (3).应用: ( ...

  8. Android中Drawable分类汇总(上)

    Android把可绘制的对象抽象为Drawable,不同的图形图像资源就代表着不同的drawable类型.Android FrameWork提供了一些具体的Drawable实现,通常在代码中都不会直接 ...

  9. android的drawable资源

    1.android中可以通过xml文件配置资源,比如字符串啦,整数拉.浮点数等等,当然也可以配置图片资源和选择器,下面我们就看看几种图片资源的配置. @1矩形方框,带渐变色的配置代码 <?xml ...

随机推荐

  1. Android中注册获取验证码倒计时按钮

    public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...

  2. 16 - 文件操作-StringIO-BytesIO

    目录 1 文件操作 1.1 open函数介绍 1.2 打开操作 1.2.1 mode模式 1.2.2 文件指针 1.2.3 缓冲区 1.2.4 encoding编码 1.2.5 其他参数 1.3 读写 ...

  3. ab的使用方法【转】

    使用方法 ab -n 800 -c 800  http://192.168.0.10/ (-n发出800个请求,-c模拟800并发,相当800人同时访问,后面是测试url) ab -t 60 -c 1 ...

  4. 初学Memcached安装及使用【转】

    1.yum install memcached安装memecached 2.chkconfig memcached on设置memcached开机启动 3.service memcached star ...

  5. 2017 SWERC

    2017 SWERC A:Cakey McCakeFace 题目描述:有一个炉每次只能放一个蛋糕,炉的进口和出口各放了一个探测器,当放蛋糕进去时,进口的探测器会记录时刻,当蛋糕做好后,蛋糕从出口出来, ...

  6. caffe Python API 之Inference

    #以SSD的检测测试为例 def detetion(image_dir,weight,deploy,resolution=300): caffe.set_mode_gpu() net = caffe. ...

  7. Oracle安装出现报错

    报错信息如下: >>> Couldnot execute auto check for display colors using command /usr/bin/xdpyinfo. ...

  8. Nginx源码分析--epoll模块

    Nginx采用epoll模块实现高并发的网络编程,现在对Nginx的epoll模块进行分析. 定义在src/event/modules/ngx_epoll_module.c中 1. epoll_cre ...

  9. python进行des加密解密,而且可以与JAVA进行互相加密解密

    import binasciifrom pyDes import des, CBC, PAD_PKCS5import uuidimport time # pip install -i https:// ...

  10. day5冒泡排序

    冒泡排序:是一种基础的算法,实现数据的排序,排序的原则是前一个与后一个进行比较,如果前面的值大则交换,否则不交换,多次循环每次把最大的数据循环至后面就能够完成所需. 上面的图是冒泡排序的原理,每次循环 ...