Can you convert VectorDrawable pathData to Path object

Is it possible to pull pathData

from VectorDrawable

and convert it to an object Path

?

I want to create a custom one ViewOutlineProvider

and give it an arbitrary shape for a clip and drop shadows. If there is a way to directly use VectorDrawable

that would be even better.

thanks NDH

+3


source to share


1 answer


"Android You can convert pathData to android.graphics. ". VectorDrawable

Path

Introduction

We need:

There github

is a VectorMaster that can do all the work for you.

Just add the following dependency to your application build.gradle

(see sample application):

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

      

Here is a picture of my test app using it: enter image description here

Setting up



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 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

Install 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: , , pathData , , ,   , . Vector drawables overview

Path

VectorDrawable

AnimatedVectorDrawable

VectorDrawableCompat

AnimatedVectorDrawableCompat

0


source







All Articles