Glide是否有加载PNG和SVG的方法? [英] Does Glide have a method for loading both PNG and SVG?

查看:622
本文介绍了Glide是否有加载PNG和SVG的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Glide 异步图像加载到某些图像中我的ImageView s,我知道它可以处理PNGJPG之类的图像,因为它可以处理SVG.

I'm using Glide to load some images asynchronously into some of my ImageViews, 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:

正在加载正常"图像

Glide.with(mContext)
                .load("URL")
                .into(cardHolder.iv_card);

正在加载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);

如果我尝试使用第一种方法加载SVG,它将无法正常工作.如果我尝试使用第二种方法加载PNG或JPG,则也将无法正常工作.

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.

是否有使用Glide加载两种图像类型的通用方法?

我从中获取这些图像的服务器在下载图像之前没有告诉我图像类型.它是REST服务器,将以类似"http://foo.bar/resource"的方式检索资源.知道图像类型的唯一方法是读取HEAD响应.

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.

推荐答案

您可以使用 Glide & AndroidSVG 一起实现您的目标.

You can use Glide & AndroidSVG together to achieve your goal.

有来自Glide的SVG示例.示例示例

There is sample from Glide for SVG.Sample Example

设置RequestBuilder

Setup RequestBuilder

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一起使用RequestBuilder

Use RequestBuilder with 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的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆