【Android】【Drawable总结】- Drawable家族


督促自己学习总结,特用文章的形式记录下来,共同进步
Android中用到Drawable的地方很多,Google提供了很多Drawable的子类,实现不同的效果,以前只是使用的时候再去查资料,并没有最所有Drawable做一个总结,现有时间做一个总结。了解Android的Drawable后能提高一些开发效率

先来一张Drawable的家族,可以看到有很多,本篇文章不会涉及到所有的,只涉及到一些常用,其他的读者可以自己去尝试了解或者后续有空在补充。
在这里插入图片描述

BitmapDrawable

BitmapDrawable是使用最多,可以直接在xml中定义,标签是<bitmap>

gravity

当图片小于容器的尺寸时,设置此选项可以对图片进行定位,这个属性的可选项比较多,相互之间可以通过“|”来组合使用

可选项含义
top将图片放在容器顶部,不改变图片大小
bottom将图片放在容器低部,不改变图片大小
left将图片放在容器左部,不改变图片大小
right将图片放在容器右部,不改变图片大小
center_vertical竖直居中,不改变图片大小
fill_vertical竖直方向填充,会竖直拉伸
center_horizontal水平居中,不改变图片大小
fill_horizontal水平拉伸
center居中,不改变图片大小
fill水平和竖直方向都拉伸,会改变图片大小,默认配置

top

将图片放在容器顶部,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="top"
    android:src="@drawable/ycy">
    <!-- android:src 资源id-->
    <!-- android:antialias 是否开启图片抗锯齿功能   -->
    <!-- android:dither 是否开启抖动效果  -->
    <!-- android:filter 是否开启过滤功能   -->
    <!-- android:gravity 当图片小于容器的尺寸时,设置此选项可以对图片进行定位   -->
    <!-- android:tileMode 平铺模式-->
</bitmap>

在这里插入图片描述

bottom

将图片放在容器低部,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="bottom"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

left

将图片放在容器左部,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="left"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

right

将图片放在容器右部,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="right"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

center_vertical

竖直居中,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

fill_vertical

竖直方向填充,会竖直拉伸

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="fill_vertical"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

center_horizontal

水平居中,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_horizontal"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

fill_horizontal

水平拉伸

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="fill_horizontal"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

center

居中,不改变图片大小

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

fill

水平和竖直方向都拉伸,会改变图片大小,默认配置

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="fill"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

top|left

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="top|left"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

right|bottom

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="bottom|right"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

tileMode

平铺模式

disabled

默认的,关闭平铺模式,当开启平铺模式后,gravity属性无效

clamp

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="clamp"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

repeat

直接平铺

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="repeat"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

mirror

水平和竖直方向镜面投影效果

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="mirror"
    android:src="@drawable/ycy">
</bitmap>

在这里插入图片描述

NinePatchDrawable

NinePatchDrawable主要针对.9图片

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ycy9">
</nine-patch>

在这里插入图片描述
图片会按照.9图片的设置进行拉伸

ShapeDrawable

shape

rectangle [矩形]

普通矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorAccent" />
</shape>

在这里插入图片描述

corners 圆角
corners 带有圆角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--  背景-->
    <solid android:color="@color/colorAccent" />
    <!--  圆角-->
    <corners android:radius="10dp" />
    <!--  大小-->
    <size
        android:width="100dp"
        android:height="100dp" />
</shape>

在这里插入图片描述

corners 带有圆角,四个圆角半径不一样
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--  背景-->
    <solid android:color="@color/colorAccent" />
    <!--  圆角-->
    <!--  radius优先级最低 会被其他四个数据覆盖  -->
    <corners
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="20dp"
        android:radius="10dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="10dp" />
    <size
        android:width="100dp"
        android:height="50dp" />
</shape>

在这里插入图片描述

corners 带有圆角,四个圆角半径不一样(作为button背景)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--  背景-->
    <solid android:color="@color/colorAccent" />
    <!--  圆角-->
    <!--  radius优先级最低 会被其他四个数据覆盖  -->
    <corners
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp" />
    <size
        android:width="200dp"
        android:height="100dp" />
</shape>

在这里插入图片描述

gradient 渐变
普通渐变
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="@color/black"
        android:startColor="@color/white"
        android:type="linear"
        android:useLevel="false" />
</shape>

在这里插入图片描述

中间色渐变
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerColor="@color/red"
        android:endColor="@color/black"
        android:startColor="@color/white"
        android:type="linear"
        android:useLevel="false" />
</shape>

在这里插入图片描述

stroke 描边
属性说明
width描边的宽度
color描边的颜色
dashWidth组成虚线的线段的宽度
dashGap组成虚线的线段之间的间隔
example 1
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorAccent" />
    <stroke
        android:width="5dp"
        android:color="@color/red"
        android:dashWidth="20dp"
        android:dashGap="10dp" />
</shape>

在这里插入图片描述

oval [椭圆]

1.普通椭圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorAccent" />
</shape>

这是一个没有大小的椭圆,默认会覆盖所有背景
在这里插入图片描述
2.带大小的椭圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:dither="true"
    android:useLevel="false">

    <solid android:color="@color/red" />
    <stroke
        android:width="5dp"
        android:color="@color/black" />
    <size
        android:width="50dp"
        android:height="50dp" />

    <corners android:radius="25dp" />
</shape>

在这里插入图片描述

line [横线]

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <solid android:color="@color/colorAccent" />
    <stroke
        android:width="2dp"
        android:color="@color/colorPrimary"
        />
</shape>

在这里插入图片描述

ring [圆环]

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="30dp"
    android:innerRadiusRatio="9"
    android:shape="ring"
    android:thickness="10dp"
    android:thicknessRatio="9"
    android:useLevel="false">
    <solid android:color="@color/colorAccent" />

    <stroke android:width="2dp" />
</shape>

在这里插入图片描述

LayerDrawable

LayerDrawable对应的标签是<layer-list>
是将不同的Drawable放置在不同的层从而达到一种叠加后的效果
1.旋转

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="-10"
            android:pivotX="0"
            android:pivotY="0"
            android:toDegrees="-10">
            <bitmap android:src="@drawable/ycy" />
        </rotate>
    </item>

    <item>
        <rotate
            android:fromDegrees="15"
            android:pivotX="0"
            android:pivotY="0"
            android:toDegrees="15">
            <bitmap android:src="@drawable/ycy" />
        </rotate>
    </item>

    <item>
        <rotate
            android:fromDegrees="55"
            android:pivotX="0"
            android:pivotY="0"
            android:toDegrees="55">
            <bitmap android:src="@drawable/ycy" />
        </rotate>
    </item>
</layer-list>

在这里插入图片描述
2.阴影

Button背景
3.输入框背景
在这里插入图片描述

StateListDrawable

StateListDrawable对应于<selector>标签
它表示Drawable的集合,每个Drawable都对应着View的一种状态,这样系统会根据View的状态来选择合适的Drawable
主要用来设置可点击View的背景,比如Button,俗称状态Drawable,或者说点击Drawable,在开发中很常用,显示点击效果,便于用户感知到操作

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

在这里插入图片描述

代码

GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值