Android xml 代码绘制图形之 Shape 详解

引言

Android 应用程序开发离不开图标,图标具有释义、美化、引导的作用,android开发工具往往提供了xml绘制简单图形的方法,可不必依赖UI工程师切图,而且图片样式能随意的更改,既方便又节省空间,使用起来非常便捷。具体可以分为 shape、layer-list、vector三种方法,本文将详细介绍shape的使用方法

Shape使用方法

  • 在'res/drawable'下创建xml资源文件,编辑属性
  • 使用该资源文件的方式和图片资源一样

1. Shape根节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

    <!--以下4个属性只有当类型为环形时才有效-->
    android:innerRadius="dimension"     <!--内环半径-->
    android:innerRadiusRatio="float"    <!--内环半径相对于环的宽度的比例,比如环的宽度为100,比例为2.0,那么内环半径为50-->
    android:thickness="dimension"       <!--环的厚度-->
    android:thicknessRatio="float"      <!--环的厚度相对于环的宽度的比例-->
    android:useLevel="boolean">         <!--如果当做是LevelListDrawable使用时值为true,否则为false-->
</shape>

2. Corners 圆角节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]>  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

    <corners  <!--圆角节点,非必须-->
        android:radius="dimen"                <!--全部的圆角半径-->
        android:topLeftRadius="dimen"         <!--左上角的圆角半径-->
        android:topRightRadius="dimen"        <!--右上角的圆角半径-->
        android:bottomLeftRadius="dimen"      <!--左下角的圆角半径-->
        android:bottomRightRadius="dimen" />  <!--右下角的圆角半径-->

</shape>

3. Gradient 渐变色节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]>  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

    <gradient  <!--渐变色节点,非必须-->
        android:type=["linear" | "radial" | "sweep"]  <!--共有3钟渐变类型:线性(默认)、放射式、扫描式-->
        android:angle="integer"                       <!--渐变角度,必须为45的倍数,0为从左到右,90为从上到下-->
        android:centerX="float"                       <!--渐变中心 X 的相当位置,范围为 0 ~ 1-->
        android:centerY="float"                       <!--渐变中心 Y 的相当位置,范围为 0 ~ 1-->
        android:startColor="color"                    <!--渐变开始点的颜色-->
        android:centerColor="color"                   <!--渐变中间点的颜色,在开始与结束点之间-->
        android:endColor="color"                      <!--渐变结束点的颜色-->
        android:gradientRadius="float"                <!--渐变的半径,只有当渐变类型为 radial 时才能使用-->
        android:useLevel=["true" | "false"] />        <!--使用LevelListDrawable时就要设置为true。设为false时才有渐变效果-->
</shape>

4. Stroke 描边(边框)节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]>  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

    <stroke  <!--描边(边框)节点、非必须-->
        android:width="dimen"       <!--描边的宽度-->
        android:color="color"       <!--描边的颜色-->

        <!--以下两个属性设置虚线-->
        android:dashWidth="dimen"   <!--虚线的宽度,值为0时是实线-->
        android:dashGap="dimen" />  <!--虚线的间隔-->
</shape>

5. Size 图形尺寸节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]>  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

    <size  <!--图形尺寸节点-->
        android:width="dimen"
        android:height="dimen" />
</shape>

6. Solid 图形填充色节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]>  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

    <solid  <!--图形填充色节点-->
        android:color="color" />
</shape>

7. Padding 内部边距节点属性介绍

<?xml version="1.0" encoding="utf-8"?>
<shape
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape=["rectangle" | "oval" | "line" | "ring"]>  <!--共有4种类型:矩形(默认)、椭圆、直线、环形-->

 <padding  <!--内部边距节点-->
     android:left="dimen"
     android:top="dimen"
     android:right="dimen"
     android:bottom="dimen" />
</shape>

8、Android Studio 创建 shape 的方法

版本: Android Studio Flamingo | 2022.2.1

步骤 1: drawable -> New -> Drawable Resource File

步骤 2:在出现的对话框的 File name中输入 要创建的文件名称,然后在 Root element 中输入shape,最后点击 ok 即可。

本文参考自简书作者:Kael_Zhang的安卓笔记

链接:https://www.jianshu.com/p/fff95eea4c5c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值