Android Drawable详解-思维导图版

本文深入探讨了Android的Drawable,包括Drawable的基本概念、优点、内部宽高和各类Drawable的详细讲解,如ColorDrawable、BitmapDrawable、NinePatchDrawable等。还介绍了如何实现圆形/圆角图片的6种方法、SVG图标以及使用资源。同时提供了矢量图相关网站和参考资料。
摘要由CSDN通过智能技术生成

Drawable(67题)

版本:2018/5/11-1(0:44)

思维导图

思维导图详情点这里
Drawable思维导图

基本知识点

1.Drawable是什么

1、Drawable是什么?

  1. Android中Drawable是一个抽象类(一种可以在Canvas上进行绘制的抽象的概念),每个具体的Drawable都是其子类。
  2. Drawable提供一种比自定义View更轻量级的解决办法,用于实现特定的效果.
  3. Drawable能实现缩放、渐变、动画等效果。颜色、图片等都可以是一个Drawable。
  4. Drawable可以通过XML定义,或者通过代码创建

2.Drawable的优点

2、Drawable的优点

  1. 使用简单,比自定义View成本低
  2. 非图片类的Drawable所占空间小,能减小apk大小

3.Drawable的内部宽/高

3、Drawable的内部宽/高

  1. 一般getIntrinsicWidth/Height能获得内部宽/高
  2. 图片Drawable其内部宽高就是图片的宽高
  3. 颜色Drawable没有内部宽高的概念
  4. 内部宽高不等同于它的大小,一般Drawable没有大小概念(作为View背景时,会被拉伸至View的大小)

Drawable分类

1-ColorDrawable(color)

4、ColorDrawable的作用

  1. 纯色Drawable
  2. 标签为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的作用

  1. 表示为一种图片
  2. 标签为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的作用

  1. android:tint="@color/colorPrimary"
  2. 会将所有有颜色的地方都着色为指定颜色,且保留透明度。

10、android:tintMode=”xxx”有哪些着色模式

  1. android:tint指定的颜色就是src源,原来的内容属于dst目标
  2. 着色模式按照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的作用

  1. 自动根据宽高进行缩放且不会失真
  2. 使用:可以直接引用图片或者通过XML描述
  3. 也具有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的作用

  1. 能构造出纯色、渐变、圆角等效果的图形。
  2. 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的作用

  1. 层次化的Drawable合集.
  2. 可以包含多个item,每个item表示一个Drawable, 后者覆盖在前者Item之上。
  3. item中可以通过android:drawable直接引用资源。
  4. 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的作用

  1. 用于View根据不同状态选择不同的Drawable
  2. 标签为selector

22、与AnimatedStateListDrawable的区别

  1. StateListDrawable瞬间切换图片,显得比较突兀。
  2. AnimatedStateListDrawable是根据不同状态选择不同的动画效果,更平滑流畅。

23、StateLi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值