本文介绍了你能把 VectorDrawable pathData 转换成 Path 对象吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 VectorDrawable 中提取 pathData 并将其转换为 Path 对象?

我想创建一个自定义 ViewOutlineProvider 并给它一个任意形状来剪辑和投射阴影.如果有办法直接使用 VectorDrawable 那就更好了.

谢谢,ndh

解决方案

Introduction

We need to:

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:

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.

这篇关于你能把 VectorDrawable pathData 转换成 Path 对象吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:05