Drawable(67题)
版本:2018/5/11-1(0:44)
思维导图
- Drawable(67题)
- 思维导图
- 基本知识点
- Drawable分类
- 1-ColorDrawable(color)
- 2-BitmapDrawable(xml标签:bitmap)
- 3-NinePatchDrawable(nine-patch)
- 4-GradientDrawable(shape)
- 5-LayerDrawable(layer-list)
- 6-StateListDrawable(selector)
- 7-LevelListDrawable(level-list)
- 8-TransitionDrawable(transition)
- 9-InsetDrawable(inset)
- 10-ScaleDrawable(scale)
- 11-ClipDrawable(clip)
- 12-RotateDrawable(rotate)
- 13-AnimationDrawable(animation-list)
- 14-ShapeDrawable(无标签)
- 15-RippleDrawable(ripple)
- 16-AnimatedStateListDrawable
- SVG矢量图
- 自定义Drawable
- 实例
- 参考资料
基本知识点
1.Drawable是什么
1、Drawable是什么?
- Android中Drawable是一个抽象类(一种可以在Canvas上进行绘制的抽象的概念),每个具体的Drawable都是其子类。
- Drawable提供一种比自定义View更轻量级的解决办法,用于实现特定的效果.
- Drawable能实现缩放、渐变、动画等效果。颜色、图片等都可以是一个Drawable。
- Drawable可以通过XML定义,或者通过代码创建
2.Drawable的优点
2、Drawable的优点
- 使用简单,比自定义View成本低
- 非图片类的Drawable所占空间小,能减小apk大小
3.Drawable的内部宽/高
3、Drawable的内部宽/高
- 一般
getIntrinsicWidth/Height
能获得内部宽/高- 图片Drawable其内部宽高就是图片的宽高
- 颜色Drawable没有内部宽高的概念
- 内部宽高不等同于它的大小,一般Drawable没有大小概念(作为View背景时,会被拉伸至View的大小)
Drawable分类
1-ColorDrawable(color)
4、ColorDrawable的作用
- 纯色Drawable
- 标签为
color
5、ColorDrawable的实例
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
</color>
2-BitmapDrawable(xml标签:bitmap)
6、BitmapDrawable的作用
- 表示为一种图片
- 标签为
bitmap
7、BitmapDrawable可以直接引用原始图片(如ImageView)
android:src="@drawable/ic_launcher" //引用原始图片
imageView.getDrawable(); //获取src指定的Drawable,本身就是BitmapDrawable
8、BitmapDrawable也可以通过XML
进行描述
//mybitmap.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@color/colorPrimary"
android:antialias="true"
android:dither="true"
android:filter="true"
android:gravity="center"
android:mipMap="false"
android:tileMode="disabled"
android:tint="@color/colorPrimaryDark"
android:tintMode="screen"
/>
android:src="@drawable/bitmap" //引用xml描述的BitmapDrawable
属性
9、BitmapDrawable具有的基本属性
属性 | 作用 | 备注 |
---|---|---|
android:src | 图片资源ID | |
android:antialias | 图片抗锯齿-图片平滑,清晰度降低 | 应该开启 |
android:dither | 开启抖动效果-用于高质量图片在低质量屏幕上保存较好的显示效果(不会失真) | 应该开启 |
android:filter | 开启过滤-在图片尺寸拉伸和压缩时保持较好的显示效果 | 应该开启 |
android:mipMap | 纹理映射-图像处理技术 | 默认false |
android:tileMode | 平铺模式-repeat单纯重复、mirror镜面反射、clamp图片四周像素扩散 | 默认disable关闭 |
android:tint和android:tintMode
9、BitmapDrawable的属性android:tint
的作用
android:tint="@color/colorPrimary"
- 会将所有有颜色的地方都着色为指定颜色,且保留透明度。
10、android:tintMode=”xxx”有哪些着色模式
- android:tint指定的颜色就是
src源
,原来的内容属于dst目标
- 着色模式按照
PorterDuffMode
进行混合,可以参考View绘制详解
11、着色模式的分类
着色模式 | 作用 |
---|---|
src_in | 【默认】着色有颜色区域 |
src_over | |
src_atop | |
add | 相加 |
multiply | 混合相乘 |
screen | 混合变淡 |
12、着色模式的设置(Java)
View bitmapView = findViewById(R.id.bitmap_view);
BitmapDrawable bitmapDrawable = (BitmapDrawable) bitmapView.getBackground();
bitmapDrawable.setTintMode(PorterDuff.Mode.SCREEN);
android:gravity
13、android:gravity属性的作用
图片小于容器尺寸时,对图片进行定位-选项之间用‘|’来组合使用
可选项 | 含义 |
---|---|
top/bottom/left/right | 将图片放在容器上/下/左/右,不改变图片大小 |
center_vertical/horizontal | 垂直居中/水平居中,不改变图片大小 |
center | 水平和垂直方向同时居中,不改变图片大小 |
fill_vertical/horizontal | 垂直/水平方向填充容器 |
fill | 水平和垂直方向同时填充容器 |
clip_vertical/horizontal | 垂直/水平方向的裁剪-较少使用 |
3-NinePatchDrawable(nine-patch)
14、NinePatchDrawable的作用
- 自动根据宽高进行缩放且不会失真
- 使用:可以直接引用图片或者通过XML描述
- 也具有
tint和tintMode
属性。
15、NinePatchDrawable的实例
<?xml version="1.0" encoding="utf-8"?>
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@color/colorPrimary"
android:antialias="true"
android:dither="true"
android:filter="true"
android:gravity="center"
android:mipMap="false"
android:tileMode="disabled"
/>
4-GradientDrawable(shape)
16、GradientDrawable的作用
- 能构造出纯色、渐变、圆角等效果的图形。
shape
标签创建的Drawable实体是GradientDrawable
17、GradientDrawable的实例
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
<gradient
android:angle="45"
android:centerX="30"
android:centerY="30"
android:centerColor="@color/colorAccent"
android:endColor="@color/colorPrimary"
android:startColor="@color/colorPrimaryDark"
android:gradientRadius="20"
android:type="linear"
android:useLevel="true" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
<size
android:width="200dp"
android:height="200dp" />
<solid
android:color="@color/colorPrimary"/>
<stroke
android:width="10dp"
android:color="@color/colorAccent"
android:dashWidth="5dp"
android:dashGap="3dp"/>
</shape>
属性
18、GradientDrawable的属性
属性/标签 | 作用 | 备注 |
---|---|---|
android:shape | 图形的形状:rectangle矩形、oval椭圆、line横线、ring圆环 | corners 标签对应于矩形;line和ring通过stroke 指定线的宽度和颜色; ring圆环有五个特殊的shape属性 |
corners 标签 |
四个角的角度 | |
gradient 标签 |
渐变效果-android:angle表示渐变角度,必须为45的倍数 | android:type指明渐变类型:linear线性,radial径向、sweep扫描 |
solid 标签 |
纯色填充 | 与gradient标签排斥 |
stroke 标签 |
描边 | 有描边线和虚线 |
size 标签 |
表示shape的固有大小,并非最终显示的大小 | 没有时getIntrinsicWidth返回-1;能指明Drawable的固有宽高,但如果作为View背景还是会被拉伸 |
5-LayerDrawable(layer-list)
19、LayerDrawable的作用
- 层次化的Drawable合集.
- 可以包含多个
item
,每个item表示一个Drawable, 后者覆盖在前者Item之上。- item中可以通过
android:drawable
直接引用资源。android:top
等表示Drawable相当于View上下左右的偏移量。
20、LayerDrawable的实例
1、微信文本输入框:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid
android:color="#0ac39e"/>
</shape>
</item>
<item
android:bottom="6dp">
<shape android:shape="rectangle">
<solid
android:color="#FFFFFF"/>
</shape>
</item>
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape android:shape="rectangle">
<solid
android:color="#FFFFFF"/>
</shape>
</item>
</layer-list>
2、图片的默认图片-不会被拉伸
6-StateListDrawable(selector)
21、StateListDrawable的作用
- 用于View根据不同状态选择不同的
Drawable
- 标签为
selector
22、与AnimatedStateListDrawable
的区别
StateListDrawable
瞬间切换图片,显得比较突兀。AnimatedStateListDrawable
是根据不同状态选择不同的动画效果,更平滑流畅。
23、StateLi