问题描述
我正在使用 Glide 将一些图像异步加载到一些我的 ImageView
s,我知道它可以处理像 PNG
或 JPG
这样的图像,因为它可以处理 SVG
.
事情是,据我所知,我加载这两种图像的方式不同.喜欢:
加载正常"图像
Glide.with(mContext).load("URL").into(cardHolder.iv_card);
加载 SVG
GenericRequestBuilderrequestBuilder = 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(新的SVGDecoder()).listener(new SvgSoftwareLayerSetter());请求构建器.diskCacheStrategy(DiskCacheStrategy.NONE).load(Uri.parse("URL")).into(cardHolder.iv_card);
如果我尝试使用第一种方法加载 SVG,它将无法正常工作.如果我尝试使用第二种方法加载 PNG 或 JPG,它也不起作用.
是否有一种通用的方法可以使用 Glide 加载两种图像类型?
我从中获取这些图像的服务器在我下载之前不会告诉我图像类型.它是一个 REST 服务器,资源将以类似于 "http://foo.bar/resource"
的方式检索.了解图像类型的唯一方法是读取 HEAD 响应.
您可以使用 Glide &AndroidSVG 共同实现您的目标.
有来自 Glide 的 SVG 示例.示例示例
设置请求构建器
requestBuilder = Glide.with(mActivity).using(Glide.buildStreamModelLoader(Uri.class, mActivity), 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.ic_facebook).错误(R.drawable.ic_web).animate(android.R.anim.fade_in).listener(new SvgSoftwareLayerSetter());
使用带有 uri 的 RequestBuilder
Uri uri = Uri.parse("http://upload.wikimedia.org/wikipedia/commons/e/e8/Svg_example3.svg");请求构建器.diskCacheStrategy(DiskCacheStrategy.SOURCE)//SVG 无法序列化,因此不值得缓存它.load(uri).into(mImageView);
这样你就可以实现你的目标.我希望这会有所帮助.
I'm using Glide to load some images asynchronously into some of my ImageView
s, and I know it can handle images like PNG
or JPG
as it can handle SVG
.
Thing is, As far as I know, the way I load those two kinds of image differs. Like:
Loading "normal" images
Glide.with(mContext)
.load("URL")
.into(cardHolder.iv_card);
Loading SVG
GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> 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())
.listener(new SvgSoftwareLayerSetter<Uri>());
requestBuilder
.diskCacheStrategy(DiskCacheStrategy.NONE)
.load(Uri.parse("URL"))
.into(cardHolder.iv_card);
And if I try to load SVG with the first method it won't work. If I try to load PNG or JPG with the second method, it won't work either.
Is there a generic way to load both image types using Glide?
The server where I'm fetching those images from don't tell me the image type before I download it. It's a REST server and the resource will be retrieved in a way like "http://foo.bar/resource"
. The only way to know the image type is reading the HEAD response.
You can use Glide & AndroidSVG together to achieve your goal.
There is sample from Glide for SVG.Sample Example
requestBuilder = Glide.with(mActivity)
.using(Glide.buildStreamModelLoader(Uri.class, mActivity), InputStream.class)
.from(Uri.class)
.as(SVG.class)
.transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
.sourceEncoder(new StreamEncoder())
.cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
.decoder(new SvgDecoder())
.placeholder(R.drawable.ic_facebook)
.error(R.drawable.ic_web)
.animate(android.R.anim.fade_in)
.listener(new SvgSoftwareLayerSetter<Uri>());
Uri uri = Uri.parse("http://upload.wikimedia.org/wikipedia/commons/e/e8/Svg_example3.svg");
requestBuilder
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
// SVG cannot be serialized so it's not worth to cache it
.load(uri)
.into(mImageView);
This way you can achieve your goal. I hope this is helpful.
这篇关于Glide 有同时加载 PNG 和 SVG 的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!