android - 防止形状可绘制笔划的部分重叠

标签 android android-shapedrawable

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

这是我的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>

这是实现的目标
Sample Image

如您所见,描边与实心部分重叠 50%,但我希望它是 100%。

最佳答案

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

<?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>

您可以调整偏移以获得外部或内部笔划

Outer & inner stroke

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

关于android - 防止形状可绘制笔划的部分重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40169488/

相关文章:

android - 在设备中加载大背景图像的问题(Andengine)

android - Android 上的消息应用程序 (mms)

android - 如何获取 styles.xml 文件的资源标识符?

android - OpenCVAndroid Imgproc.GaussianBlur 应用程序停止

android - 如何绘制具有可变笔划宽度的Android Shape Rectangle

android - 如何在 Android 中获得虚线水平进度条?

android - 如何检测Android平板电脑中充当USB Host的USB设备?

Android:为按钮添加 NEON 效果

Android:使用 Shape Drawable 的多虚线背景