简体   繁体   English

Android Vector Asset Studio为一些矢量图像提供额外的填充

[英]Android Vector Asset Studio gives extra padding to some vector images

I'm trying to import some icons from Material Vector package in Vector Asset Studio. 我正在尝试从Vector Asset Studio中的Material Vector包导入一些图标。

But they come with padding. 但他们带有填充物。

在此输入图像描述

Why does this happen and how can I remove it? 为什么会发生这种情况,如何将其删除?

This is inconvenient because this means if I want my icon to be 17dp x 17dp in XML, then I need to set it more than 17x17 to make up for the padding. 这是不方便的,因为这意味着如果我希望我的图标是XML格式的17dp x 17dp,那么我需要设置它超过17x17以弥补填充。

You are able to scale a vector that will remove additional space. 您可以缩放一个将删除额外空间的向量。 This is possible using group tag. 这可以使用group标记。 Just modify your vector xml file. 只需修改vector xml文件即可。

From

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

    <path
        android:fillColor="#FF000000"
        android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z" />
</vector>

to

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

    <group
        android:pivotX="12"
        android:pivotY="12"
        android:scaleX="1.5"
        android:scaleY="1.5">

        <path
            android:fillColor="#FF000000"
            android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z" />
    </group>
</vector>

As result 结果

在此输入图像描述

You can adjust for any "implicit" padding that may be contained within a VectorDrawables source image ( .SVG , .PSD ) by setting your ImageViews android:scaleType to the appropriate value so it can handle the padding that is secretly contained in the VectorDrawables source image. 您可以通过将ImageViews android:scaleType设置为适当的值来调整VectorDrawables源图像( .SVG.PSD )中可能包含的任何“隐式”填充,以便它可以处理VectorDrawables源中秘密包含的填充图片。 You will also need to set android:adjustViewBounds="true" . 你还需要设置android:adjustViewBounds="true"

For example, lets say your VectorDrawable has some really annoying padding at the start of the image when you display it. 例如,假设您的VectorDrawable在显示时在图像的开头有一些非常烦人的padding You have no idea why it's there because you aren't setting any android:paddingStart on the ImageView ... what you need to do is set the ImageViews android:scaleType to fitStart and android:adjustViewBounds to true . 你不知道它为什么会在那里因为你没有在ImageView上设置任何android:paddingStart ...你需要做的是将ImageViews android:scaleTypefitStart并将android:adjustViewBoundstrue

tl;dr TL;博士

Adjust your ImageViews android:scaleType to handle any "implicit" padding that is contained in your VectorDrawable s source file ( .SVG , .PSD ). 调整ImageViews android:scaleType以处理VectorDrawable源文件( .SVG.PSD )中包含的任何“隐式”填充。 Also set android:adjustViewBounds="true" . 还设置android:adjustViewBounds="true"

Quick Example: 快速示例:

<ImageView android:id="@+id/vectorDrawable_imageView"
           <!--Other ImageView settings-->
           android:adjustViewBounds="true"
           android:scaleType="fitStart"
           app:srcCompat="@drawable/vector_with_implicit_padding_at_start"
/>

This will remove that annoying "implicit" padding that was at the start of your VectorDrawable . 这将删除VectorDrawable开头那个烦人的“隐式”填充。

Note: Adjust the android:scaleType according to your rendering needs. 注意:根据渲染需要调整android:scaleType

This padding is on some icons so that all of the icons can align properly. 此填充位于某些图标上,因此所有图标都可以正确对齐。 For example, if in that dialog, you pick ic_3d_rotation_24dp , you'll see the icon goes all the way to the edge of the bounds. 例如,如果在该对话框中,您选择ic_3d_rotation_24dp ,您将看到图标一直到边界的边缘。

PS if you aim to have all your sizes be a multiple of 8dp , things will line up nicely and look great. PS如果你的目标是让你的所有尺寸都是8dp的倍数,那么事情会顺利排列并且看起来很棒。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="28"
    android:viewportHeight="28">
    <group
        android:translateX="2"
        android:translateY="2">
        <path
            android:fillColor="#8A333333"
            android:pathData="M13.12,2.06L7.58,7.6c-0.37,0.37 -0.58,0.88 -0.58,1.41V19c0,1.1 0.9,2 2,2h9c0.8,0 1.52,-0.48 1.84,-1.21l3.26,-7.61C23.94,10.2 22.49,8 20.34,8h-5.65l0.95,-4.58c0.1,-0.5 -0.05,-1.01 -0.41,-1.37 -0.59,-0.58 -1.53,-0.58 -2.11,0.01zM3,21c1.1,0 2,-0.9 2,-2v-8c0,-1.1 -0.9,-2 -2,-2s-2,0.9 -2,2v8c0,1.1 0.9,2 2,2z" />
    </group>
</vector>

android:viewportWidth += android:translateX * 2 (padding start / end) android:viewportWidth + = android:translateX * 2(填充开始/结束)

android:viewportHeight += android:translateY * 2 (padding top / bottom) android:viewportHeight + = android:translateY * 2(填充顶部/底部)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM