This commit is contained in:
2022-09-09 15:20:59 +08:00
parent 89ed3f5121
commit dd39064ca5
10 changed files with 187 additions and 109 deletions

View File

@@ -1,64 +0,0 @@
package com.yunbao.common.glide;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import jp.wasabeef.glide.transformations.BlurTransformation;
public class ImageLoadUtils {
public static void initImageLoader(Context context) {
ImageLoader imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
.diskCache(new UnlimitedDiskCache(context.getCacheDir()))
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.build();
imageLoader.init(configuration);
}
private static ImageLoader getLoader() {
return ImageLoader.getInstance();
}
private static DisplayImageOptions getDisplayImageOptions() {
return new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
public static void loadUrl(String url, int width, int height, ImageView imageView) {
//ImageSize size = new ImageSize(width, height);
//getLoader().displayImage(url,imageView,size);
}
public static void loadUrlToBlur(Context context,String url,int width,int height,ImageView imageView){
getLoader().loadImage(url,new ImageSize(width,height),getDisplayImageOptions(),new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
Glide.with(context).asBitmap().load(loadedImage)
.skipMemoryCache(false)
.apply(RequestOptions.bitmapTransform( new BlurTransformation(25)))
.into(imageView);
}
});
}
}

View File

@@ -2,7 +2,9 @@ package com.yunbao.common.glide;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.widget.ImageView;
@@ -11,6 +13,7 @@ import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.yunbao.common.R;
@@ -32,36 +35,113 @@ public class ImgLoader {
sBlurTransformation = new BlurTransformation(25);
}
public static void display(Context context, String url, ImageView imageView) {
display(context, url, imageView, -1, -1);
}
public static void display(Context context, String url, ImageView imageView, int width, int height) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(url).skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asBitmap()
.load(url)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (width == -1 || height == -1) {
imageView.setImageBitmap(resource);
} else {
Bitmap bitmap = ThumbnailUtils.extractThumbnail(resource, width, height);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
public static void display2(Context context, String url, ImageView imageView) {
display2(context, url, imageView, -1, -1);
}
public static void display2(Context context, String url, ImageView imageView, int width, int height) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(url).placeholder(imageView.getDrawable()).dontAnimate().skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asBitmap()
.load(url)
.placeholder(imageView.getDrawable())
.dontAnimate()
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (width == -1 || height == -1) {
imageView.setImageBitmap(resource);
} else {
Bitmap bitmap = ThumbnailUtils.extractThumbnail(resource, width, height);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
public static void display2(Context context, int url, ImageView imageView) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(url).placeholder(imageView.getDrawable()).dontAnimate().skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asDrawable()
.load(url)
.placeholder(imageView.getDrawable())
.dontAnimate()
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(imageView);
}
public static void displayWithError(Context context, String url, ImageView imageView, int errorRes) {
displayWithError(context, url, imageView, errorRes, -1, -1);
}
public static void displayWithError(Context context, String url, ImageView imageView, int errorRes, int width, int height) {
if (!contextIsExist(context)) {
return;
}
if (imageView == null) {
return;
}
Glide.with(context).asDrawable().load(url).error(errorRes).skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asBitmap()
.load(url)
.error(errorRes)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (width == -1 || height == -1) {
imageView.setImageBitmap(resource);
} else {
Bitmap bitmap = ThumbnailUtils.extractThumbnail(resource, width, height);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
public static void displayWithError(Context context, int url, ImageView imageView, int errorRes) {
@@ -78,8 +158,34 @@ public class ImgLoader {
if (!contextIsExist(context)) {
return;
}
displayWithError(context, url, imageView, R.mipmap.icon_avatar_placeholder);
displayAvatar(context, url, imageView, -1, -1);
}
public static void displayAvatar(Context context, String url, ImageView imageView, int width, int height) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context)
.asBitmap()
.load(url)
.error(R.mipmap.icon_avatar_placeholder)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (width == -1 || height == -1) {
imageView.setImageBitmap(resource);
} else {
Bitmap bitmap = ThumbnailUtils.extractThumbnail(resource, width, height);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
public static void displayAvatar(Context context, int url, ImageView imageView) {
@@ -94,14 +200,22 @@ public class ImgLoader {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(file).skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asDrawable()
.load(file)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(imageView);
}
public static void display(Context context, int res, ImageView imageView) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(res).skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asDrawable()
.load(res)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(imageView);
}
/**
@@ -111,7 +225,11 @@ public class ImgLoader {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(Uri.fromFile(new File(videoPath))).skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asDrawable()
.load(Uri.fromFile(new File(videoPath)))
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(imageView);
}
/**
@@ -121,7 +239,11 @@ public class ImgLoader {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asGif().load(videoPath).skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView);
Glide.with(context)
.asGif()
.load(videoPath)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.into(imageView);
}
public static void displayDrawable(Context context, String url, final DrawableCallback callback) {
@@ -158,27 +280,67 @@ public class ImgLoader {
* 显示模糊的毛玻璃图片
*/
public static void displayBlur(Context context, String url, ImageView imageView) {
displayBlur(context, url, imageView, -1, -1);
}
public static void displayBlur(Context context, String url, ImageView imageView, int width, int height) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(url)
Glide.with(context)
.asBitmap()
.load(url)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.apply(RequestOptions.bitmapTransform(sBlurTransformation))
.into(imageView);
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (width == -1 || height == -1) {
imageView.setImageBitmap(resource);
} else {
Bitmap bitmap = ThumbnailUtils.extractThumbnail(resource, width, height);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
/**
* 显示模糊的毛玻璃图片
*/
public static void displayBlurLive(Context context, String url, ImageView imageView) {
displayBlurLive(context, url, imageView, -1, -1);
}
public static void displayBlurLive(Context context, String url, ImageView imageView, int width, int height) {
if (!contextIsExist(context)) {
return;
}
Glide.with(context).asDrawable().load(url)
Glide.with(context).asBitmap().load(url)
.skipMemoryCache(SKIP_MEMORY_CACHE)
.apply(RequestOptions.bitmapTransform(new BlurTransformation(100)))
.placeholder(R.mipmap.live_bg)
.into(imageView);
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (width == -1 || height == -1) {
imageView.setImageBitmap(resource);
} else {
Bitmap bitmap = ThumbnailUtils.extractThumbnail(resource, width, height);
imageView.setImageBitmap(bitmap);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
private static boolean contextIsExist(Context context) {

View File

@@ -8,6 +8,7 @@ import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.ms.banner.holder.BannerViewHolder;
import com.yunbao.common.bean.BannerBean;
import com.yunbao.common.glide.ImgLoader;
public class CustomViewHolder implements BannerViewHolder<BannerBean> {
@@ -20,7 +21,8 @@ public class CustomViewHolder implements BannerViewHolder<BannerBean> {
);
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context).load(data.getImageUrl()).into(imageView);
//Glide.with(context).load(data.getImageUrl()).into(imageView);
ImgLoader.display(context,data.getImageUrl(),imageView,600,170);
return imageView;
}