详解Android中Drawable方法
1. BitmapDrawable相关方法:
新建在drawable目录下面,示例如下:
<bitmap xmlns:android="http
本文为大家分享了Android中Drawable方法的详细使用方法,供大家参考,具体内容如下
1. BitmapDrawable相关方法:
新建在drawable目录下面,示例如下:
1 2 3 4 5 6 7 8 | <bitmap xmlns:android= "http://schemas.android.com/apk/res/android" android:antialias= "true" android:dither= "true" android:filter= "true" android:gravity= "center" android:mipMap= "false" android:src= "@drawable/ic_launcher" android:tileMode= "disabled" /> |
具体每个属性的解释如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?xml version= "1.0" encoding= "utf-8" ?> <bitmap xmlns:android= "http://schemas.android.com/apk/res/android" <!-- 图片抗锯齿效果,开启后会让图片变平滑,也可以在一定程度上降低图片分辨率(幅度可忽略) --> android:antialias= "true" <!-- 是否开启抖动效果,开启这个选项可以染高质量的图片在低质量的屏幕下还能较好的显示 --> android:dither= "true" <!-- 是否开启过滤,开启可以保持较好的显示效果 --> android:filter= "true" <!-- 图片重力控制,可以控制图片位置,也可以通过“|”来组合使用: top 图片放到顶部,不改变图片大小 bottom 图片放到底部,不改变大小 left 图片放到左边,不改变大小 right 图片放到右边,不改变大小 center_vertical 使图片竖直居中,不改变大小 fill_vertical 图片竖直方向充满整个容器 center_horizontal 图片水平方向居中,不改变图片大小 fill_horizontal 图片水平方向充满 center 图片中间居中 fill 图片充满容器,这个是默认值 clip_vertical 附加选项,表示竖直方向的裁剪,较少使用 clip_horizontal 附加选项,便是水平方向的裁剪,较少使用 --> android:gravity= "center" <!-- 纹理映射,一遍为false --> android:mipMap= "false" <!-- 图片的数据源 --> android:src= "@drawable/ic_launcher" <!-- 有如下几个值,对应的方式如下: disabled 默认模式,关闭平铺 repeat 加单的水平和竖直方向平铺 mirror 一种在水平和竖直方向的镜面投影效果 clamp 四周像素扩散到周围区域 --> android:tileMode= "disabled" /> |
上述的排版方式只是为了方便查看,不能直接复制到代码中使用
2. ShapeDrawable,纯色、渐变色的drawable
基本示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?xml version= "1.0" encoding= "utf-8" ?> <shape xmlns:android= "http://schemas.android.com/apk/res/android" android:shape= "rectangle" > <corners android:bottomLeftRadius= "1px" android:bottomRightRadius= "1px" android:radius= "2px" android:topLeftRadius= "1px" android:topRightRadius= "1px" /> <gradient android:angle= "5" android:centerColor= "#ff789434" android:centerX= "x" android:centerY= "y" android:endColor= "#34234324" android:gradientRadius= "55dp" android:startColor= "#ff783247" android:type= "linear" android:useLevel= "true" /> <padding android:bottom= "5dp" android:left= "5dp" android:right= "5dp" android:top= "5dp" /> <size android:width= "70dp" android:height= "70dp" /> <solid android:color= "#fffff333" /> <stroke android:width= "30dp" android:color= "#33333333" android:dashGap= "4dp" android:dashWidth= "4dp" /> </shape> |
各个标签详细说明:
<shape>表示图像形状
表示图形形状,有四个选项rectangle(矩形)、oval(椭圆)、line(横线)、ring(圆环),默认是矩形。在使用line和ring时候需要通过stroke属性来指定线的宽度和颜色,否则显示效果将会异常。
当设置ring时,有五个特殊的属性,对应如下:
android:innerRadius——圆环内半径,和innerRadiusRatio同时存在时,以innerRadius为准。
android:thickness——圆环的厚度,及外半径减去内半径
android:innerRAdiusRatio——内半径占整个drawable的宽度比例,默认为9
android:thicknessRadio——厚度咱整个Drawable的比例,默认为3,计算方式为厚度=宽度/n
android:useLevel——一般都应该使用false,除非被当做LevelListDrawable来使用时候
<corners>(四个角角度,适用于矩形)
表示shape的四个角的角度。它只试用与矩形,角度值的是圆角的程度,用px来表示。其中radius用来为四个角同意设置角度,优先级比较低。topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius分别表示左上、右上、左下、右下角的角度。
<gradient>(颜色渐变)
表示渐变填充,它与标签互斥,包含的几个属性值意义如下:
android:angle——渐变角度,默认为零,值必须为45的倍数。
android:centerX——渐变中心点横坐标
android:centerY——渐变中心点纵坐标
android:startColor——渐变起始颜色
android:centerColor——渐变的中间色
android:endColor——渐变的结束色
android:gradientRadius——渐变半径
android:useLevel——一般为false,仅当android:type=”radial”
android:type——渐变的类型,有个linear(线性渐变)、radial(径向渐变)、sweep(扫描线渐变)三种,其中默认为线性渐变
<solid>(纯色填充)
通过android:color既可指定shape中填充的颜色
<stroke>(描边属性)
包含以下常用标签:
android:width——描边的宽度,越大则shape的边缘就会看起来越粗
android:color——描边的颜色
android:dashWidth——组成虚线的线段的宽度
android:dashGap——组成虚线的线段之间的间隔,间隔越大空隙越大
<padding>内边距
可以理解为内边距,使用起来和直接在view中加边距类似包含四个属性:android:top、android:bottom、android:left和android:right,分别表示上下左右内边距。
<size>
有两个属性android:width和android:height,表示该view的宽高,类似图片本身的宽高,在view中会视情况拉伸。
3. LayerDrawable层次化的Drawable,类似图层
多个drawable的集合,类似图层概念,通过将不同的drawable放置在不同的层上面从而达到一种叠加后的效果,它的语法规则如下:
1 2 3 4 5 6 7 8 9 10 11 | <?xml version= "1.0" encoding= "utf-8" ?> <layer-list xmlns:android= "http://schemas.android.com/apk/res/android" > <item android:id= "@+id/item1" android:bottom= "1dp" android:drawable= "@drawable/ic_launcher" android:left= "1dp" android:right= "1dp" android:top= "1dp" > </item> </layer-list> |
一个layer-list可以包含多个item,每个item表示一个drawable。上下左右是个属性表示Drawable相对于view的上下左右偏移量,单位为像素。可以通过drawable来直接引用一个drawable资源。也可以直接在item标签下自定义drawable。
默认情况下,layer-list中所有的drawable都会被缩放到view的大小。layer-list有层次概念,下面的drawable会覆盖上面的drawable。通过合理分层,可以实现一些特殊的叠加效果。
4. StateListDrawable(选择器)
选择器标签,对应的标签是<selector>,也是一个drawable的集合,常用来根据当前状态改变颜色。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version= "1.0" encoding= "utf-8" ?> <selector xmlns:android= "http://schemas.android.com/apk/res/android" <!-- 是否会随着状态的改变而改变,默认为false,表示会随着改变 --> android:constantSize= "true" <!-- 是否开启抖动,开启可以在低分辨率手机上获得较好的显示,建议为true --> android:dither= "true" <!-- 是否随着状态而改变内边距,建议为true --> android:variablePadding= "true" > <item <!-- 对应的drawable --> android:drawable= "@drawable/ic_launcher" <!-- 按下状态,比如button按下未松开 --> android:state_pressed= "true" <!-- 表示View已经获得焦点 --> android:state_focused= "true" <!-- 光标是否悬停,4.0以后特性 --> android:state_hovered= "true" <!-- 表示用户选择了view --> android:state_selected= "true" <!-- 表示组件是否能被选中,如RadioButton --> android:state_checkable= "true" <!-- 表示用户选中了view,一般用于选择按钮 --> android:state_checked= "true" <!-- 表示view当前处于可用状态 --> android:state_enabled= "true" <!-- 表示view处于激活状态 --> android:state_activated= "true" <!-- 应用程序是否在前台 --> android:state_window_focused= "true" > </item> </selector> |
注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。所以默认的item应该放到最后一条,否则会导致下边的选项没有效果。
5. LevelListDrawable(不同等级切换不同drawable)
LevelListDrawable对应于<level-list>标签,用来表示一个drawable集合。集合中每一个drawable都一个等级,根据不同的等级,LevelListDrawable会切换为对应的Drawable,具体详情如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version= "1.0" encoding= "utf-8" ?> <level-list xmlns:android= "http://schemas.android.com/apk/res/android" > <item <!-- 对应的drawable --> android:drawable = "@drawable/ic_lunch" <!-- 最大等级 --> android:maxLevel = "2" <!-- 最小等级 --> android:minLevel = "1" > <!-- 在最大和最小等级之间的等级会对应此item中的Drawable,当它作为view的背景时候,可以通过 Drawable的setLevel方法来设置不同的等级,从而切换具体的drawable。还可以通过ImageView的 setImageLevel方法来切换,等级范围是0到10000,0是默认值 --> </item> </level-list> |
6. TransitionDrawable(可实现淡入淡出)
对应的是<transition>标签,用于实现两个drawable之间淡入淡出的效果,具体使用方法如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version= "1.0" encoding= "utf-8" ?> <transition xmlns:android= "http://schemas.android.com/apk/res/android" > <item <!-- 对应的drawable --> android:drawable = "@drawable/ic_lunch" <!-- 对应的id --> android:id = "@+id/item1" <!-- drawable四周的偏移量 --> android:top = "1dp" android:right = "1dp" android:bottom = "1dp" android:left = "1dp" > </item> </transition> |
使用方式:
定义渐变
1 2 3 4 5 6 | <?xml version= "1.0" encoding= "utf-8" ?> <transition xmlns:android= "http://schemas.android.com/apk/res/android" > <item android:drawable= "@drawable/drawable1" /> <item android:drawable= "@drawable/drawable" /> </transition> |
将渐变添加到背景
1 2 3 4 5 6 | <TextView android:id = "@+id/button" andoird:layout_height = "wrap_content" android:layout_width = "wrap_content" android:background = "@drawable/transition_drawable" /> |
在代码中控制
1 2 3 | TextView textView = (TextView)findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable)textView.getBackground(); drawable.startTransition(1000); |
7. InsetDrawable(内嵌一个Drawable)
可以将Drawable内嵌到自己内部,并在四周留出空隙,一般用在希望自己背景比自己实际区域小的view中。
1 2 3 4 5 6 7 8 9 10 11 | <?xml version= "1.0" encoding= "utf-8" ?> <inset xmlns:android= "http://schemas.android.com/apk/res/android" <!-- 对应的Drawable,也可以直接写在内部 --> android:drawable= "@drawable/ic_launcher" <!-- Drawable距离四周的边距 --> android:insetBottom= "2dp" android:insetLeft= "2dp" android:insetRight= "2dp" android:insetTop= "2dp" > <!-- 内部用来放置对应的Drawable,也可以直接在上边引用 --> </inset> |
8. ScaleDrawable(缩放类,可以缩小)
对应的代码如下:
1 2 3 4 5 6 7 8 9 10 | <?xml version= "1.0" encoding= "utf-8" ?> <scale xmlns:android= "http://schemas.android.com/apk/res/android" <!-- 对应的Drawable --> android:drawable= "@drawable/ic_launcher" <!-- 显示的位置 --> android:scaleGravity= "center" <!-- 对应的缩放比例 --> android:scaleHeight= "50%" android:scaleWidth= "50%" > </scale> |
在使用时时候必须设置Drawable的级别,否则无法使用,设置级别的代码如下:
1 2 3 | View testScale = findViewById(R.id.test_scale); scaleDrawable testScaleDrawable = (ScaleDrawable)testScale.getBackground(); testScaleDrawable.setLevel(1); //范围是1~10000,只要不为零就可以 |
9. ClipDrawable(根据不同设置裁剪Drawable)
可以根据当前等级来裁剪另一个Drawable,具体的代码如下:
1 2 3 4 5 6 7 8 | <?xml version= "1.0" encoding= "utf-8" ?> <clip xmlns:android= "http://schemas.android.com/apk/res/android" <!-- 裁剪方向,水平或竖直 --> android:clipOrientation= "horizontal" android:drawable= "@+id/icon" <!-- 和clipOrientation配合使用,详情见下面说明--> android:gravity= "top" > </clip> |
top——放置到顶部,如果为竖直裁剪,那么从底部开始裁剪
bottom——放到底部,如果为竖直裁剪,那么从顶部开始裁剪
left——放到左边,如果为水平裁剪,那么从右开始裁剪(此为默认)
right——放到右边,若为水平裁剪,那么从左边开始裁剪
center_vertical——竖直居中,竖直裁剪时,上下同时裁剪
fill_vertical——竖直方向填充,如果为竖直裁剪,仅当clipDrawable等级为0时(此时不可见,为完全裁剪),才有裁剪行为
(水平方向也具有和竖直类似的属性,对应为center_horizontal和fill_horizontal,用法与上面两个类似)
center——中部居中,竖直裁剪时上下同时减,水平裁剪时左右同时减
fill——充满整个drawable,当clipDrawable等级为零时有效
clip_vertical——附加选项,表竖直方向裁剪
clip_horizontal——附加选项,表竖直方向裁剪
使用时候需要在代码中设置等级具体代码如下
1 2 3 4 | ImageViwe testClip = (ImageView)findViewById(R.id.test_clip); ClipDrawable testClipDrawable = (ClipDrawable)testClip.getDrawable(); //等级为从0到10000,等级0表示完全裁剪,等级10000表示不裁剪 testClipDrawable.setLevel(5000); |
以上就是本文的全部内容,希望对大家的学习有所帮助。