0

我的 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;
  }
4

1 回答 1

0

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

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;
}
于 2018-02-22T12:07:22.390 回答