Drawable的应用整理
来源:互联网 发布:scd数据库期刊 编辑:程序博客网 时间:2024/06/16 13:36
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>
<?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>
<?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>
六、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
<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" />
<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>
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%" />
<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"
<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"/>
0 0
- Drawable的应用整理
- android 自定义Drawable Shape 的应用
- Android中Drawable资源的应用
- Android 的drawable state 应用(selector)
- Android Drawable 那些不为人知的高效用法(Drawable实现圆角,应用更广)
- android drawable 应用
- android drawable高级应用
- Android资源文件-drawable整理
- Android的R.drawable应用——制作NinePatch图片
- android开发 drawable中XML的相关应用
- android开发 drawable中XML的相关应用
- android开发 drawable中XML的相关应用
- android开发 drawable中XML的相关应用
- 创建Material Design风格的Android应用--使用Drawable
- 创建Material Design风格的Android应用--使用Drawable
- android开发 drawable中XML的相关应用
- Android学习笔记--应用资源的使用(Drawable)
- Android的R.drawable应用——制作NinePatch图片
- 操作系统分页存储管理
- ubuntu 下安装配置jdk的shell脚本程序
- 随机梯度下降和批量梯度下降的简单代码实现
- Linux下fdisk磁盘分区
- Nova and RabbitMQ
- Drawable的应用整理
- 【水水】HDU5665Lucky
- wustoj 1583: Sharing! 思维
- mysql服务性能优化—my.cnf配置说明详解(16G内存)
- 关于Android Studio import时出现cannot find settings.jar错误
- 数据结构_对称矩阵
- UML建模学习2:UML基本构造块之事物
- UVA 815 Flooded!
- VS2015 xamarin android axml 文件添加智能提示