你能把 VectorDrawable pathData 转换成 Path 对象吗 [英] Can you convert VectorDrawable pathData into a Path object

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

问题描述

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

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

谢谢,ndh

解决方案

"Android 能否转换

设置

这是一个与 (pathData) 一起使用的 Vector ...res\drawable\ic_heart.xml:

<路径机器人:名称=大纲"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"机器人:strokeLineCap =圆形"android:strokeColor="#5D5D5D"android:fillColor="#00000000"机器人:strokeWidth =2"android:strokeLineJoin="round"/></向量>

这是我们布局中的一个 VectorMasterView:

...res\layout\activity_main:

代码

onCreate中设置Vector:

//普通东西setContentView(R.layout.activity_main);//膨胀`Vector`VectorMasterView vmHeartVector = (VectorMasterView) findViewById(R.id.heart_vector);//使用名称找到正确的路径PathModel 大纲 = vmHeartVector.getPathModelByName("outline");String pathData = outline.getPathData();//这是我们的pathData路径路径 = outline.getPath();//这是我们的路径对象;

一些链接:矢量可绘制概览Path,pathData,VectorDrawable,AnimatedVectorDrawable,VectorDrawableCompat,AnimatedVectorDrawableCompat.

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

解决方案

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

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 对象吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆