使用SkiaSharp在Xamarin中加载SVG文件 [英] Load SVG file in Xamarin with SkiaSharp

查看:593
本文介绍了使用SkiaSharp在Xamarin中加载SVG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从1.55.0版开始,SkiaSharp支持读取SVG文件. 该软件包已于几天前(2016年11月10日)发布,我找不到足够的文档来了解如何使用它.

From release 1.55.0 SkiaSharp has the support for reading SVG files. The package has been release few days ago (10 Nov. 2016) and I couldn't find enough documentation on how to use it.

以下软件包是必需的: SkiaSharp 1.55.0 SkiaSharp Views& 1.55.0层 SkiaSharp.Svg 1.55.0-beta1

The following packages are required: SkiaSharp 1.55.0 SkiaSharp Views & Layers 1.55.0 SkiaSharp.Svg 1.55.0-beta1

第一个问题是在Xamarin.Android中加载SKSvg的最佳方法是什么?

The first question is what's the best way to load an SKSvg in Xamarin.Android?

推荐答案

以下两种开始与SkiaSharp合作的可能对我有用的解决方案:

Here two possible solutions to start working with SkiaSharp that are working for me:

从Asset文件夹(或子文件夹)加载SVG:

public SKSvg LoadAnSvgFromAssets(Context ctx, string assetSvgFileLoc)
    {
        var assets = ctx.Assets;
        var svg = new SKSvg();
        using (var stream = new StreamReader(assets.Open(assetSvgFileLoc)))
            {
              svg.Load(stream.BaseStream);
              return svg;
            }
    }

其中"assetSvgFileLoc"是要加载的svgFilename.svg,包括(如果是)包括Asset文件夹内的路径(例如"subf1/subf2/mysvg.svg").

where "assetSvgFileLoc" is the svgFilename.svg to load, including (if it's the case) the path inside Asset folder (e.g. "subf1/subf2/mysvg.svg").

将SVG加载为RAW资源

public SKSvg LoadAnSvgFromResources(Context ctx, string svgName))
  {
    var resId = ctx.Resources.GetIdentifier(svgName, "raw", ctx.PackageName);           
    var svg = new SKSvg();
    using (var stream = ctx.Resources.OpenRawResource(resId))
    {
        svg.Load(stream);
        return svg;
    }
}

在这种情况下,文件位于Resources子文件夹"raw"内,而"svgName"是我们svg的文件名,不带扩展名.

In this case the file is inside the Resources subfolder "raw" and the "svgName" is the filename of our svg without extension.

这篇关于使用SkiaSharp在Xamarin中加载SVG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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