Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

But they come with padding.

enter image description here

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.

like image 496
Guy Avatar asked Nov 11 '15 14:11

Guy


2 Answers

Android Vector Asset

You are able to scale a vector that will remove additional space. This is possible using group tag. Just modify your vector xml file.

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

enter image description here

like image 137
yoAlex5 Avatar answered Oct 14 '22 16:10

yoAlex5


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. You will also need to set android:adjustViewBounds="true".

For example, lets say your VectorDrawable has some really annoying padding at the start of the image when you display it. 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.

tl;dr

Adjust your ImageViews android:scaleType to handle any "implicit" padding that is contained in your VectorDrawables source file (.SVG, .PSD). Also set 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.

Note: Adjust the android:scaleType according to your rendering needs.

like image 45
Sakiboy Avatar answered Oct 14 '22 17:10

Sakiboy