[英]Image size too small when loading SVG with Glide
我正在使用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
的固定宽度为102dp
,XML 的固定高度为94dp
。但是图像在加载后变得比应有的小。 难道我做错了什么?
scaleType 是: android:scaleType="fitXY"
我决定在 libs 存储库上将此问题作为问题打开,然后我就能够解决该问题。
事实证明,问题与我的 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);
}
}
现在它可以正常工作。
除了接受的答案 - 对我来说解决方案不起作用,这背后的原因是 svg 中没有合适的视图框
添加
if (svg.documentViewBox == null)
svg.setDocumentViewBox(0f, 0f, svg.documentWidth, svg.documentHeight)
在最终更改 svg 宽度/高度固定缩放之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.