多个 Android Drawable shape 组合画田字格

本文介绍如何使用Android DrawableShape及layer-list实现田字格样式背景。通过组合多个形状元素,包括矩形、线条,并利用旋转效果绘制虚线交叉,最终达到所需图形效果。

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

Android Drawable Shape 组合画田字格

我们常用Android Drawable Shape来创建Android的背景、圆角边框、分隔线等图形。这样的教程网上有许多。象下面的

我也常在项目中使用。但最近想做一个田字格背景,用上面的方法就不行了。


以前我对Android Drawable shape的了解并不深入,经过在网上不断寻找,最后找到了个较全面的文档

看过这个文档后,对Android Drawable shape的理解较透了。原来Android Drawable shape不仅可以画简单图形,还可以做动画,还可以加入图片,当然还有我需要的功能,多个Shape组合,构建较复杂的图形。


使用layer-list组合多个Shap

话不多说,把我弄的代码贴出来

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="96dp"
                android:height="96dp"></size>
            <stroke
                android:width="1dp"
                android:color="#FF0000" />
        </shape>
    </item>
    
    <item>
        <rotate
            android:fromDegrees="90"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="90">
            <shape android:shape="line">
                <stroke
                    android:width="1dp"
                    android:color="#FF0000"
                    android:dashGap="3dp"
                    android:dashWidth="1dp" />
            </shape>
        </rotate>
    </item>
    
    <item>
        <shape android:shape="line">
            <stroke
                android:width="1dp"
                android:color="#FF0000"
                android:dashGap="3dp"
                android:dashWidth="1dp" />
        </shape>
    </item>
</layer-list>
画出的田字格图形如下
多个shape组合画田字格

上面的代码也很简单,使用了一个标签layer-list,中间套着多个item,每个item中加一个shape。

上面的代码还有两点需要说一下:

1、必须设置宽高,不然竖线只能画和宽度一样长。

2、画竖线的方法就是横线加旋转。那个旋转的设置还是比较麻烦的。旋转的代码如下

<rotate
            android:fromDegrees="90"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="90">
            <shape android:shape="line">
                <stroke
                    android:width="1dp"
                    android:color="#FF0000"
                    android:dashGap="3dp"
                    android:dashWidth="1dp" />
            </shape>
        </rotate>

下面介绍一下那四个属性

android:fromDegrees="90"  开始的角度
android:toDegrees="90"      结束的角度

开始和结束都是90度,就竖起来了

android:pivotX="50%"   旋转中心点X座标,可以使用百分比设置
android:pivotY="50%"   旋转中心点Y座标,可以使用百分比设置
都设成50%就是在中心旋转。


关于 Android Drawable shape 的更从介绍可以看文档:Android Drawable XML Documentation

Android中,Drawable Shape是一种自定义视图绘制形状的技术,它可以让你在布局文件中创建各种复杂的图形,如圆形、矩形、弧线等。如果你想要在Shape中添加文字,通常会结合`<vector>`标签,因为Android 5.0 (Lollipop)引入了向量绘图的支持。 首先,你需要在`res/drawable`目录下创建一个新的XML文件,比如`custom_shape.xml`: ```xml <vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="wrap_content" android:width="wrap_content" android:viewportWidth="100" android:viewportHeight="100"> <!-- 定义shape --> <group> <path android:pathData="M50,0 L50,100 A50,50 0 0,1 0,50 L100,50 A50,50 0 0,1 100,0 Z" /> <!-- 添加文字 --> <text android:text="Sample Text" android:fontFamily="@font/your_font_family" android:layout_width="wrap_content" android:layout_height="wrap_content" android:x="40" <!-- 文字x坐标调整位置 --> android:y="60" <!-- 文字y坐标调整位置 --> /> </group> </vector> ``` 在这个例子中,我们首先定义了一个路径(path)来表示形状,然后嵌套了一个`text`元素来放置文字。通过调整`android:x`和`android:y`属性,你可以定位文字在shape内的位置。 要使用这个自定义的drawable,只需在你的布局文件中像引用其他drawable一样引用它,并设置宽高属性: ```xml <ImageView android:src="@drawable/custom_shape" android:layout_width="wrap_content" android:layout_height="wrap_content"/> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值