防止形状绘制笔触的部分重叠 [英] prevent partial overlapping of shape drawable stroke

查看:104
本文介绍了防止形状绘制笔触的部分重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以防止strokeshape drawable上部分重叠.我更喜欢将笔划完全重叠在形状的边界上.

Is there any way to prevent partial overlapping of the stroke on the shape drawable. I prefer to overlap the stroke completely on the borders of the shape.

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <padding
        android:bottom="0dp"
        android:top="0dp"
        android:left="0dp"
        android:right="0dp" />
    <solid android:color="@color/green" />
    <stroke android:color="@color/red_50"
        android:width="20dp"
        android:dashGap="2dp"
        android:dashWidth="10dp" />
</shape>

colors.xml

<color name="green">#0f0</color>
<color name="red_50">#8f00</color>

这是实现的目标

And here is what is achieved

如您所见,笔画与实心部分重叠了50%,但我希望它是100%.

As you can see that the stroke is overlapping the solid part by 50% but I want it to be 100%.

推荐答案

尝试将其分为两种形状-一种用于 中风 ,另一种用于 矩形 .在此解决方案中,我操纵矩形的大小,以便可以更改其与边框的关系.

Try to divide it into two shapes - one for stroke and one for rectangle. In this solution I manipulate size of rectangle so that I can change its relation with borders.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:bottom="@dimen/offset"
        android:left="@dimen/offset"
        android:right="@dimen/offset"
        android:top="@dimen/offset">

        <shape android:shape="rectangle">
            <solid android:color="@color/green" />
        </shape>

    </item>

    <item>

        <shape android:shape="rectangle">
            <stroke
                android:width="20dp"
                android:color="@color/red_50"
                android:dashGap="2dp"
                android:dashWidth="10dp" />
        </shape>

    </item>

</layer-list>

您可以调整偏移量以获得外部或内部笔触.

You can adjust the offset to get outer or inner stroke.

这些值来自 透明矩形 (描边层)和 绿色的 的大小差异.在您的情况下,它们将为20dp或无.

这篇关于防止形状绘制笔触的部分重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆