本文介绍了Android:如何分别绘制.svg文件的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .svg 文件,其中包含一些元素.有什么办法可以在画布上逐步绘制它们?

I have .svg a file with some elements in it. Is there any way to draw them step by step on the canvas?

我正在使用 com.caverock:androidsvg 库,但似乎无法提取单个元素.

I'm using com.caverock:androidsvg library, but it seems like there is no way to extract individual element.

推荐答案

这是我的测试示例视频.我从250复制了一个 Path (图像中间的白色轮廓,不会平移或滚动).这是 SVG 文档中的第一个 Path ,也是 Path的 List 中的第一个元素' s

Here is my test example video. I copied one Path from 250 (the white outline in the middle of the image, that does not pan or scroll). It was the first Path in the SVG document, and the first element in the List of Path's

是的,您可以执行此操作(至少对于重要的 Path 数据),请参见 Android路径跟踪教程,以及 github 此处中的源代码.该演示( road-trip )提取(实际上是 intercepts )路径,并对它们进行动画处理 android.graphics.Path的 (带有 android.animation.ObjectAnimator ). You Tube .

Yes you can do this (at least for the important Path data), see Android path tracing tutorial, and the source code in the github here. The demo (road-trip) extracts (actually it intercepts) the Path's and also animates these android.graphics.Path's (with android.animation.ObjectAnimator). You Tube.

//list of Path's
private final List<VecPath> mPaths = new ArrayList<VecPath>();

Canvas canvas = new Canvas() {
    private final Matrix mMatrix = new Matrix();

    @Override
    public void drawPath(Path path, Paint paint) {
        Path dst = new Path();

        // Get the current transform matrix
        getMatrix(mMatrix);
        // Apply the matrix to the path
        path.transform(mMatrix, dst);
        // Store the transformed path
        mPaths.add(new SvgPath(dst, new Paint(mSourcePaint)));
    }
};

// Load an SVG document
SVG svg = SVG.getFromResource(context, R.raw.map_usa);
// Capture the paths
svg.renderToCanvas(canvas);

注意

请记住 .SVG 格式是文档文件,不是只是一种简单的矢量图像类型.您可能可以修改 viewBox ,使用上述代码从 .SVG 文档中选择不同的图像.

Note

Remember the .SVG format is a document file, not just a simple vector image type. You possibly could modify the viewBox to select different images from your .SVG document, using the code above.

<svg
   width="650"
   height="1000"
   viewBox="40 350 900 1050"
   id="svg1">

这篇关于Android:如何分别绘制.svg文件的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:44