gpt4 book ai didi

java - Android:如何单独绘制.svg文件元素?

转载 作者:行者123 更新时间:2023-11-29 23:55:03 24 4
gpt4 key购买 nike

我有一个 .svg 文件,里面有一些元素。有什么方法可以一步步在 Canvas 上画出来吗?

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

最佳答案

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

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

是的,你可以这样做(至少对于重要的 Path 数据),参见 Android path tracing教程和源代码在github here .演示 (road-trip) 提取(实际上它拦截)Path's并为这些设置动画 android.graphics.Path's (使用 android.animation.ObjectAnimator)。 You Tube .

The library I picked, androidsvg, is easy to use but does not give access to the paths contained in the SVG document. I worked around this issue by creating a custom Canvas that intercepts drawPath() calls:

//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 format 是一个文档 文件, 只是一个简单的 vector 图像类型。您可能可以修改 viewBox使用上面的代码从您的 .SVG 文档中选择不同的图像。

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

关于java - Android:如何单独绘制.svg文件元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com