您的位置:首页 > 移动开发 > Android开发

[转]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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: