Android Drawable
2015-12-09 10:43
435 查看
BitmapDrawable:
这几乎是最简单的Drawable了,他表示的就是一张图片。在实际开发中,我们可以直接引用原始的图片即可,但是也可以通过xml的方式来描述他,通过cml来描述的BitmapDrawable可以设置更多的效果:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable/drawable_resource"
android:antialias=["true"|"false"]
android:dither=["true"|"false"]
android:filter=["true"|"false"]
android:gravity=["top"|"bottom"|"left"|"right"|"center_vertical"|"fill_vertical"|"center_horizontal"|"fill_horizontal"|"center"|"fill"|"clip_vertical"|"clip_horizontal"]
android:mipMap=["true"|"false"]
android:titleMode=["disable"|"clamp"|"repeat"|"mirror"]
下面是它的各个属性的含义:
android:src
这个很简单,就是图片的资源id
android:antialias
是否开启图片的抗锯齿功能。开启后会让图片变得平滑,同时会在一定程度上降低图片的清晰度,但是这个降低的幅度较低以至于可以忽略,因此抗锯齿选项应该开启。
android:dither
是否开启抖动效果。当图片的像素配置和手机屏幕配置不一致时开启这个选项可以让高质量的图片在低质量的屏幕上还能保持良好的显示效果
android:filter
是否开启过滤效果。当图片尺寸被拉伸或者压缩时,开启过滤效果可以保持较好的显示效果。
android:gravity
当图片小于容器的尺寸时,设置此项可以对图片进行定位。这个属性选项比较多,不同的选项可以通过“|”来组合使用
android:mipMap
一种图像处理技术,不常用
android:titleMode
平铺模式。这个选项有如下几个值:["disable"]["clamp"]["repeat"]["mirror"]disable表示关闭平铺模式,此值为默认值。其它三个都表示开启平铺模式,但效果有差别,具体在使用中感受。
ShapeDrawable
ShapDrawable是一种很常见的Drawable,可以理解为通过颜色来构造的图形,他既可以是纯色的图形,也可以是具有渐变效果的图形。语法规则如下:
属性:
下面的属性只有当
属性:
注意:每个圆角半径值都必须大于1,否侧就没有圆角。
下面的话不明白,我直接设置圆角为0就可以不圆了,其余的设置有圆角,一样的可行。不知道它为什么要这么讲。
(If you want specific cornersto not be rounded, a work-around is to use
you reallywant, providing zero ("0dp") where you don't want rounded corners.)
属性:
属性:
属性:
注意:默认情况下,这个shape会缩放到与他所在容器大小成正比。当你在一个ImageView中使用这个shape,你可以使用
属性:
属性:
这几乎是最简单的Drawable了,他表示的就是一张图片。在实际开发中,我们可以直接引用原始的图片即可,但是也可以通过xml的方式来描述他,通过cml来描述的BitmapDrawable可以设置更多的效果:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable/drawable_resource"
android:antialias=["true"|"false"]
android:dither=["true"|"false"]
android:filter=["true"|"false"]
android:gravity=["top"|"bottom"|"left"|"right"|"center_vertical"|"fill_vertical"|"center_horizontal"|"fill_horizontal"|"center"|"fill"|"clip_vertical"|"clip_horizontal"]
android:mipMap=["true"|"false"]
android:titleMode=["disable"|"clamp"|"repeat"|"mirror"]
下面是它的各个属性的含义:
android:src
这个很简单,就是图片的资源id
android:antialias
是否开启图片的抗锯齿功能。开启后会让图片变得平滑,同时会在一定程度上降低图片的清晰度,但是这个降低的幅度较低以至于可以忽略,因此抗锯齿选项应该开启。
android:dither
是否开启抖动效果。当图片的像素配置和手机屏幕配置不一致时开启这个选项可以让高质量的图片在低质量的屏幕上还能保持良好的显示效果
android:filter
是否开启过滤效果。当图片尺寸被拉伸或者压缩时,开启过滤效果可以保持较好的显示效果。
android:gravity
当图片小于容器的尺寸时,设置此项可以对图片进行定位。这个属性选项比较多,不同的选项可以通过“|”来组合使用
android:mipMap
一种图像处理技术,不常用
android:titleMode
平铺模式。这个选项有如下几个值:["disable"]["clamp"]["repeat"]["mirror"]disable表示关闭平铺模式,此值为默认值。其它三个都表示开启平铺模式,但效果有差别,具体在使用中感受。
ShapeDrawable
ShapDrawable是一种很常见的Drawable,可以理解为通过颜色来构造的图形,他既可以是纯色的图形,也可以是具有渐变效果的图形。语法规则如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <gradient android:angle="integer" android:centerX="integer" android:centerY="integer" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:usesLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>
<shape>定义这是一个GradientDrawable,必须作为根元素。
属性:
xmlns:androidString类型。必须的,定义xml文件的命名空间,必须是"http://schemas.a
ndroid.com/apk/res/android".
android:shape关键字。定义shape的值,必须是下面的之一:
值 | 描述 |
---|---|
"rectangle" | 矩阵,这也是默认的shape |
"oval" | 椭圆 |
"line" | 一条水平的直线。这种shape必须使用 <stroke>元素来定义这条线的宽度 |
"ring" | 圆环 |
android:shape="ring"才使用:
android:innerRadius尺寸。 内环的半径。一个尺寸值(dip等等)或者一个尺寸资源。
android:innerRadiusRatioFloat类型。这个值表示内部环的比例,例如,如果android:innerRadiusRatio = " 5 ",那么内部的半径等于环的宽度除以5。这个值会被android:innerRadius重写。 默认值是9。
android:thickness尺寸。环的厚度,是一个尺寸值或尺寸的资源。
android:thicknessRatioFloat类型。厚度的比例。例如,如果android:thicknessRatio= " 2 ",然后厚度等于环的宽度除以2。这个值是被android:innerRadius重写, 默认值是3。
android:useLevelBoolean类型。如果用在
LevelListDrawable里,那么就是true。如果通常不出现则为false。
<corners>为Shape创建一个圆角,只有shape是rectangle时候才使用。
属性:
android:radiusDimension。圆角的半径。会被下面每个特定的圆角属性重写。
android:topLeftRadiusDimension。top-left 圆角的半径。
android:topRightRadiusDimension。top-right 圆角的半径。
android:bottomLeftRadiusDimension。 bottom-left圆角的半径。
android:bottomRightRadiusDimension。bottom-right圆角的半径。
注意:每个圆角半径值都必须大于1,否侧就没有圆角。
下面的话不明白,我直接设置圆角为0就可以不圆了,其余的设置有圆角,一样的可行。不知道它为什么要这么讲。
(If you want specific cornersto not be rounded, a work-around is to use
android:radiusto set a default cornerradius greater than 1, but then override each and every corner with the values
you reallywant, providing zero ("0dp") where you don't want rounded corners.)
<gradient>指定这个shape的渐变颜色。
属性:
android:angleInteger。渐变的角度。 0 代表从 left 到 right。90 代表bottom到 top。必须是45的倍数,默认为0
android:centerXFloat。渐变中心的相对X坐标,在0到1.0之间。
android:centerYFloat。渐变中心的相对Y坐标,在0到1.0之间。
android:centerColorColor。可选的颜色值。基于startColor和endColor之间。
android:endColorColor。 结束的颜色。
android:gradientRadiusFloat 。渐变的半径。只有在
android:type="radial"才使用
android:startColorColor。开始的颜色值。
android:typeKeyword。渐变的模式,下面值之一:
值 | 描述 |
---|---|
"linear" | 线形渐变。这也是默认的模式 |
"radial" | 辐射渐变。startColor即辐射中心的颜色 |
"sweep" | 扫描线渐变。 |
android:useLevelBoolean。如果在LevelListDrawable中使用,则为true
<padding>
内容与视图边界的距离
属性:
android:leftDimension。左边填充距离.
android:topDimension。顶部填充距离.
android:rightDimension。右边填充距离.
android:bottomDimension。底部填充距离.
<size>这个shape的大小。
属性:
android:heightDimension。这个shape的高度。
android:widthDimension。这个shape的宽度。
注意:默认情况下,这个shape会缩放到与他所在容器大小成正比。当你在一个ImageView中使用这个shape,你可以使用
android:scaleType
="center"来限制这种缩放。
<solid>填充这个shape的纯色
属性:
android:colorColor。颜色值,十六进制数,或者一个Color资源
<stroke>这个shape使用的笔画,当android:shape="line"的时候,必须设置改元素。
属性:
android:widthDimension。笔画的粗细。
android:colorColor。笔画的颜色
android:dashGapDimension。每画一条线就间隔多少。只有当
android:dashWidth也设置了才有效。
android:dashWidthDimension。每画一条线的长度。只有当
android:dashGap也设置了才有效。
相关文章推荐
- 分享:android之hardwareAccelerated你不知道的一些问题
- Android GridView属性集合
- Android系统五大布局详解Layout
- android dialog关闭按钮 在半透明区域(最简单实现)
- Android Studio基础之主界面及使用(三)-学习篇
- android camera中的预览图片变形的解决办法
- Android:初识异步任务AsyncTask
- Android Camera 使用详解
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- android开发之流量监控
- android 流量监控实现想法
- Android递归方式删除某文件夹下的所有文件
- Android调用系统安装/卸载apk过程
- Android 编译系统 (二)
- Android的四种启动模式
- android java.io.IOException: open failed: EBUSY (Device or resource busy)
- Handler常见问题
- android 创建快捷方式
- Android studio的使用 -- 代码混淆与打包
- Android自定义相机