[大白装逼]Android好多好多的Drawable

前言

Android中提供了很多的Drawable,一般我们都是用来当做背景的,其实他还有很多其他的用途….

有什么Drawable?

bitmapDrawable
ShapeDrawable
LayerDrawable
StateListDrawable
LevelListDrawable
TransitionDrawable
InsetDrawable
ScaleDrawable
ClipDrawable

注意

在drawable中,每个标签<…>显示的内容会按顺序绘制,所以放在后面的标签会覆盖前面的绘制

bitmapDrawable

显示图片,并对图片的显示进行处理,其中也可以对点9的图片进行处理,还有个NinePatchDrawable是用于显示点9图片的

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/icon"//设置显示的图片为icon
android:antialias="true"//抗锯齿功能,最好开启让图片更加平滑,虽然会模糊一点点
android:dither="true"//抖动效果,在相片像素和手机像素不一致或手机不支持其色彩模式时(有些低端机不支持ARGB8888格式,现在的应该都兼容,但是都不好说)会保持较好的显示效果,应该开启
android:filter="true"//过滤效果,当图片被拉伸或压缩时,保持良好的显示效果,应该开启
android:gravity="center"//当图片大小小于容器大小时,其显示的位置
android:tileMode="disabled"//平铺模式,默认关闭(disabled),开启时会覆盖gravity的设置,会议tileMode设置的平铺方式来填充容器,repeat(简单平铺,会使用多个本图片水平和竖直方向进行填充),mirror(水平和竖直方向上的镜面投影效果),clamp(边缘像素会向四周扩散)
/>
<nine-path
.......//与bitmapDrawable一样
/>

ShapeDrawable

通过颜色来构造图形,可为纯色和渐变色

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"//图形的形状,默认为rectangle(矩形),还有oval(椭圆),line(横线)和ring(圆环).其中line和ring要使用<stroke>设置其宽度和颜色,否则显示会出问题
(ring有特殊的属性,其他的都没有
android:innerRadius="圆环内半径,与innerRadiusRatio同时存在时,以innerRadius为准"
android:innerRadiusRatio="内半径占整个Drawable宽度的比例,默认为9,内半径=宽度/9"
android:thickness="圆环厚度,与thicknessRatio同时存在时,以thickness为准"
android:thicknessRatio="圆环厚度占整个Drawable宽度的比例,默认为3,厚度=宽度/3"
android:useLevel="false"//一般为False,在将其用于LevelListDrawable时才使用true
)
>
<corners//四个角的角度(圆角度),仅适用于矩形
android:radius="2dp"//设置4个角的角度,会被另4个单独设置的属性覆盖
android:topLeftRadius="2dp"//左上角角度
android:topRightRadius="2dp"//右上角角度
android:bottomLeftRadius="2dp"/左下角角度
android:bottomRightRadius="2dp"//右下角角度
/>
<gradient//渐变色填充
android:angle="0"//渐变角度,必须为45的倍数
android:centerX="0"//渐变中心点的横坐标
android:centerY="0"//渐变中心点的纵坐标
android:startColor=" "//渐变的起始色
android:centerColor=" "//渐变中间色
android:endColor=" "//渐变的结束色
android:gradientRadius=""//渐变半径,在type为radial(径向渐变)时有效
android:useLevel="false"//一般为false,在作为StateListDrawable时为true
android:type="linear"//渐变类别,默认为linear(线性渐变),radial(径向渐变),sweep(扫描先渐变)
/>
<solid//纯色填充
android:color=" "//填充的颜色
/>
<stroke//描边,其中dashWidth和dashGap一起设置才会有效果
android:width="2dp"//描边的厚度
android:color=" "//描边的颜色
android:dashWidth="5dp"//虚线描边的每节虚线长度
android:dashGap="5dp"//虚线之间的间隔长度
/>
<padding//所在的控件留白
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"
/>
<size//shape的固有大小,但将drawable设置为背景时,其还是会根据控件的大小进行自适应
android:width="10dp"
android:height="10dp"
/>
</shape>

LayerDrawable

层次化的drawable集合,将不同的drawable放到不同的层去显示达到叠加后的效果

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item//一个item为一层显示,后面的item后遮盖上面的item
android:drawable=" "//显示的drawable
android:id=" "//设置id
android:top=" "//顶部偏移量
android:bottom=" "//底部偏移量
android:left=" "//左边偏移量
android:right=" "//右边偏移量
/>
<item
...>
<shape ...>//也可以在item中直接自定义drawable
</item>
</layer-list>

StateListDrawable

根据view的不同状态来设置不同的背景

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
android:constantSize="false"//固有大小是否随drawable的改变而改变,默认false为改变
android:dither="true"//抖动效果
android:variablePadding="true"//其padding是否随drawable的改变而改变,默认为true不改变,其padding为所有drawable的最大值
>
<item//一个item表示一种状态 
android:state_pressed="true"//按下为松开的状态,false为不处于此状态
android:drawable=" "//此状态下要先显示的drawable
/>
//其他的状态
//state_focused:view获取焦点
//state_selected:用户选择了view
//state_checked:用户选中了view,使用与checkBox
//state_enabled:当前view可用
//若没有注明状态,则为默认状态
</selector>

LevelListDrawable

根据当前等级来显示不同的drawable,可通过Drawable或者ImageView的setImageLevel来设置等级,默认为0,最大等级为10000.

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item//一个item为一个drawable,在其最大值和最小值之间的等级就会显示他
android:drawable=" "//显示的drawable
android:maxLevel=" "//最大的等级,默认为10000
android:minLevel=" "//最小的等级,默认为0
/>
</level-list>

TransitionDrawable

实现两个drawable之间的淡入淡出的转化效果

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable=" "/>//一个item代表一个
<item android:drawable=" "/>
</transition>

将上面的drawable设置为View的背景,要切换时在代码中调用如下代码:

TextView tv = findViewById(R.id.tv);
TransitionDrawable drawable = (TransitionDrawable)tv.getBackGround();
drawable.startTransition(1000);//开始转换动画,进行两个item的切换

InsetDrawable

将其他drawable内嵌到自身中并留白,可使drawable做背景时,比实际区域小

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable=" "//显示的drawable
android:insetTop=" "//留白
android:insetBottom=" "
android:insetLeft=" "
android:insetRight=" "
/>

ScaleDrawable

根据等级缩放,其等级为0时不会显示,等级越大显示的越大,缩放越大显示的越小.需要注意的是,其缩放公式(伪代码)为

if(level != 0){
width -= width * (10000-level) * scaleWidth/10000
}
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable=" "//显示的drawable
android:scaleGravity=" "//与gravity一样
android:scaleHeight="25%"//按比例缩放高
android:scaleWidth="25%"//按比例缩放宽
/>

以上的drawable没有设置等级,无法显示,可在代码中设置等级

((ScaleDrawable)view.getBackground()).setlevel(1);//设置drawable的等级

ClipDrawable

用于裁剪drawable,其等级表示裁剪剩余多少,如0为全裁剪,10000为不裁剪,666为裁剪(666/10000)*100%

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable=" "//处理的drawable
android:clipOrientation=" "//裁剪方向,horizontal(水平方向),vertical(竖直方向)
android:gravity=" "//裁剪方式
/>

裁剪方式比较多,且可以多个使用”|”一起组合
top : 放在顶部,为vertical时,从底部开始裁剪,不改变大小
bottom : 放在底部,为vertical时,从顶部开始裁剪,不改变大小
left : 放在左边,为horizontal时,从右边开始裁剪,不改变大小
right : 放在右边,为horizontal时,从左边开始裁剪,不改变大小
center_vertical : 竖直居中,为vertical时,上下同时开始裁剪,不改变大小
fill_vertical : 竖直方向上填充容器,会改变大小,当等级为0时会不显示
center_horizontal : 水平居中,horizontal时,从左右两边同时裁剪,不改变大小
fill_horizontal : 水平方向上填充容器,会改变大小,等级为0时不显示
center : 水平和竖直方向都居中,为vertical时,上下同时裁剪,为horizontal时,左右同时裁剪,不改变大小
fill : 水平和竖直方向同时填充,会改变大小,等级为0时不显示
clip_vertical : 竖直方向的裁剪
clip_horizontal : 水平方向的裁剪

小结

Drawable一般用来作为控件的背景,而android提供了很多的Drawable,这些已经可以满足一般开发的需求,并且使用drawable可以很轻松的实现我们想要的效果~~

个人博客:https://lewis-v.github.io/

公众号 个人公众号

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值