Android Glide:如何下载和缓存位图? [英] Android Glide: How to download and cache bitmaps?

查看:40
本文介绍了Android Glide:如何下载和缓存位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Glide 在 Android 上下载和缓存图像.除了我不想将位图直接加载到 ImageView 之外,一切都运行良好,我不想有淡入淡出的动画,也不想有图像占位符.

I am using Glide to download and cache images on Android. Everything works well except the fact that I don't want to load the bitmaps directly into the ImageView, I don't want to have the fade animation, neither image placeholders.

我想要的只是创建一个全局方法来帮助我在整个应用程序中下载图像.

All I want is to create a global method which will help me to download the image all over the application.

public class MyApp extends Application {

   public static void downloadImage(String url, final OnImageLoadedCallback callback) {

     // And how to implement the listener ?

     RequestListener<String, Bitmap> requestListener = new RequestListener<String, Bitmap() {
        @Override
        public boolean onException(Exception exc, String string, Target<Bitmap> target, boolean isFirstResource) {

           callback.onDone(null);               

           return false;
        }

        @Override
        public boolean onResourceReady(Bitmap bitmap, String string, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {

            callback.onDone(bitmap);

            return false;
        }
     };

      Glide.with(context)
           .load(url)
           .asBitmap()
           .dontAnimate()
           .diskCacheStrategy(DiskCacheStrategy.SOURCE)
           .listener(requestListener);
   }

}

问题是我不知道如何实现监听器.RequestListener 根本没有被调用.

The problem is that I don't know how to implement the listener. RequestListener isn't called at all.

推荐答案

在调用 进入.RequestListener 接口观察请求,但通常不用于处理结果.不要使用RequestListener,考虑让你的回调实现Target 接口并使用 into 传入.

Loads in Glide don't start until you make a call to into. The RequestListener interface observes requests, but isn't typically meant for handling results. Instead of using RequestListener, consider having your callback implement the Target interface and pass it in using into.

或者,您可以扩展 SimpleTarget 和以与您尝试使用 RequestListener 相同的方式将其传递给每个请求:

Alternatively you could just extend SimpleTarget and pass it in to each request in the same way you're trying to use RequestListener:

Target target = Glide.with(context)
     ...
     .into(new SimpleTarget<Bitmap>(width, height) {
          @Override
          public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
              callback.onDone(resource);
          }

          @Override
          public void onLoadFailed(Exception e, Drawable errorDrawable) {
              callback.onDone(null);
          }
     });

// At some point later, if you want to cancel the load:
Glide.clear(target);

您需要提供宽度和高度,以便 Glide 可以适当地对图像进行下采样和变换.如果您在视图中显示这些位图,您也可能会遇到取消问题,在这种情况下,我强烈建议您将视图提供给加载 API 并将视图传递给 into 将为您处理大小和取消.

You will want to provide a width and a height so that Glide can downsample and transform images appropriately. You may also run in to issues with cancellation if you're displaying these Bitmaps in views, in which case I'd strongly recommend making the view available to your loading API and passing the view to into which will handle sizing and cancellation for you.

这篇关于Android Glide:如何下载和缓存位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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