-->

How to outline a vector drawable?

2019-01-27 00:34发布

问题:

In my app I allow user to set background color and images. So to make sure that all necessary information and icons are visible, I use text with shadow of different color. It is easy to set it by setting shadow properties in xml.

However, I could not find a way to draw outline on vector drawable. I found similar questions however the answers are more or less drawing rectangular or square background with shadow. That makes it look too ugly in my case. I want to outline the shape instead of setting background to square with shaddow.

回答1:

I found a trick. Surprisingly it's very simple.

Answer 1. Make a copy of vector drawable - give it diff name. 2. Change the height and width in the xml

<vector **android:height="28dp" android:width="28dp"**
    android:viewportHeight="24.0" android:viewportWidth="24.0" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#56F0FF00" android:pathData="M1,21h4L5,9L1,9v12zM23,10c0,-1.1 -0.9,-2 -2,-2h-6.31l0.95,-4.57 0.03,-0.32c0,-0.41 -0.17,-0.79 -0.44,-1.06L14.17,1 7.59,7.59C7.22,7.95 7,8.45 7,9v10c0,1.1 0.9,2 2,2h9c0.83,0 1.54,-0.5 1.84,-1.22l3.02,-7.05c0.09,-0.23 0.14,-0.47 0.14,-0.73v-1.91l-0.01,-0.01L23,10z"/>
</vector>
  1. Now set this new drawable as background (foreground if the size of new drawable is smaller than the original). Make sure you use proper scaling so as to get outlined drawable.