I'm trying to import some icons from Material Vector package in Vector Asset Studio.
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.
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
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 VectorDrawable
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With