安卓滑翔:如何下载和高速缓存的位图? [英] Android Glide: How to download and cache bitmaps?

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

问题描述

我使用滑翔下载并缓存图片在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 不叫的。

推荐答案

在滑行载荷不启动,直到你打电话到<一个href="http://bumptech.github.io/glide/javadocs/340/com/bumptech/glide/GenericRequestBuilder.html#into(Y)">into.所述RequestListener接口观察请求,但通常不意味着处理结果。而不是使用RequestListener的,考虑让你的回调实施<一个href="http://bumptech.github.io/glide/javadocs/340/com/bumptech/glide/request/target/Target.html">Target接口并通过它使用<一个href="http://bumptech.github.io/glide/javadocs/340/com/bumptech/glide/GenericRequestBuilder.html#into(Y)">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.

另外,您可以只延长<一href="http://bumptech.github.io/glide/javadocs/340/com/bumptech/glide/request/target/SimpleTarget.html">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);

您将要提供的宽度和高度,使滑翔可以降低采样率并适当地变换图像。您也可以运行中的问题与取消,如果你显示这些位图中的观点,在这种情况下,我强烈推荐使用视图以您加载API和传递视图<一个href="http://bumptech.github.io/glide/javadocs/340/com/bumptech/glide/DrawableRequestBuilder.html#into(android.widget.ImageView)">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.

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

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