Android Drawable系列(1):自定义背景以及注意事项

本文详细介绍Android中ShapeDrawable的使用方法,包括Shape自身属性、padding、size等关键配置项,以及solid、stroke、corners和gradient等填充方式。通过具体实例展示如何创建不同类型的图形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0、 Shape自身属性

android:shape=["rectangle" | "oval" | "line" | "ring"] //指定shape的形状,矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)
android:tint="#FFFFFFFF" //将图片渲染成指定颜色,无论solid中定义的是什么颜色,可以用于改变图标的颜色
android:dither=true //图像的抖动处理,当每个颜色值以低于8位表示时,对应图像做抖动处理可以实现在可显示颜色总数比较低(比如256色)时还保持较好的显示效果
android:tintMode //API 21+未做详细研究
android:innerRadius //内环的半径 以下属性均为shape=ring 时有效使用
android:innerRadiusRatio //浮点型,以环的宽度比率来表示内环的半径,例如,如果android:innerRadiusRatio="5",表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.
android:thickness //环的厚度
android:thicknessRatio //浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.
注意
android:shape="ring"时,useLevel必须设为false

1、 padding

android:top、bottom、left、right //上下左右,很简单就是显示在shape上的内容与shape边界的距离

2、 size

android:width、 android:height
用于矩形时,设置矩形的宽高;
用于椭圆时,设置椭圆外切矩形的宽高,当宽与高相等时,为正圆,否则为椭圆;
用于直线时,宽代表了直线的长度,高没有效果
用于圆环时,宽会影响内圆半径和圆环的宽窄,高没有效果。
注意:当我们将一个shape drawable作为一个View组件的背景或是内容时,通常这个drawable都会被缩放。所以实际显示的时候可能与这里的size设置不相符。

3、 solid

android:color // 实心之意,用这种颜色来填充该shape内部,如果不设置该shape的边框,就能得到一张纯色的drawable。

4、 stroke

用笔画出来的轨迹,笔画”的意思,这里指shape的边框。
用于矩形和椭圆时,设置其外边界
用于直线时,呃,直线就是用这个节点来设置的。
用于圆环时,会同时影响内圆和外圆。
android:width//边框线条的宽度,特别地,用于直线时设置直线的粗细。
android:color//设置线条的颜色
android:dashWidth//设置虚线效果里实点的长度
android:dashGap//设置虚线效果里空白点的长度

5、 corners

只用于android:shape="rectangle",用于设置矩形的4个角的圆角半径,生成圆角矩形。
android:radius——同时设置矩形4个角的圆角半径
android:topLeftRadius——左上角圆角半径
android:topRightRadius——右上角圆角半径
android:bottomLeftRadius——左下角圆角半径
android:bottomRightRadius——右下角圆角半径
注意

  • 这四个单独的设置的值会覆盖android:radius设置的值。
    <corners
        android:bottomLeftRadius="15dp"
        android:radius="5dp"
        android:topLeftRadius="15dp" />

Android2.3.7模拟器下

  • 兼容性问题 使用以下代码
    <corners
        android:topLeftRadius="5dp"
        android:bottomLeftRadius="5dp" />

Android2.3.7模拟器下
Android2.3.7模拟器下left属性有问题
Android4.4.2模拟器下
Android4.4.2模拟器下

解决方案
在res目录下新建一个名为drawable-v12的文件夹,将适配Android3.1+的资源文件(正确的左右方向)放在此文件夹中即可

6、 gradient

与solid一样均为shape填充的一种方式,优先级取决于谁被写在后面
android:startColor、endColor 两者至少得有一个,另一个不指定时默认为白色
android:type 渐变类型 linear(线性渐变),radial(径向渐变)与android:gradientRadius(用于指定渐变区域圆的半径)属性一同使用,sweep(扫描渐变,顺时针方向渐变,起点为3点钟方向)
android:angle 指定线性渐变的方向 必须为45的倍数,即只能指定八个方向
android:centerX、android:centerY 径向渐变与扫描渐变时指定中心点位置

7、 典型举例

  • a. tint红色覆盖gradient中定义的蓝色,gradient中定义android:type为径向渐变,所以必须指定一个android:gradientRadius的值。
    典型举例1:tint与gradient
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:tint="@color/main_red">
    <solid android:color="@color/main_green" />
    <gradient
        android:startColor="@color/main_blue"
        android:gradientRadius="100dp"
        android:type="radial"/>
    <size
        android:width="200dp"
        android:height="200dp" />
</shape>
  • b. 总宽度为(25+50+2)*2>150所以左右边会有被裁减痕迹,边框为红色;渐变为从左上角开始从绿色变为蓝色。
    典型举例2:ring及其相关属性,以及渐变
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="25dp"
    android:shape="ring"
    android:thickness="50dp"
    android:useLevel="false">
    <stroke
        android:width="2dp"
        android:color="@color/main_red" />
    <gradient
        android:angle="135"
        android:endColor="@color/main_green"
        android:startColor="@color/main_blue"
        android:type="linear" />
    <size
        android:width="150dp"
        android:height="150dp" />
</shape>

8、参考文章

Shape Drawable的学习
Android中shape的使用
android圆角View实现及不同版本这间的兼容
How can I work around Android issue 9161, where bottomRightRadius and bottomLeftRadius are swapped?

转载于:https://www.cnblogs.com/qingjian/p/4772833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值