[转]android中drawable资源的解释及例子
2015-09-19 22:09
375 查看
原文链接: 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);
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" />
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"/>
文章中的内容参考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);
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" />
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之AnimationSet
- 当dialog挡住activity时的 键盘隐藏
- 【Android小游戏】安卓小游戏开发(直播手把手系列二)
- Android核心基础-10.通知
- Android Studio 百度地图开发
- Android注释技巧
- Android 动画之一 Drawable Animation —— 逐帧(Frame)动画
- 高速建成Android开发环境ADT-Bundle和Hello World
- 轻松学会android中补间动画
- android的存储
- Android Intent分享文件|分享图片|分享文字|功能的实现
- 个人日记-安卓里OnPageChangeListener的坑爹
- android性能测试内存泄漏
- Android中BaseActivity的用法
- Android FrameWork中的SP、RefBase、weakref_impl,Thread类
- Android游戏开发中常用的三种视图
- Android 应用内多进程实现
- Android智能指针sp wp详解
- android studio 使用库
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解