【问题标题】:Download SVG and convert to a Bitmap or Drawable下载 SVG 并转换为 Bitmap 或 Drawable
【发布时间】:2018-08-01 23:37:39
【问题描述】:

我的 API 正在返回一个 SVG,我想将这个 SVG 转换为位图,这样我就可以将它用作我的谷歌地图片段上的 Pin。

我发现 https://github.com/japgolly/svg-android 一旦我将 Jar 添加到我的应用程序中,我就开始遇到与字体相关的奇怪运行时错误。显然它已经过时了,没有任何用处。

我研究了 Glide,因为有些人认为它可以与 SVG 一起使用。

我什至没有数据类型来读取它,而且真的没有办法将它转换为可用的格式。

我想要做的就是把这个 responseBody.byteStream() 变成一个位图。也就是说,Java 解决方案也必须存在。

 public Observable<Bitmap> fetchBitmap(String url) {
    Observable<Bitmap> bitmapObservable = mGenericApiService.getBitmap(url)
        .map(responseBody -> {
          Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
          Bitmap bitmap = Bitmap.createBitmap(50, 50, conf);
          //******** CODE HERE?? ********
          return bitmap;
        }).doOnError(getUniversalErrorHandler(mContext, mEventBus));
    return bitmapObservable;
  }

【问题讨论】:

标签: java android svg android-graphics androidsvg


【解决方案1】:

您缺少的是以下内容(未经测试):

public Observable<Bitmap> fetchBitmap(String url) {
    Observable<Bitmap> bitmapObservable = mGenericApiService.getBitmap(url)
        .map(responseBody -> {
          Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
          Bitmap bitmap = Bitmap.createBitmap(50, 50, conf);

          // Get a Canvas for the Bitmap
          Canvas  canvas = new Canvas(bitmap);

          // Read the SVG file
          SVG svg = SVGParser.getSVGFromInputStream(inputstream);
          // There are other ways to read an SVG file. See the SVGParser class for the others.

          // Get a Picture from the SVG and render it to the Canvas
          canvas.drawPicture(SVG.getPicture());

          return bitmap;
        }).doOnError(getUniversalErrorHandler(mContext, mEventBus));
    return bitmapObservable;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多