你能把 VectorDrawable pathData 转换成 Path 对象吗

Nig*_* DH 10 android vector

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

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

谢谢,ndh

Jon*_*win 7

“Android 能否将-> pathData转换为 android.graphics。”。VectorDrawable Path

介绍

我们需要:

VectorMastergithub其中可以做所有的工作 适合你。

只需在您的应用程序中添加以下依赖项build.gradle(请参阅包含的示例应用程序):

dependencies {
      compile 'com.sdsmdg.harjot:vectormaster:1.1.3'
}
Run Code Online (Sandbox Code Playgroud)

这是使用它的测试应用程序的图像: 在此处输入图片说明

设置

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

...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" />
Run Code Online (Sandbox Code Playgroud)

代码

安装程序VectoronCreate

//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;
Run Code Online (Sandbox Code Playgroud)

一些链接: Vector drawables overview, Path, pathData , VectorDrawable, AnimatedVectorDrawable, VectorDrawableCompat, AnimatedVectorDrawableCompat