Android:如何分别绘制.svg文件的元素? [英] Android: how to draw .svg file's elements separetly?
问题描述
我有一个 .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的
s List
中的第一个元素'
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
我有
.svg
文件,其中包含一些元素.有没有办法绘制他们一步一步在画布上?"
"I have
.svg
file with some elements in it. Is there any way to draw them step by step on the canvas?"
是的,您可以执行此操作(至少对于重要的 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.
我选择的库
androidsvg
易于使用,但不能提供访问SVG
文档中包含的路径.我工作了通过创建可拦截drawPath()
的自定义Canvas来解决此问题通话:
The library I picked,
androidsvg
, is easy to use but does not give access to the paths contained in theSVG
document. I worked around this issue by creating a custom Canvas that interceptsdrawPath()
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
格式是文档文件,不是只是一种简单的矢量图像类型.您可能可以修改 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文件的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!