|
| 1 | +package com.play.library_base.imageloader; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.drawable.Drawable; |
| 6 | +import android.net.Uri; |
| 7 | +import android.widget.ImageView; |
| 8 | + |
| 9 | +import androidx.annotation.DrawableRes; |
| 10 | + |
| 11 | +import com.bumptech.glide.Glide; |
| 12 | +import com.bumptech.glide.GlideBuilder; |
| 13 | +import com.bumptech.glide.MemoryCategory; |
| 14 | +import com.bumptech.glide.RequestBuilder; |
| 15 | +import com.bumptech.glide.load.Transformation; |
| 16 | +import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory; |
| 17 | +import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory; |
| 18 | +import com.bumptech.glide.request.target.DrawableImageViewTarget; |
| 19 | +import com.bumptech.glide.request.transition.Transition; |
| 20 | +import com.play.library_base.imageloader.progress.OnProgressListener; |
| 21 | +import com.play.library_base.imageloader.progress.ProgressManager; |
| 22 | + |
| 23 | +import java.lang.ref.WeakReference; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author by sunfusheng on 2017/6/6. |
| 27 | + */ |
| 28 | +public class GlideImageLoader { |
| 29 | + |
| 30 | + protected static final String ANDROID_RESOURCE = "android.resource://"; |
| 31 | + protected static final String FILE = "file://"; |
| 32 | + protected static final String SEPARATOR = "/"; |
| 33 | + |
| 34 | + private String url; |
| 35 | + private WeakReference<ImageView> imageViewWeakReference; |
| 36 | + private RequestBuilder<Drawable> glideRequest; |
| 37 | + |
| 38 | + public static GlideImageLoader create(ImageView imageView) { |
| 39 | + return new GlideImageLoader(imageView); |
| 40 | + } |
| 41 | + |
| 42 | + private GlideImageLoader(ImageView imageView) { |
| 43 | + imageViewWeakReference = new WeakReference<>(imageView); |
| 44 | + glideRequest = Glide.with(getContext()).asDrawable(); |
| 45 | + } |
| 46 | + |
| 47 | + public ImageView getImageView() { |
| 48 | + if (imageViewWeakReference != null) { |
| 49 | + return imageViewWeakReference.get(); |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + public Context getContext() { |
| 55 | + if (getImageView() != null) { |
| 56 | + return getImageView().getContext(); |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + public String getUrl() { |
| 62 | + return url; |
| 63 | + } |
| 64 | + |
| 65 | + public RequestBuilder getGlideRequest() { |
| 66 | + if (glideRequest == null) { |
| 67 | + glideRequest = Glide.with(getContext()).asDrawable(); |
| 68 | + } |
| 69 | + return glideRequest; |
| 70 | + } |
| 71 | + |
| 72 | + protected Uri resId2Uri(@DrawableRes int resId) { |
| 73 | + return Uri.parse(ANDROID_RESOURCE + getContext().getPackageName() + SEPARATOR + resId); |
| 74 | + } |
| 75 | + |
| 76 | + public GlideImageLoader load(@DrawableRes int resId, @DrawableRes int placeholder,Transformation<Bitmap> transformation) { |
| 77 | + return loadImage(resId2Uri(resId), placeholder, transformation); |
| 78 | + } |
| 79 | + |
| 80 | + protected RequestBuilder<Drawable> loadImage(Object obj) { |
| 81 | + if (obj instanceof String) { |
| 82 | + url = (String) obj; |
| 83 | + } |
| 84 | + return glideRequest.load(obj); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + protected GlideImageLoader loadImage(Object obj, @DrawableRes int placeholder, Transformation<Bitmap> transformation) { |
| 89 | + glideRequest = loadImage(obj); |
| 90 | + if (placeholder != 0) { |
| 91 | + glideRequest = glideRequest.placeholder(placeholder); |
| 92 | + } |
| 93 | + |
| 94 | + if (transformation != null) { |
| 95 | + glideRequest = glideRequest.transform(transformation); |
| 96 | + } |
| 97 | + |
| 98 | + glideRequest.into(new GlideImageViewTarget(getImageView())); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public GlideImageLoader listener(Object obj, OnProgressListener onProgressListener) { |
| 103 | + if (obj instanceof String) { |
| 104 | + url = (String) obj; |
| 105 | + } |
| 106 | + ProgressManager.addListener(url, onProgressListener); |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + private class GlideImageViewTarget extends DrawableImageViewTarget { |
| 111 | + GlideImageViewTarget(ImageView view) { |
| 112 | + super(view); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onLoadStarted(Drawable placeholder) { |
| 117 | + super.onLoadStarted(placeholder); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onLoadFailed(Drawable errorDrawable) { |
| 122 | + OnProgressListener onProgressListener = ProgressManager.getProgressListener(getUrl()); |
| 123 | + if (onProgressListener != null) { |
| 124 | + onProgressListener.onProgress(true, 100, 0, 0); |
| 125 | + ProgressManager.removeListener(getUrl()); |
| 126 | + } |
| 127 | + super.onLoadFailed(errorDrawable); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onResourceReady( Drawable resource,Transition<? super Drawable> transition) { |
| 132 | + OnProgressListener onProgressListener = ProgressManager.getProgressListener(getUrl()); |
| 133 | + if (onProgressListener != null) { |
| 134 | + onProgressListener.onProgress(true, 100, 0, 0); |
| 135 | + ProgressManager.removeListener(getUrl()); |
| 136 | + } |
| 137 | + super.onResourceReady(resource, transition); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments