【问题标题】:Image size too small when loading SVG with Glide使用 Glide 加载 SVG 时图像尺寸太小
【发布时间】:2016-06-01 11:29:40
【问题描述】:

我正在使用Glide 来加载图片。

在我的应用程序中,我使用以下示例将 SVG 图像加载到我的 ImageView 中,该图像位于 CardView 内。

GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder;

requestBuilder = Glide.with(mContext)
        .using(Glide.buildStreamModelLoader(Uri.class, mContext), InputStream.class)
        .from(Uri.class)
        .as(SVG.class)
        .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
        .sourceEncoder(new StreamEncoder())
        .cacheDecoder(new FileToStreamDecoder<>(new SVGDecoder()))
        .decoder(new SVGDecoder())
        .placeholder(R.drawable.modulo)
        .error(R.drawable.banner_error)
        .animate(android.R.anim.fade_in)
        .listener(new SvgSoftwareLayerSetter<Uri>());

requestBuilder
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .load(Uri.parse("http://foo.bar/blah"))
        .into(cardHolder.iv_card);

ImageView 在 XML 中具有固定宽度 102dp 和固定高度 94dp。但是图像在加载后变得比它们应该的要小。我做错了吗?

scaleType 是:android:scaleType="fitXY"

【问题讨论】:

  • 你是否在 ImageView 上设置了 scaleType 属性?
  • 是的,它是android:scaleType="fitXY"
  • 我不确定,但这可能与this AndroidSVG issue 有关。我还没有时间调查那个错误报告。只是fitXY模式会发生这种情况吗?

标签: java android svg android-glide


【解决方案1】:

我决定以an issue on the libs repository 的身份提出这个问题,然后我就能解决这个问题。

事实证明,问题与我的 SVG 具有固定大小有关,因此我必须修改我的 SvgDecoder.decode 方法,并添加这三行:

svg.setDocumentWidth(width);
svg.setDocumentHeight(height);
svg.setDocumentPreserveAspectRatio(PreserveAspectRatio.STRETCH);

该方法现在看起来像这样:

public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);

        svg.setDocumentWidth(width);
        svg.setDocumentHeight(height);
        svg.setDocumentPreserveAspectRatio(PreserveAspectRatio.STRETCH);

        return new SimpleResource<>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream.", ex);
    }
}

现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 2018-12-07
    • 1970-01-01
    • 2018-03-22
    • 2019-03-01
    • 2020-06-05
    • 1970-01-01
    • 2018-11-09
    • 2021-08-17
    相关资源
    最近更新 更多