Can you convert VectorDrawable pathData into a Path object

Viewed 4025

Is it possible to pull the pathData out of a VectorDrawable and convert it into a Path object?

I want to create a custom ViewOutlineProvider and give it an arbitrary shape to clip and cast shadows from. If there's a way to directly use a VectorDrawable that'd be even better.

thanks, ndh

1 Answers

"Android Can you convert VectorDrawable -> pathData into a android.graphics.Path".

Introduction

We need to:

There is VectorMaster in the github which can do all the work for you.

Just add the following dependency in your app's build.gradle (see example app included):

dependencies {
      compile 'com.sdsmdg.harjot:vectormaster:1.1.3'
}

Here is an Image of my test app which uses it: enter image description here

Setup

Here's a Vector to work with (pathData) ...res\drawable\ic_heart.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:name="outline"
        android:pathData="M20.84,4.61a5.5,5.5 0,0 0,-7.78 0L12,5.67l-1.06,-1.06a5.5,5.5 0,0 0,-7.78 7.78l1.06,1.06L12,21.23l7.78,-7.78 1.06,-1.06a5.5,5.5 0,0 0,0 -7.78z"
        android:strokeLineCap="round"
        android:strokeColor="#5D5D5D"
        android:fillColor="#00000000"
        android:strokeWidth="2"
        android:strokeLineJoin="round"/>
</vector>

Here is a VectorMasterView in our layout:

...res\layout\activity_main:

<com.sdsmdg.harjot.vectormaster.VectorMasterView
    android:id="@+id/heart_vector"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_margin="10dp"
    android:scaleX="0.8"
    android:scaleY="0.8"
    app:vector_src="@drawable/ic_heart" />

Code

Setup the Vector in onCreate:

//normal stuff
setContentView(R.layout.activity_main);

//Inflate the `Vector`
VectorMasterView vmHeartVector = (VectorMasterView) findViewById(R.id.heart_vector);

// find the correct path using name
PathModel outline = vmHeartVector.getPathModelByName("outline");

String pathData   = outline.getPathData();// this is our pathData
Path   path       = outline.getPath();    // this is our path object;

Some links: Vector drawables overview, Path, pathData, VectorDrawable, AnimatedVectorDrawable, VectorDrawableCompat, AnimatedVectorDrawableCompat.

Related