1.Drawable
通过getIntrinsicWidth
和getIntrinsicHeight
方法获取内部宽高,但不是所有的都有内部宽高,BitmapDrawable
的内部宽高就是图片的宽高,颜色形成的Drawable
没有宽高。且要注意的是,内部宽高不等于Drawable
大小,Drawable
没有大小概念,做背景时会被拉伸。
2.BitmapDrawable
常见xml
文件如下:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@mipmap/app_icon"
android:antialias="true"
android:dither="true"
android:filter="true"
android:gravity="center"
android:mipMap="true"
android:tileMode="disabled"/>
各属性的解释如下:
src
图片资源id
antialias
是否开启抗锯齿功能,开启后图片变得平滑,但可能影响清晰度
dither
是否开启抖动效果,图片像素和屏幕像素不一样时,开启后可以更好的适配
filter
是否开启过滤效果,图片拉伸或压缩时,开启可以保持较好的显示效果
gravity
图片的定位,如下表:

mipMap
纹理映射,默认是false
tileMode
平铺模式,默认是disabled
,关闭平铺,开启平铺后gravity
会失效,其他几种如下图:

3.ShapeDrawable
常用xml
文件如下:
<?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标签指定宽度颜色,否则显示不正常-->
<!--四个角的角度,只适用于rectangle矩形,radius会被其他属性覆盖-->
<corners android:radius="40dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
<!--和solid标签互斥,表示渐变效果
angle:渐变角度,必须是45的整数倍,0表示从左到右,90表示从上到下
centerX: 渐变中心点横坐标
centerY:渐变中心点纵坐标
startColor:渐变的起色
centerColor:渐变的中间色
endColor:渐变的结束色
gradientRadius:渐变半径,只有type=radial时有效
type:渐变的类型(linear线性渐变,radial径向渐变,sweep扫描线渐变)
useLevel:作StateListDrawable用时为true
-->
<gradient android:angle="45"
android:centerColor="@color/colorAccent"
android:centerX="0.5"
android:centerY="0.5"
android:endColor="@color/colorPrimary"
android:gradientRadius="5dp"
android:startColor="@color/colorPrimaryDark"
android:type="radial"
android:useLevel="true"/>
<!--表示包含它的View的空白-->
<padding android:left="10dp"
android:bottom="10dp"
android:right="10dp"
android:top="10dp"/>
<!--Shape的固定宽高,但不是最终宽高,一般会被拉伸-->
<size android:width="100dp"
android:height="100dp"/>
<!--Shape的纯色填充颜色-->
<solid android:color="#ff0000"/>
<!--Shape的描边
width:描边的宽度
color:描边的颜色
dashGap:虚线间的距离
dashWidth:虚线段的长度
-->
<stroke android:width="10dp"
android:color="@color/colorPrimaryDark"
android:dashGap="10dp"
android:dashWidth="1dp"/>
</shape>
4.layer-list
常用xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/aaa"
android:drawable="@color/colorPrimaryDark"
android:top="@dimen/widget_margin"
android:left="@dimen/widget_margin"
android:right="@dimen/widget_margin"
android:bottom="@dimen/widget_margin"/>
</layer-list>
top、left、right、bottom
都是相对View的偏移量,单位像素。
一个layer-list
可以有多个item
,后面item
的会覆盖前面的item
,通过合理分层达到特殊的叠加效果
5.StateListDrawable
就是常用的selector
,用于View
不同状态下的显示。
6.LevelListDrawable
常用xml
文件如下:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryDark"
android:maxLevel="100"
android:minLevel="0"/>
</level-list>
可以根据不同的等级来切换drawable
,最小0
,最大10000
。
7.TransitionDrawable
常用xml
文件如下:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryDark"/>
<item android:drawable="@color/colorPrimary"/>
</transition>
用来展示两个Drawable
的淡入淡出效果,通过startTransition
和reverseTransition
方法实现淡入淡出效果及其逆过程。
8.InsetDrawable
把其他Drawable
内嵌到自己当中,并在四周留出一定边距,常用xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="10dp"
android:insetLeft="10dp"
android:insetRight="10dp"
android:insetTop="10dp">
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent"></solid>
</shape>
</inset>
9.ScaleDrawable
根据自己的等级将指定的Drawable
缩放一定比例,为0表示不可见(默认),需要在代码中设置Level
才可见,常用xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@color/colorPrimary"
android:scaleGravity="center"
android:scaleHeight="20"
android:scaleWidth="20">
</scale>
<!--scaleHeight和scaleWidth都是百分比-->
10.ClipDrawable
根据自己的等级来裁剪另一个Drawable
,0表示完全裁剪(默认),10000表示完全不裁剪,例如8000表示裁剪20%,常用xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@color/colorPrimary"
android:clipOrientation="vertical"
android:gravity="center">
</clip>
其中,gravity
配合clipOrientation
使用,规则如下:

网友评论