Android学习之Drawable(一)

Drawable有很多种,它们表示一种图像概念,但它们不全是图片。Drawable是什么呢?下面是Google Android API中的定义:

A Drawable is a general abstraction for “something that can be drawn.” Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user. 
它大致的意思是:Drawable一种图像概念。通常,你会把它当成一种能够在屏幕上显示的资源类型来处理,Drawable类提供了一个通用的API来处理不同形式的图像资源。与View不同,Drawable不能接受事件,也不能和用户交互。

下面介绍几种Drawable

BitmapDrawable

BitmapDrawable几乎是最简单的了,它表示一张图片。通常在开发中我们就直接引用图片即可,比如: R.drawable.image(drawable目录下有一个image.jpg或者image.png的图片资源),但是我们也可以用xml来描述Drawable。xml文件如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
3         android:src="@mipmap/ic_launcher"
4         android:antialias="true|false"
5         android:dither="true|false"
6         android:filter="true|false"         android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"
7         android:mipMap="true|false"
8         android:tileMode="disabled|clamp|repeat|mirror"/>

ShapeDrawable

这是一种很常见的Drawable,通常是通过编写xml文件来创建的,因此有些复杂。ShapeDrawable通常是通过颜色来构建图形的,既可以是纯色,也可以具有渐变效果。使用大致如下所示:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:shape="rectangle|oval|line|ring">
 4     <corners
 5         android:bottomLeftRadius="integer"
 6         android:bottomRightRadius="integer"
 7         android:radius="integer"
 8         android:topLeftRadius="integer"
 9         android:topRightRadius="integer" />
10     <gradient
11         android:angle="integer"
12         android:centerColor="integer"
13         android:centerX="integer"
14         android:centerY="integer"
15         android:endColor="color"
16         android:gradientRadius="integer"
17         android:startColor="color"
18         android:type="linear|radial|sweep"
19         android:useLevel="true|false" />
20     <padding
21         android:bottom="integer"
22         android:left="integer"
23         android:right="integer"
24         android:top="integer" />
25     <size
26         android:width="integer"
27         android:height="integer" />
28     <solid android:color="color" />
29     <stroke
30         android:width="integer"
31         android:color="color"
32         android:dashGap="integer"
33         android:dashWidth="integer" />
34
35 </shape>
    • android:shape 
        表示图形形状:rectangle(矩形)、oval(椭圆)、line(横线)、ring(圆环),默认矩形,line和ring必须有 < stroke>标签来指定宽度和颜色,否则达不到预期效果。
    • < gradient>  渐变效果,与< solid>标签互斥
    • < solid>  纯色填充 通过android:color即可指定shape的颜色

LayerDrawable

它是一种层次化的Drawable,在< layer-list>< /layer-list>结点下有多个< item>< /item>其中后面的< item>< /item>叠加在前面的< item>< /item>上面,想爱你面是一耳光文本输入框的例子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 3     <item>
 4         <shape android:shape="rectangle">
 5             <solid android:color="#0ac39e" />
 6         </shape>
 7     </item>
 8     <item android:bottom="6dp">
 9         <shape android:shape="rectangle">
10             <solid android:color="#ffffff" />
11         </shape>
12     </item>
13     <item
14         android:bottom="1dp"
15         android:left="1dp"
16         android:right="1dp">
17         <shape android:shape="rectangle">
18             <solid android:color="#ffffff" />
19         </shape>
20     </item>
21
22 </layer-list>

StateListDrawable

StateListDrawable对应< selector>标签,这个大家应该比较熟悉。我们经常会给Button设置一个selector。StateListDrawable表示Drawable的集合,集合中的每个Drawable都对应着View的一种状态,系统会根据View的状态来给View设定相应的Drawable,下面是一个selector的创建样例:

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3     <item android:drawable="@android:color/black"/>    <!-- 表示默认状态-->
4     <item android:state_focused="true"
5           android:drawable="@android:color/holo_orange_dark"/><!-- 表示获取焦点状态-->
6     <item android:state_pressed="true"
7           android:drawable="@android:color/holo_red_dark"/><!-- 表示被点击状态-->
8 </selector>
时间: 2024-08-03 01:18:09

Android学习之Drawable(一)的相关文章

Android学习系列(17)--App列表之圆角ListView(续)

http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html 本来这篇文章想并到上篇Android学习系列(16)--App列表之圆角ListView中的,但是若是如此就让大家错过一篇新的好的文章,着实可惜.上篇中我们使用shape,corners,gradient实现了一个渐变的圆角效果,但是在完文之后的实践中,我发现有时效果不甚满意,选中和放手的事件监听没有去正确的判断,然后渐变效果也比较单一,性能也觉得不是很快

Pro Android学习笔记(三三):Menu(4):Alternative菜单

什么是Alternative menu(替代菜单) 举个例子,Activity显示一个文本文件.如果用户想对文本文件进行编辑,Activity不提供编辑能力,但可由其他activity或者其他应用提供.我们将相关信息存储在一个intent中,例如该文本的Uri.这个intent可以匹配系统的多个应用,替代菜单将这些应用一一列出,菜单项的title就是该可被调用的activity的名字,图标也为该可被调用的activity的图表. 小例子说明 我们通过一个小例子进行学习,简单地打开一个URL:we

Android学习笔记(二二): 多页显示-Tag的使用

在手机屏幕中,Tab也是比较常用的,通常和List结合,例如我们手机的通信录.下面是Tag的结构. TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout.TabWidget就是每个tab的标签,FrameLayout则是tab内容. 如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost TabWidget必须设置android:id为@android:id/tabs F

android学习---Gallery画廊视图

Gallery与Spinner有共同父类:AbsPinner,说明Gallery与Spinner都是一个列表框.它们之间的区别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是一个水平的列表选择框.Spinner的作用是供用户选择,而Gallery则允许用户通过拖动查看上一个,下一个. Gallery用法与Spinner的用法形似,只要为它提供一个内容Adapter就可以了.Adapter的getView方法返回View作为Gallery列表的列表项.如果程序需要监控Gal

Android学习Scroller(五)——详解Scroller调用过程以及View的重绘

MainActivity如下: package cc.ww; import android.os.Bundle; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.app.Activity;

android学习常用资料博客目录

android 技术总结 1.android 系统签名 介绍如何使用源码中的  (testkey:普通APK,默认情况下使用,platform:该APK完成一些系统的核心功能,经过对系统中存在的文件夹的访问测试,这种方式编译出来的APK所在进程的UID为system,shared:该APK需要和home/contacts进程共享数据,media:该APK是media/download系统中的一环.)4种密钥公钥进行签名. 2.MVC ListView 介绍什么是MVC以及MVC在SDK中的使用,

android学习笔记——利用BaseAdapter生成40个列表项

RT: main.xml ? 1 2 3 4 5 6 7 8 9 10 11 12 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"        

[Android学习系列13]关于Gridview的一些事

用mvc的思想去理解 数据源,adapter,xxxView的关系,写代码就轻松多了 先写一个简单的gridview 第一版:只有图片 xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent&q

Android:学习笔记(一)

在android学习中遇到的一些问题,记录一下. 1.解决edittext感叹号的问题. This text field does not specify an inputType or a hint 加入以下代码就可以解决: android:hint="@null" 2.解决android4.0后edittext边框不全显示的问题 android:background="@android:drawable/edit_text" 3.感叹号问题 android:pa