如何在android中添加填充向量文件

如何在android中添加填充向量文件,android,vector,Android,Vector,如何在向量文件中添加paddingleft、Right、Top和bottomveriable 我变了 android:viewportWidth 及 android:viewportHeight 但一切都没有改变 我的向量 <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="31.416dp" android:height="31.416dp" andro

如何在向量文件中添加paddingleft、Right、Top和bottomveriable

我变了

android:viewportWidth

android:viewportHeight

但一切都没有改变

我的向量

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="31.416dp"
    android:height="31.416dp"
    android:viewportWidth="31.416"
    android:viewportHeight="31.416">

    <path
        android:fillColor="#fff"
        android:pathData="M28.755,6.968l-0.47,0.149L25.782,7.34l-0.707,1.129l-0.513-0.163L22.57,6.51l-0.289-0.934L21.894,4.58l-1.252-1.123
l-1.477-0.289l-0.034,0.676l1.447,1.412l0.708,0.834L20.49,6.506l-0.648-0.191L18.871,5.91l0.033-0.783l-1.274-0.524l-0.423,1.841
l-1.284,0.291l0.127,1.027l1.673,0.322l0.289-1.641l1.381,0.204l0.642,0.376h1.03l0.705,1.412l1.869,1.896l-0.137,0.737
l-1.507-0.192l-2.604,1.315l-1.875,2.249l-0.244,0.996h-0.673l-1.254-0.578l-1.218,0.578l0.303,1.285l0.53-0.611l0.932-0.029
l-0.065,1.154l0.772,0.226l0.771,0.866l1.259-0.354l1.438,0.227l1.67,0.449l0.834,0.098l1.414,1.605l2.729,1.605l-1.765,3.372
l-1.863,0.866l-0.707,1.927l-2.696,1.8l-0.287,1.038c6.892-1.66,12.019-7.851,12.019-15.253
C31.413,12.474,30.433,9.465,28.755,6.968z" />
    <path
        android:fillColor="#fff"
        android:pathData="M17.515,23.917l-1.144-2.121l1.05-2.188l-1.05-0.314l-1.179-1.184l-2.612-0.586l-0.867-1.814v1.077h-0.382l-2.251-3.052
v-2.507L7.43,8.545L4.81,9.012H3.045L2.157,8.43L3.29,7.532L2.16,7.793c-1.362,2.326-2.156,5.025-2.156,7.916
c0,8.673,7.031,15.707,15.705,15.707c0.668,0,1.323-0.059,1.971-0.137l-0.164-1.903c0,0,0.721-2.826,0.721-2.922
C18.236,26.357,17.515,23.917,17.515,23.917z" />
    <path
        android:fillColor="#fff"
        android:pathData="M5.84,5.065l2.79-0.389l1.286-0.705l1.447,0.417l2.312-0.128l0.792-1.245l1.155,0.19l2.805-0.263L19.2,2.09l1.09-0.728
l1.542,0.232l0.562-0.085C20.363,0.553,18.103,0,15.708,0C10.833,0,6.474,2.222,3.596,5.711h0.008L5.84,5.065z
M16.372,1.562
l1.604-0.883l1.03,0.595l-1.491,1.135l-1.424,0.143l-0.641-0.416L16.372,1.562z
M11.621,1.691l0.708,0.295l0.927-0.295
l0.505,0.875l-2.14,0.562l-1.029-0.602C10.591,2.526,11.598,1.878,11.621,1.691z" />
</vector>

像那样


可能吗

用组标记包装路径标记。然后在组标记中添加属性scaleX、scaleY、pivotX和pivotY以创建填充效果。像这样:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:alpha="0.6"
        android:viewportHeight="24.0"
        android:viewportWidth="24.0">
    <group
        android:scaleX="0.5"
        android:scaleY="0.5"
        android:pivotX="12"
        android:pivotY="12">
        <path
            android:fillColor="#FF000000"
            android:pathData="M3,4L3,1h2v3h3v2L5,6v3L3,9L3,6L0,6L0,4h3zM6,10L6,7h3L9,4h7l1.83,2L21,6c1.1,0 2,0.9 2,2v12c0,1.1 -0.9,2 -2,2L5,22c-1.1,0 -2,-0.9 -2,-2L3,10h3zM13,19c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5 -5,2.24 -5,5 2.24,5 5,5zM9.8,14c0,1.77 1.43,3.2 3.2,3.2s3.2,-1.43 3.2,-3.2 -1.43,-3.2 -3.2,-3.2 -3.2,1.43 -3.2,3.2z"/>
    </group>

</vector>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="15dp" android:left="15dp" android:right="15dp" android:bottom="15dp"
        android:drawable="@drawable/your_image_or_icon"/>
</layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp">
    <vector
        android:width="40dp"
        android:height="40dp"
        android:viewportWidth="512"
        android:viewportHeight="512">
        <path
            android:fillColor="#4086f4"
            android:pathData="m451,135 l-105,-30 -30,-105h-210c-24.853,0 -45,20.147 -45,45v422c0,24.853 20.147,45 45,45h300c24.853,0 45,-20.147 45,-45z" />
        <path
            android:fillColor="#4175df"
            android:pathData="m451,135v332c0,24.853 -20.147,45 -45,45h-150v-512h60l30,105z" />
        <path
            android:fillColor="#80aef8"
            android:pathData="m451,135h-105c-16.5,0 -30,-13.5 -30,-30v-105c3.9,0 7.8,1.5 10.499,4.501l120,120c3.001,2.699 4.501,6.599 4.501,10.499z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,241h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,301h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,361h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m286,421h-120c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h120c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,421h30c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-30z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,361h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,301h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,241h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
    </vector>
</item>


*pivot值取决于向量的宽度和高度的大小好的
scaleX
ScaleY
工作正常,但我发现很难通过
android:pivotX
android:pivotY
对齐,所以我做了以下工作:

创建一个新的xml
drawable
文件,如下所示:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:alpha="0.6"
        android:viewportHeight="24.0"
        android:viewportWidth="24.0">
    <group
        android:scaleX="0.5"
        android:scaleY="0.5"
        android:pivotX="12"
        android:pivotY="12">
        <path
            android:fillColor="#FF000000"
            android:pathData="M3,4L3,1h2v3h3v2L5,6v3L3,9L3,6L0,6L0,4h3zM6,10L6,7h3L9,4h7l1.83,2L21,6c1.1,0 2,0.9 2,2v12c0,1.1 -0.9,2 -2,2L5,22c-1.1,0 -2,-0.9 -2,-2L3,10h3zM13,19c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5 -5,2.24 -5,5 2.24,5 5,5zM9.8,14c0,1.77 1.43,3.2 3.2,3.2s3.2,-1.43 3.2,-3.2 -1.43,-3.2 -3.2,-3.2 -3.2,1.43 -3.2,3.2z"/>
    </group>

</vector>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="15dp" android:left="15dp" android:right="15dp" android:bottom="15dp"
        android:drawable="@drawable/your_image_or_icon"/>
</layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp">
    <vector
        android:width="40dp"
        android:height="40dp"
        android:viewportWidth="512"
        android:viewportHeight="512">
        <path
            android:fillColor="#4086f4"
            android:pathData="m451,135 l-105,-30 -30,-105h-210c-24.853,0 -45,20.147 -45,45v422c0,24.853 20.147,45 45,45h300c24.853,0 45,-20.147 45,-45z" />
        <path
            android:fillColor="#4175df"
            android:pathData="m451,135v332c0,24.853 -20.147,45 -45,45h-150v-512h60l30,105z" />
        <path
            android:fillColor="#80aef8"
            android:pathData="m451,135h-105c-16.5,0 -30,-13.5 -30,-30v-105c3.9,0 7.8,1.5 10.499,4.501l120,120c3.001,2.699 4.501,6.599 4.501,10.499z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,241h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,301h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,361h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m286,421h-120c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h120c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,421h30c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-30z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,361h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,301h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,241h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
    </vector>
</item>

创建一个新的可绘制文件,添加图层列表,并将矢量图像包装到项目块中。 像这样:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:alpha="0.6"
        android:viewportHeight="24.0"
        android:viewportWidth="24.0">
    <group
        android:scaleX="0.5"
        android:scaleY="0.5"
        android:pivotX="12"
        android:pivotY="12">
        <path
            android:fillColor="#FF000000"
            android:pathData="M3,4L3,1h2v3h3v2L5,6v3L3,9L3,6L0,6L0,4h3zM6,10L6,7h3L9,4h7l1.83,2L21,6c1.1,0 2,0.9 2,2v12c0,1.1 -0.9,2 -2,2L5,22c-1.1,0 -2,-0.9 -2,-2L3,10h3zM13,19c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5 -5,2.24 -5,5 2.24,5 5,5zM9.8,14c0,1.77 1.43,3.2 3.2,3.2s3.2,-1.43 3.2,-3.2 -1.43,-3.2 -3.2,-3.2 -3.2,1.43 -3.2,3.2z"/>
    </group>

</vector>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="15dp" android:left="15dp" android:right="15dp" android:bottom="15dp"
        android:drawable="@drawable/your_image_or_icon"/>
</layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp">
    <vector
        android:width="40dp"
        android:height="40dp"
        android:viewportWidth="512"
        android:viewportHeight="512">
        <path
            android:fillColor="#4086f4"
            android:pathData="m451,135 l-105,-30 -30,-105h-210c-24.853,0 -45,20.147 -45,45v422c0,24.853 20.147,45 45,45h300c24.853,0 45,-20.147 45,-45z" />
        <path
            android:fillColor="#4175df"
            android:pathData="m451,135v332c0,24.853 -20.147,45 -45,45h-150v-512h60l30,105z" />
        <path
            android:fillColor="#80aef8"
            android:pathData="m451,135h-105c-16.5,0 -30,-13.5 -30,-30v-105c3.9,0 7.8,1.5 10.499,4.501l120,120c3.001,2.699 4.501,6.599 4.501,10.499z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,241h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,301h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m346,361h-180c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h180c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#fff5f5"
            android:pathData="m286,421h-120c-8.291,0 -15,-6.709 -15,-15s6.709,-15 15,-15h120c8.291,0 15,6.709 15,15s-6.709,15 -15,15z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,421h30c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-30z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,361h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,301h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
        <path
            android:fillColor="#e3e7ea"
            android:pathData="m256,241h90c8.291,0 15,-6.709 15,-15s-6.709,-15 -15,-15h-90z" />
    </vector>
</item>

插图
可从API级别21开始使用

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetTop="16dp"
    android:insetRight="16dp"
    android:insetBottom="16dp">

    <vector
        // ....... my vector
    </vector>

</inset>

不适合我,这里是我的vector drawable与vector drawable配合得很好。更多信息:
scaleX
scaleY
定义drawable的原始大小。例如,如果两者都设置为
0.85
,图像将缩小15%。但是,当您将其缩小时,它不会自动调整到中心位置,因此当您使用
pivotX
pivotY
在每个轴上添加偏移量时,只需添加一个偏移量,如果您希望在视口中使图像居中,只需将pivotX和pivotY值设置为viewportHeight和viewportWidth值的一半即可。因此,如果viewport为24.0,则将pivot设置为12.0以使图标居中。完美。感谢您的回答和其他有价值的评论。如果您将vector元素放在item元素中,则不需要新的drawable。对于API级别21以下的API,它引发了
Resources$NotFoundException
。对于API级别21以下的API,它引发了
Resources$NotFoundException