微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android正确绘制笔触宽度的矢量的方法

如何解决Android正确绘制笔触宽度的矢量的方法

嗨,我正在尝试在我的android应用中使用vector drawable。

这是相同的xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="1024dp"
    android:height="1024dp"
    android:viewportWidth="1024"
    android:viewportHeight="1024">
    <path
        android:pathData="M 0,0 L 50,50 z"
        android:strokeColor="#000000" />
</vector>

这是它的外观,没有任何笔触宽度

enter image description here

如果我添加 android:strokeWidth =“ 10” ,则外观如下

enter image description here

所有行的笔画宽度不一致(相同宽度),并且最左边的点似乎没有被切掉

有没有办法使所有绘制的3条线都一致(相同的宽度)而不是不规则的?

解决方法

将可绘制对象想象为一个坐标系统,其中点p(0,0)位于左上角。您的x轴从左上角到右上角,您的y轴从左上角到左下角。

在创建路径时,它将从坐标系中的一个点移动到另一个点。当您抚摸此路径时,绘制的线有一些strokeWidth,默认情况下为1。但是,当您将宽度设置为10时,这条线在您的坐标系中要粗得多,但是如果您的路径已经在y = 0处,那么该“厚度”应该去哪里?可绘制对象不会显示任何负坐标。这就是您的可绘制对象被切断的原因。

解决方案: 将strokeWidth添加到小于strokeWidth的坐标中+ 从坐标中减去strokeWidth,该坐标大于您的viewportWidth-strokeWidth和/或viewportHeight-strokeWidth +为位于视口边界处的边缘提供间距(在此示例中,额外的间距为3很好)

所以您的代码应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="1024dp"
    android:height="1024dp"
    android:viewportWidth="1024"
    android:viewportHeight="1024">
    <path
        android:pathData="M 13,13 L 50,50 z"
        android:strokeColor="#000000"
        android:strokeWidth="10"/>
</vector>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。