在项目implementation 'com.github.bumptech.glide:glide:4.8.0'
中使用
从服务器链接(.svg)获取
如何下载给定URL的.svg
图标,并使其在ImageView中滑动显示?
现在我用它像这样:
Glide.with(bankLogoView.context)
.`as`(PictureDrawable::class.java)
.load(data.logoUrl)
.listener(SvgSoftwareLayerSetter())
.apply(options)
.into(bankLogoView)
哪里有:
private val options = RequestOptions().centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_logo_splash)
还添加了下一个课程SvgDecoder
:
class SvgDecoder : ResourceDecoder<InputStream, SVG> {
override fun handles(source: InputStream, options: Options): Boolean {
// TODO: Can we tell?
return true
}
@Throws(IOException::class)
override fun decode(source: InputStream, width: Int, height: Int,
options: Options): Resource<SVG>? {
try {
val svg = SVG.getFromInputStream(source)
return SimpleResource(svg)
} catch (ex: SVGParseException) {
throw IOException("Cannot load SVG from stream", ex)
}
}
}
SvgDrawableTranscoder:
class SvgDrawableTranscoder : ResourceTranscoder<SVG, PictureDrawable> {
override fun transcode(toTranscode: Resource<SVG>,
options: Options): Resource<PictureDrawable>? {
val svg = toTranscode.get()
val picture = svg.renderToPicture()
val drawable = PictureDrawable(picture)
return SimpleResource(drawable)
}
}
SvgModule:
@GlideModule
class SvgModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide,
registry: Registry) {
registry.register(SVG::class.java, PictureDrawable::class.java, SvgDrawableTranscoder())
.append(InputStream::class.java, SVG::class.java, SvgDecoder())
}
// Disable manifest parsing to avoid adding similar modules twice.
override fun isManifestParsingEnabled(): Boolean {
return false
}
}
SvgSoftwareLayerSetter:
class SvgSoftwareLayerSetter : RequestListener<PictureDrawable> {
override fun onLoadFailed(e: GlideException?, model: Any, target: Target<PictureDrawable>,
isFirstResource: Boolean): Boolean {
val view = (target as ImageViewTarget<*>).view
view.setLayerType(ImageView.LAYER_TYPE_NONE, null)
return false
}
override fun onResourceReady(resource: PictureDrawable, model: Any,
target: Target<PictureDrawable>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
val view = (target as ImageViewTarget<*>).view
view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null)
return false
}
}
但是我也只显示选项。 data.logoUrl
不为空,但不显示
UPD:
滑行:https://mandarine.com/logos/providers/xf/fake_demobank_xf.svg的加载失败,大小为[65x66] com.bumptech.glide.load.engine.GlideException类:无法加载资源
当我.load("url")
时,试图对Google的一些网址进行硬编码:
https://upload.wikimedia.org/wikipedia/sco/7/71/Pringles.svg
但也没有显示图像。
我尝试做的第二种方式: 实施此library
implementation 'com.caverock:androidsvg-aar:1.3'
然后修改了我的布局:
<com.caverock.androidsvg.SVGImageView
android:id="@+id/bankLogoView"
style="@style/icon_account_view"
app:svg="@drawable/ic_buy_violet"/>
然后在我的持有人身上这样:
bankLogoView.setImageAsset("wallet.svg")
但是我如何从服务器下载.svg文件?
答案 0 :(得分:4)
我的代码是Java语言,但希望您能得到备忘录。将以下依赖项添加到您的应用模块build.gradle文件中:
implementation 'com.caverock:androidsvg:1.2.1'
在您的GlideModule
中:
public class GlideModule extends AppGlideModule {
...
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder()).append(InputStream.class, SVG.class, new SvgDecoder());
}
}
SvgDecoder.java
public class SvgDecoder implements ResourceDecoder<InputStream, SVG> {
@Override
public boolean handles(InputStream source, Options options) throws IOException {
// TODO: Can we tell?
return true;
}
public Resource<SVG> decode(InputStream source, int width, int height, Options options)
throws IOException {
try {
SVG svg = SVG.getFromInputStream(source);
return new SimpleResource<SVG>(svg);
} catch (SVGParseException ex) {
throw new IOException("Cannot load SVG from stream", ex);
}
}
}
SvgDrawableTranscoder.java
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode, Options options) {
SVG svg = toTranscode.get();
Picture picture = svg.renderToPicture();
PictureDrawable drawable = new PictureDrawable(picture);
return new SimpleResource<PictureDrawable>(drawable);
}
}
SvgSoftwareLayerSetter.java
public class SvgSoftwareLayerSetter implements RequestListener<PictureDrawable> {
@Override
public boolean onLoadFailed(GlideException e, Object model, Target<PictureDrawable> target,
boolean isFirstResource) {
ImageView view = ((ImageViewTarget<?>) target).getView();
view.setLayerType(ImageView.LAYER_TYPE_NONE, null);
return false;
}
@Override
public boolean onResourceReady(PictureDrawable resource, Object model,
Target<PictureDrawable> target, DataSource dataSource, boolean isFirstResource) {
ImageView view = ((ImageViewTarget<?>) target).getView();
view.setLayerType(ImageView.LAYER_TYPE_SOFTWARE, null);
return false;
}
}
然后将其与Glide一起使用:
Glide.with(this)
.as(PictureDrawable.class)
.load(svgUrl)
.listener(new SvgSoftwareLayerSetter())
.into(imageView);
答案 1 :(得分:1)
做到了。从Glide v4开始,不必为每个请求都创建解码器和代码转换器,最好在GlideModule中设置一次。 Glide通过默认的ModelLoader之一处理InputStream的URL。
@GlideModule
class GlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.append( InputStream::class.java, SVG::class.java, SvgDecoder() )
registry.register( SVG::class.java, PictureDrawable::class.java, SvgDrawableTranscoder() )
}
}
class SvgDecoder : ResourceDecoder<InputStream, SVG> {
override fun handles(source: InputStream, options: Options): Boolean {
return true
}
@Throws(IOException::class)
override fun decode(source: InputStream, width: Int, height: Int, options: Options): Resource<SVG> {
try {
val svg = SVG.getFromInputStream(source)
return SimpleResource(svg)
} catch (ex: SVGParseException) {
throw IOException("Cannot load SVG from stream", ex)
}
}
}
class SvgDrawableTranscoder : ResourceTranscoder<SVG, PictureDrawable> {
override fun transcode(toTranscode: Resource<SVG>, options: Options): Resource<PictureDrawable> {
val svg = toTranscode.get()
val picture = svg.renderToPicture()
val drawable = PictureDrawable(picture)
return SimpleResource(drawable)
}
}
GlideApp.with(this)
.load("https://sample.svg" )
.into(imageView)