Drawable专讲 Android

来源:互联网 发布:神机妙算软件神木销售 编辑:程序博客网 时间:2024/04/29 15:10

Drawable资源


Drawable资源是对图像的一个抽象,你可以通过getDrawable(int)得到并绘制到屏幕上。这里有几种不同类型的Drawable:

Bitmap File

一个Bitmap图像文件(.png、.jpg或.gif)。BitmapDrawable。

Nine-Patch File

一个带有伸缩区域的PNG文件,可以基于content伸缩图片(.9.png)。NinePatchDrawable。

State List

一个XML文件,为不同的状态引用不同的Bitmap图像(例如,当按钮按下时使用不同的图片)。StateListDrawable。

Color

定义在XML中的资源,指定一个矩形(圆角可以有)的颜色。PaintDrawable。

Shape

一个XML文件,定义了一个几何形状,包括颜色和渐变。ShapeDrawable。

AnimationDrawable资源的说明在【Animation资源】文章中。

Bitmap File

基本的Bitmap图像。Android支持几种不同格式的Bitmap文件:.png(最佳)、.jpg(可接受)、.gif(不要)。

注意:Bitmap文件可能会被aapt工具进行无损图像压缩优化。例如,一个真彩色的PNG(不超过256色)可能会被转换成一个带有颜色板的8位PNG。这样做能保证图片质量一样,但减少内存占用。因此,需要了解的是放在这个文件夹下的二进制图像在编译时可能会发生变更。如果你打算以位流方式读取图像来转化成Bitmap的话,可以把它们放到res/raw文件中,在这里,它们不会被优化。

File Location:

res/drawable/filename.png (.png, .jpg, 或.gif)

文件名会被当作资源ID使用。

Complied Resource Datatype:

指向BitmapDrawable的资源指针。

Resource Reference:

R.drawable.filename(Java)

@[package:]drawable/filename(XML)

Example:

在res/drawable/myimage.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:

1234
[xml] view plaincopyprint?
  1.  <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><ImageView</strong></span>  
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_height</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span></span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_width</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span></span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:src</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/myimage"</span> <strong>/></strong></span>  

下面的代码可以以Drawable方式得到图片:

12
[java] view plaincopyprint?
  1. Resources res <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> getResources<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>  
  2.     Drawable drawable <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> res.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">getDrawable</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>R.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">drawable</span>.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">myimage</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>  

Nine-Patch File

NinePatch是一种PNG图像,可以定义拉伸区域,当View的content超出图像边界的话,Android会拉伸它。典型用法是把这个图像设置为View的背景,而这个View至少有一个尺寸设置为“wrap_content”,当这个View变大来容纳content时,Nine-Patch图像也会拉伸来匹配View的大小。

File Location:

res/drawable/filename.9.png

文件名将被当作资源ID使用。

Complied Resource Datatype:

指向NinePatchDrawable的资源指针。

Resource Reference:

R.drawable.filename(Java)

@[package:]drawable/filename(XML)

Example:

在res/drawable/myninepatch.9.png位置保存了一张图片,在Layout XML中可以应用这个图片到一个View上:

1234
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><Button</strong></span>  
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_height</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span></span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_width</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span></span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:background</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/myninepatch"</span> <strong>/></strong></span>  

State List

StateListDrawable是定义在XML中的Drawable对象,能根据状态来呈现不同的图像。例如,Button存在多种不同的状态(pressed、focused或other),使用StateListDrawable,你可以为Button的每个状态提供不同的按钮图像。

你可以在XML文件中描述状态列表。在
元素里的每个代表每个图像。每个使用不同的特性来描述使用的时机。

当每次状态改变时,StateList都会从上到下遍历一次,第一个匹配当前状态的item将被使用——选择的过程不是基于“最佳匹配”,只是符合state的最低标准的第一个item。

File Location

Res/drawable/filename.xml

文件名将被当作资源ID使用。

Complied Resource Datatype:

指向StateListDrawable的资源指针。

Resource Reference:

R.drawable.filename(Java)

@[package:]drawable/filename(XML)

Syntax:

12345678910111213141516
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><?xml</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">version</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"1.0"</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">encoding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"utf-8"</span><strong>?></strong></span>   
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><selector</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">xmlns:android</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"http://schemas.android.com/apk/res/android"</span> </span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:constantSize</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:dither</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  5. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:variablePadding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> <strong>></strong></span>   
  6.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><item</strong> </span>  
  7. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:drawable</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@[package:]drawable/drawable_resource"</span> </span>  
  8. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_pressed</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  9. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_focused</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  10. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_selected</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  11. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_active</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  12. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_checkable</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  13. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_checked</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  14. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_enabled</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  15. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_window_focused</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> <strong>/></strong></span>    
  16. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></selector<strong>></strong></strong></span>  

Elements:

必须。必须是根元素。可以包含一个或多个元素。

Attributes:

xmlns:android

String。必须。定义XML的命名空间,必须是

“http://schemas.android.com/apk/res/android”。

android:constantSize

Boolean。“true”表示随着状态变化,Drawable的大小保持不变(所有状态中最大的size);“false”表示大小会变化。默认是false。

android:dither

Boolean。“true”表示当Bitmap和屏幕的不是相同的像素设定时支持Bitmap抖动(例如,ARGB 8888的Bitmap和RGB 565的屏幕);“false”表示不支持。默认是“true”。

android:variablePadding

Boolean。“true”表示Drawable的Padding可以变化;“false”表示Padding保持相同(所有状态的最大Padding)。使能这一特征需要在状态变化时处理Layout,一般都不支持。默认值是false。

定义特定状态的Drawable,通过它的特性指定。必须是
的子元素。

Attributes:

android:drawable

Drawable资源。必须。指向一个Drawable资源。

android:state_pressed

Boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

android:state_focused

Boolean。“true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。

android:state_selected

Boolean。“true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。

android:state_checkable

Boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

android:state_checked

Boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

android:state_enabled

Boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

android:window_focused

Boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。

注意:记住一点,StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后(如下面的例子所示)。

Example:

XML文件保存在res/drawable/button.xml。

12345678
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><?xml</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">version</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"1.0"</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">encoding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"utf-8"</span><strong>?></strong></span>   
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><selector</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">xmlns:android</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"http://schemas.android.com/apk/res/android"</span><strong>></strong></span>   
  3.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><item</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_pressed</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> </span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">          <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:drawable</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/button_pressed"</span> <strong>/></strong></span> <em><!-- pressed --></em>   
  5.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><item</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:state_focused</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> </span>  
  6. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">          <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:drawable</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/button_focused"</span> <strong>/></strong></span> <em><!-- focused --></em>   
  7.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><item</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:drawable</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/button_normal"</span> <strong>/></strong></span> <em><!-- default --></em>   
  8. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></selector<strong>></strong></strong></span>  

Layout XML将这个Drawable应用到一个View上:

1234
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><ImageView</strong> </span>  
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_height</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span> </span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_width</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span> </span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:src</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/button"</span> <strong>/></strong></span>  

Color

定义在XML中的color,可以当作Drawable使用,来填充矩形区域(圆角可以有)。这种Drawable的行为很像是颜色填充。

注意:Color Drawable是一种简单的资源,可以使用name特性来引用其值(不再是XML文件的名)。因此,你可以在一个XML文件中的元素下添加多个Color Drawable。

File Location:

res/drawable/filename.xml

文件名随意。元素的name将会当作资源ID使用。

Complied Resource Datatype:

指向PaintDrawable资源的指针。

Resource Reference:

R.drawable.color_name(Java)

@[package:]drawable/color_name(XML)

Syntax:

12345
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><?xml</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">version</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"1.0"</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">encoding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"utf-8"</span><strong>?></strong></span>   
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><resources<strong>></strong></strong></span>   
  3.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><drawable</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">name</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"color_name"</span> </span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <strong>></strong></span>color<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></drawable<strong>></strong></strong></span>   
  5. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></resources<strong>></strong></strong></span>  

Elements:

必须。必须是根节点。

没有特性。

一个color Drawable。其值可以是任何有效的十六进制颜色值或者Color资源。Color值总是以“#”开头,后面紧跟Alpha-Red-Green-Blue信息,格式是:#RGB、#ARGB或者#AARRGGBB。

Attributes:

name

String。必须。Color的名字。这个名字将被当作资源ID使用。

Example:

XML文件保存在res/drawable/color.xml。

12345
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><?xml</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">version</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"1.0"</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">encoding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"utf-8"</span><strong>?></strong></span>   
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><resources<strong>></strong></strong></span>   
  3.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><drawable</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">name</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"solid_red"</span><strong>></strong></span>#f00<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></drawable<strong>></strong></strong></span>   
  4.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><drawable</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">name</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"solid_blue"</span><strong>></strong></span>#0000ff<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></drawable<strong>></strong></strong></span>   
  5. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></resources<strong>></strong></strong></span>  

Layout XML将会把这个Color Drawable应用到一个View上:

1234
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><TextView</strong> </span>  
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_width</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"fill_parent"</span> </span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_height</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span> </span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:background</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/solid_blue"</span> <strong>/></strong></span>  

代码中获取Color Drawable并应用到View上:

12345
[java] view plaincopyprint?
  1. Resources res <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span>  getResources<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>   
  2. Drawable redDrawable <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> res.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">getDrawable</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>R.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">drawable</span>.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">solid_red</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>   
  3.    
  4. TextView tv <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>TextView<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span> findViewByID<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>R.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">id</span>.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">text</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>   
  5. tv.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">setBackground</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>redDrawable<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>  

Shape

定义在XML中的几何形状。

File Location:

res/drawable/filename.xml

文件名将被当作资源ID使用。

Complied Resource Datatype:

指向ShapeDrawable的资源指针。

Resource Reference:

R.drawable.filename(Java)

@[package:]drawable/filename(XML)

Syntax:

1234567891011121314151617181920212223242526272829303132
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><?xml</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">version</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"1.0"</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">encoding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"utf-8"</span><strong>?></strong></span>   
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><shape</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">xmlns:android</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"http://schemas.android.com/apk/res/android"</span> </span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:shape</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"rectangle"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"oval"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"line"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"ring"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> <strong>></strong></span>   
  4.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><gradient</strong> </span>  
  5. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:angle</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  6. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:centerX</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  7. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:centerY</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  8. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:centerColor</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  9. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:endColor</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"color"</span> </span>  
  10. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:gradientRadius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  11. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:startColor</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"color"</span> </span>  
  12. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:type</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"linear"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"radial"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"sweep"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> </span>  
  13. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:usesLevel</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">[</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"true"</span> | <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"false"</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(102, 204, 102);">]</span> <strong>/></strong></span>   
  14.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><solid</strong> </span>  
  15. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:color</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"color"</span> <strong>/></strong></span>   
  16.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><stroke</strong> </span>  
  17. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:width</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  18. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:color</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"color"</span> </span>  
  19. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:dashWidth</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  20. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:dashGap</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> <strong>/></strong></span>   
  21.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><padding</strong> </span>  
  22. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:left</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  23. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:top</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  24. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:right</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  25. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:bottom</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> <strong>/></strong></span>   
  26.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><corners</strong> </span>  
  27. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:radius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  28. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:topLeftRadius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  29. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:topRightRadius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  30. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:bottomLeftRadius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> </span>  
  31. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:bottomRightRadius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"integer"</span> <strong>/></strong></span>   
  32. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></shape<strong>></strong></strong></span>  

Elements:

必须。必须是根元素。

Attributes:

android:shape

Keyword。定义Shape的类型。有效的值包括:

ValueDesciption"rectangle"矩形。默认形状。"oval"椭圆。"line"水平直线。需要元素定义线的宽度。"ring"环形。

接下来的特性只能在android:shape=”ring”时使用:

android:innerRadius

Dimension。内环的半径。

android:innerRadiusRatio

Float。以环的宽度比率来表示内环的半径。例如,如果android:innerRadiusRatio=”5”,内环半径等于环的宽度除以5。这个值可以被android:innerRadius覆盖。默认值是9。

android:thickness

Dimension。环的厚度。

android:thicknessRatio

Float。以环的宽度比率来表示环的厚度。例如,如果android:thicknessRatio=”2”,厚度就等于环的宽度除以2。这个值可以被android:thickness覆盖。默认值是3。

android:useLevel

Boolean。“true”表示可以当作LevelListDrawable使用。一般都为“false”。

为Shape指定渐变色。

Attributes:

android:angle

Integer。渐变色的角度值。0表示从左到右,90表示从下到上。必须是45的倍数,默认是0。

android:centerX

Float。渐变色中心的X相对位置(0-1.0)。当android:type=”linear”时无效。

android:centerY

Float。渐变色中心的Y相对位置(0-1.0)。当android:type=”linear”时无效。

android:centerColor

Color。可选的颜色,出现在start和end颜色之间。

android:endColor

Color。end颜色。

android:gradientRadius

Float。渐变色的半径。当android:type=”radial”时有效。

android:startColor

Color。start颜色。

android:type

Keyword。渐变色的样式。有效值为:

ValueDescription"linear"线性渐变,默认值。"radial"环形渐变。start颜色是处于中间的颜色。"sweep"sweep渐变

android:useLevel

Boolean。“true”表示可以当作LevelListDrawable使用。

填充shape的单一色。

Attributes:

android:color

Color。这个颜色会应用到shape上。

shape的线形。

Attributes:

android:width

Dimension。线的厚度。

android:color

Color。线的颜色。

android:dashGap

Dimension。间断线间的距离。仅在android:dashWidth设定时有效。

android:dashWidth

Dimension。间断线的大小。仅在android:dashGap设定时有效。

内部View元素的边距。

Attributes:

android:left

Dimension。左内边距。

android:top

Dimension。上内边距。

android:right

Dimension。右内边距。

android:bottom

Dimension。下内边距。

为shape创建圆角。当shape是一个矩形时有效。

Attributes:

android:radius

Dimension。圆角的半径。会被下面的特性覆盖。

android:topLeftRadius

Dimension。左上圆角半径。

android:topRightRadius

Dimension。右上圆角半径。

android:bottomLeftRadius

Dimension。左下圆角半径。

android:bottomRightRadius

Dimension。右下圆角半径。

Examples:

XML文件保存在res/drawable/gradient_box.xml

12345678910111213
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><?xml</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">version</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"1.0"</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">encoding</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"utf-8"</span><strong>?></strong></span>   
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><shape</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">xmlns:android</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"http://schemas.android.com/apk/res/android"</span> </span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:shape</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"rectangle"</span><strong>></strong></span>   
  4.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><gradient</strong>  </span>  
  5. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:startColor</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"#FFFF0000"</span>  </span>  
  6. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:endColor</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"#80FF00FF"</span> </span>  
  7. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:angle</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"45"</span><strong>/></strong></span>   
  8.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><padding</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:left</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"7dp"</span>  </span>  
  9. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:top</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"7dp"</span> </span>  
  10. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:right</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"7dp"</span>  </span>  
  11. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">        <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:bottom</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"7dp"</span> <strong>/></strong></span>   
  12.     <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><corners</strong> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:radius</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"8dp"</span> <strong>/></strong></span>   
  13. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong></shape<strong>></strong></strong></span>  

Layout XML将被当作ShapeDrawable应用到一个View上:

1234
[xml] view plaincopyprint?
  1. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);"><strong><TextView</strong> </span>  
  2. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:background</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"@drawable/gradient_box"</span> </span>  
  3. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_height</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span> </span>  
  4. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">    <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 0, 102);">android:layout_width</span>=<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(255, 0, 0);">"wrap_content"</span> <strong>/></strong></span>  

代码中也可以获得ShapeDrawable,然后应用到View上:

12345
[java] view plaincopyprint?
  1. Resources res <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> getResources<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>   
  2. Drawable shape <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> res. <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">getDrawable</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>R.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">drawable</span>.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">gradient_box</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>   
  3.    
  4. TextView tv <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">=</span> <span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>TextView<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span>findViewByID<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>R.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">id</span>.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">textview</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>   
  5. tv.<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 102, 51);">setBackground</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">(</span>shape<span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(0, 153, 0);">)</span><span style="margin: 0px; padding: 0px; border-style: none; border-width: 0px; background-color: transparent; color: rgb(51, 153, 51);">;</span>  


android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:

 

1、Drawable → Bitmap

Java代码  收藏代码
  1. public static Bitmap drawableToBitmap(Drawable drawable) {  
  2.           
  3.         Bitmap bitmap = Bitmap  
  4.                         .createBitmap(  
  5.                                         drawable.getIntrinsicWidth(),  
  6.                                         drawable.getIntrinsicHeight(),  
  7.                                         drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  8.                                                         : Bitmap.Config.RGB_565);  
  9.         Canvas canvas = new Canvas(bitmap);  
  10.         //canvas.setBitmap(bitmap);  
  11.         drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
  12.         drawable.draw(canvas);  
  13.         return bitmap;  
  14. }  

 

2、从资源中获取Bitmap

 

Java代码  收藏代码
  1. Resources res=getResources();  
  2.   
  3. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);  

 

3、Bitmap → byte[]

 

Java代码  收藏代码
  1. private byte[] Bitmap2Bytes(Bitmap bm){  
  2.     ByteArrayOutputStream baos = new ByteArrayOutputStream();    
  3.     bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    
  4.     return baos.toByteArray();  
  5.    }  

 
4、 byte[] → Bitmap

Java代码  收藏代码
  1. private Bitmap Bytes2Bimap(byte[] b){  
  2.             if(b.length!=0){  
  3.                 return BitmapFactory.decodeByteArray(b, 0, b.length);  
  4.             }  
  5.             else {  
  6.                 return null;  
  7.             }  
  8.       }  




本文转自:http://blog.csdn.net/xinwang24/article/details/7016516