android中drawable资源的解释及例子

        文章中的内容参考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"?>
    <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 
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"/>








  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这些文件夹是Android资源文件夹,用于存放应用程序所需的各种资源文件,例如图片、布局文件、颜色值、字符串等等。其drawable-v21和drawable-v24是用于存放特定版本的Android系统所需的图片资源文件,下面是它们的区别: - drawable-v21:这个文件夹是用于存放Android 5.0(API level 21)及以上版本所需的图片资源文件。通常,这些图片资源文件用于支持Material Design风格的UI设计,如矢量图标、状态选择器等。 - drawable-v24:这个文件夹是用于存放Android 7.0(API level 24)及以上版本所需的图片资源文件。它主要用于支持Android Nougat(7.0)引入的多窗口模式,以及支持新的设备像素密度。 如果你的应用程序需要支持不同版本的Android系统,那么你可以将特定版本所需的图片资源文件放置在对应的文件夹,这样系统会根据设备的Android版本来自动加载对应版本的图片资源文件。 举个例子,如果你想在Android 5.0及以上版本使用Material Design风格的矢量图标,那么你可以将这些图片资源文件放置在drawable-v21文件夹。而如果你想在Android 7.0及以上版本支持多窗口模式,那么你可以将相关的图片资源文件放置在drawable-v24文件夹。 在使用这两个文件夹时,你需要注意以下几点: - 文件夹名称必须按照规定的格式命名,即“drawable-vxx”,其xx代表Android版本号。 - 如果你在drawable文件夹也有相同名称的图片资源文件,那么系统会优先加载drawable-vxx文件夹对应版本的图片资源文件。 - 如果你的应用程序只支持较低版本的Android系统,那么你可以将所有图片资源文件都放置在drawable文件夹,系统会自动加载适合当前设备的版本的图片资源文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值