153 lines
5.9 KiB
Java
153 lines
5.9 KiB
Java
package com.fuying.sn.utils;
|
||
|
||
import android.annotation.TargetApi;
|
||
import android.app.Activity;
|
||
import android.content.Context;
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.drawable.Drawable;
|
||
import android.os.Build;
|
||
import android.util.Log;
|
||
import android.widget.ImageView;
|
||
|
||
import androidx.fragment.app.Fragment;
|
||
|
||
import com.bumptech.glide.Glide;
|
||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
||
import com.bumptech.glide.request.RequestOptions;
|
||
|
||
import java.io.File;
|
||
|
||
/**
|
||
* Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
|
||
* Created by Li_Xavier on 2017/6/20 0020.
|
||
*/
|
||
public class GlideLoadUtils {
|
||
private String TAG = "ImageLoader";
|
||
|
||
/**
|
||
* 借助内部类 实现线程安全的单例模式
|
||
* 属于懒汉式单例,因为Java机制规定,内部类SingletonHolder只有在getInstance()
|
||
* 方法第一次调用的时候才会被加载(实现了lazy),而且其加载过程是线程安全的。
|
||
* 内部类加载的时候实例化一次instance。
|
||
*/
|
||
public GlideLoadUtils() {
|
||
}
|
||
|
||
private static class GlideLoadUtilsHolder {
|
||
private final static GlideLoadUtils INSTANCE = new GlideLoadUtils();
|
||
}
|
||
|
||
public static GlideLoadUtils getInstance() {
|
||
return GlideLoadUtilsHolder.INSTANCE;
|
||
}
|
||
|
||
/**
|
||
* Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
|
||
*
|
||
* @param context
|
||
* @param url 加载图片的url地址 String
|
||
* @param imageView 加载图片的ImageView 控件
|
||
* @param defaultImage 图片展示错误的本地图片 id
|
||
*/
|
||
public void glideLoad(Context context, String url, ImageView imageView, int defaultImage) {
|
||
if (context != null) {
|
||
Glide.with(context).load(url).centerCrop().error(defaultImage).placeholder(imageView.getDrawable()).dontAnimate().into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoadRound(Context context, String url, ImageView imageView, int defaultImage) {
|
||
if (context != null) {
|
||
Glide.with(context).load(url).centerCrop().error(defaultImage)
|
||
.placeholder(imageView.getDrawable()).dontAnimate()
|
||
.apply(RequestOptions.bitmapTransform(new RoundedCorners(ScreenUtils.dp2px(context, 16f))))
|
||
.into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
|
||
public void glideLoad(Context context, int resId, ImageView imageView, int defaultImage) {
|
||
if (context != null) {
|
||
Glide.with(context).load(resId).centerCrop().placeholder(imageView.getDrawable()).error(defaultImage).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Context context, String url, ImageView imageView) {
|
||
if (context != null) {
|
||
Glide.with(context).load(url).centerCrop().dontAnimate().placeholder(imageView.getDrawable()).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Context context, Bitmap bitmap, ImageView imageView) {
|
||
if (context != null) {
|
||
Glide.with(context).load(bitmap).centerCrop().dontAnimate().placeholder(imageView.getDrawable()).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Context context, Drawable drawable, ImageView imageView) {
|
||
if (context != null) {
|
||
Glide.with(context).load(drawable).centerCrop().dontAnimate().placeholder(imageView.getDrawable()).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Context context, Drawable drawable, ImageView imageView,int resId) {
|
||
if (context != null) {
|
||
Glide.with(context).load(drawable).centerCrop().dontAnimate().placeholder(imageView.getDrawable()).error(resId).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Context context, File file, ImageView imageView) {
|
||
if (context != null) {
|
||
Glide.with(context).load(file).centerCrop().into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Context context, File file, ImageView imageView, int defaultRes) {
|
||
if (context != null) {
|
||
Glide.with(context).load(file).centerCrop().error(defaultRes).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,context is null");
|
||
}
|
||
}
|
||
|
||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||
public void glideLoad(Activity activity, String url, ImageView imageView, int default_image) {
|
||
if (!activity.isDestroyed()) {
|
||
Glide.with(activity).load(url).centerCrop().error(default_image).placeholder(imageView.getDrawable()).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,activity is Destroyed");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(Fragment fragment, String url, ImageView imageView, int default_image) {
|
||
if (fragment != null && fragment.getActivity() != null) {
|
||
Glide.with(fragment).load(url).centerCrop().error(default_image).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,fragment is null");
|
||
}
|
||
}
|
||
|
||
public void glideLoad(android.app.Fragment fragment, String url, ImageView imageView, int default_image) {
|
||
if (fragment != null && fragment.getActivity() != null) {
|
||
Glide.with(fragment).load(url).centerCrop().error(default_image).into(imageView);
|
||
} else {
|
||
Log.i(TAG, "Picture loading failed,android.app.Fragment is null");
|
||
}
|
||
}
|
||
}
|