/** * DrawableRequestBuilder 的通行配置 * @param ctx 上下文 * @param img ImageLoader封装器 * @param typeRequest url or resource * @param <T> String or Integer * @return DrawableRequestBuilder<T> */ private <T> DrawableRequestBuilder<T> getDrawableRequestBuilder(Context ctx, ImageLoader img, DrawableTypeRequest<T> typeRequest) { DrawableRequestBuilder<T> builder = typeRequest .placeholder(img.placeHolder) .error(img.errorHolder) .diskCacheStrategy(DiskCacheStrategy.ALL); // transform image type int transType = img.transType; if (transType == LoaderConfig.TRANS_CIRCLE) { builder = builder.transform(new GlideCircleTransform(ctx)); } else if (transType == LoaderConfig.TRANS_ROUND) { builder = builder.transform(new GlideRoundTransform(ctx)); } // SCALE type if (img.scaleType == LoaderConfig.SCALE_CENTER_CROP) { builder = builder.centerCrop(); } else if (img.scaleType == LoaderConfig.SCALE_FIT_CENTER) { builder = builder.fitCenter(); } return builder; }
/** * 显示图片 * @param context 上下文 * @param imageView 图片view * @param uri 图片路径 * @param bitmapTransformation bitmap转换器 */ private static void display(Context context, final ImageView imageView, String uri, BitmapTransformation bitmapTransformation, boolean isAvatar){ DrawableTypeRequest<String> drawableTypeRequest = Glide.with(context).load(uri); drawableTypeRequest .centerCrop() .placeholder(ERROR_IMAGE) .error(isAvatar ? AVATAR_IMAGE : ERROR_IMAGE) .diskCacheStrategy(DiskCacheStrategy.ALL) .dontAnimate(); // 添加转换器 if(null != bitmapTransformation){ drawableTypeRequest.bitmapTransform(bitmapTransformation); } // 设置图片 drawableTypeRequest.into(imageView); }
/** */ @Nullable @Override protected Bitmap onLoad(@NonNull RequestManager loader) { ensureHasTargetOrThrow(); final DrawableTypeRequest<String> request = onPrepareRequest(loader); try { return request.asBitmap().into(-1, -1).get(); } catch (InterruptedException | ExecutionException e) { throw new ImageLoader.Error( ImageLoader.Error.REASON_UNKNOWN, "Failed to load image.", e ); } }
/** * 图片加载 * @author leibing * @createTime 2016/8/15 * @lastModify 2016/8/15 * @param context 上下文 * @param imageView 图片显示控件 * @param localPath 图片本地链接 * @param defaultImage 默认占位图片 * @param errorImage 加载失败后图片 * @return */ public void load(Context context, ImageView imageView, File localPath, Drawable defaultImage, Drawable errorImage){ // 图片加载库采用Glide框架 DrawableTypeRequest request = Glide.with(context).load(localPath); request.thumbnail(0.1f) //用原图的1/10作为缩略图 .placeholder(defaultImage) //设置资源加载过程中的占位Drawable .crossFade() //设置加载渐变动画 .priority(Priority.NORMAL) //指定加载的优先级,优先级越高越优先加载, // 但不保证所有图片都按序加载 // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW // 默认为Priority.NORMAL .fallback(null) //设置model为空时要显示的Drawable // 如果没设置fallback,model为空时将显示error的Drawable, // 如果error的Drawable也没设置,就显示placeholder的Drawable .error(errorImage) //设置load失败时显示的Drawable .skipMemoryCache(true) //设置跳过内存缓存,但不保证一定不被缓存 // (比如请求已经在加载资源且没设置跳过内存缓存,这个资源就会被缓存在内存中) .diskCacheStrategy(DiskCacheStrategy.RESULT) //缓存策略DiskCacheStrategy.SOURCE: // 缓存原始数据,DiskCacheStrategy.RESULT: // 缓存变换(如缩放、裁剪等)后的资源数据, // DiskCacheStrategy.NONE:什么都不缓存, // DiskCacheStrategy.ALL:缓存SOURC和RESULT。 // 默认采用DiskCacheStrategy.RESULT策略, // 对于download only操作要使用DiskCacheStrategy.SOURCE .into(imageView); }
@Override public void loadNet(Context context, String url, Options options, final LoadCallback callback) { DrawableTypeRequest request = getRequestManager(context).load(url); if (options == null) options = Options.defaultOptions(); if (options.loadingResId != Options.RES_NONE) { request.placeholder(options.loadingResId); } if (options.loadErrorResId != Options.RES_NONE) { request.error(options.loadErrorResId); } wrapScaleType(request, options) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .crossFade() .into(new SimpleTarget<GlideBitmapDrawable>() { @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); if (callback != null) { callback.onLoadFailed(e); } } @Override public void onResourceReady(GlideBitmapDrawable resource, GlideAnimation<? super GlideBitmapDrawable> glideAnimation) { if (resource != null && resource.getBitmap() != null) { if (callback != null) { callback.onLoadReady(resource.getBitmap()); } } } }); }
private void load(DrawableTypeRequest request, ImageView target, Options options) { if (options == null) options = Options.defaultOptions(); if (options.loadingResId != Options.RES_NONE) { request.placeholder(options.loadingResId); } if (options.loadErrorResId != Options.RES_NONE) { request.error(options.loadErrorResId); } wrapScaleType(request, options) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .crossFade() .into(target); }
private DrawableTypeRequest wrapScaleType(DrawableTypeRequest request, Options options) { if (options != null && options.scaleType != null) { switch (options.scaleType) { case MATRIX: break; case FIT_XY: break; case FIT_START: break; case FIT_END: break; case CENTER: break; case CENTER_INSIDE: break; case FIT_CENTER: request.fitCenter(); break; case CENTER_CROP: request.centerCrop(); break; } } return request; }
@Override public void loadNet(Context context, String url, Options options, final LoadCallback callback) { DrawableTypeRequest request = getRequestManager(context).load(url); if (options == null) options = Options.defaultOptions(); if (options.loadingResId != Options.RES_NONE) { request.placeholder(options.loadingResId); } if (options.loadErrorResId != Options.RES_NONE) { request.error(options.loadErrorResId); } request.diskCacheStrategy(DiskCacheStrategy.SOURCE) .crossFade() .into(new SimpleTarget<GlideBitmapDrawable>() { @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { super.onLoadFailed(e, errorDrawable); if (callback != null) { callback.onLoadFailed(e); } } @Override public void onResourceReady(GlideBitmapDrawable resource, GlideAnimation<? super GlideBitmapDrawable> glideAnimation) { if (resource != null && resource.getBitmap() != null) { if (callback != null) { callback.onLoadReady(resource.getBitmap()); } } } }); }
private void load(DrawableTypeRequest request, ImageView target, Options options) { if (options == null) options = Options.defaultOptions(); if (options.loadingResId != Options.RES_NONE) { request.placeholder(options.loadingResId); } if (options.loadErrorResId != Options.RES_NONE) { request.error(options.loadErrorResId); } request.diskCacheStrategy(DiskCacheStrategy.SOURCE) .crossFade() .into(target); }
public static DrawableTypeRequest createBaseRequest(RequestManager requestManager, Song song, boolean ignoreMediaStore) { if (ignoreMediaStore) { return requestManager.load(new AudioFileCover(song.data)); } else { return requestManager.loadFromMediaStore(MusicUtil.getMediaStoreAlbumCoverUri(song.albumId)); } }
private void load(DrawableTypeRequest request, ImageView target, Options options) { if (options == null) options = Options.defaultOptions(); if (options.loadingResId != Options.RES_NONE) { request.placeholder(options.loadingResId); } if (options.loadErrorResId != Options.RES_NONE) { request.error(options.loadErrorResId); } request.crossFade().into(target); }
/** * 图片加载 * @author leibing * @createTime 2016/8/15 * @lastModify 2016/8/15 * @param context 上下文 * @param imageView 图片显示控件 * @param localPath 图片本地链接 * @param defaultImage 默认占位图片 * @param errorImage 加载失败后图片 * @param isCropCircle 是否圆角 * @return */ public void load(Context context, ImageView imageView, File localPath, Drawable defaultImage, Drawable errorImage , boolean isCropCircle){ // 图片加载库采用Glide框架 DrawableTypeRequest request = Glide.with(context).load(localPath); // 设置scaleType request.centerCrop(); // 圆角裁切 if (isCropCircle) request.bitmapTransform(new CropCircleTransformation(context)); request.thumbnail(0.1f) //用原图的1/10作为缩略图 .placeholder(defaultImage) //设置资源加载过程中的占位Drawable .crossFade() //设置加载渐变动画 .priority(Priority.NORMAL) //指定加载的优先级,优先级越高越优先加载, // 但不保证所有图片都按序加载 // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW // 默认为Priority.NORMAL .fallback(null) //设置model为空时要显示的Drawable // 如果没设置fallback,model为空时将显示error的Drawable, // 如果error的Drawable也没设置,就显示placeholder的Drawable .error(errorImage) //设置load失败时显示的Drawable .skipMemoryCache(true) //设置跳过内存缓存,但不保证一定不被缓存 // (比如请求已经在加载资源且没设置跳过内存缓存,这个资源就会被缓存在内存中) .diskCacheStrategy(DiskCacheStrategy.RESULT) //缓存策略DiskCacheStrategy.SOURCE: // 缓存原始数据,DiskCacheStrategy.RESULT: // 缓存变换(如缩放、裁剪等)后的资源数据, // DiskCacheStrategy.NONE:什么都不缓存, // DiskCacheStrategy.ALL:缓存SOURC和RESULT。 // 默认采用DiskCacheStrategy.RESULT策略, // 对于download only操作要使用DiskCacheStrategy.SOURCE .into(imageView); }
@Override public void preload(final FileItem item) { final DrawableTypeRequest<String> glideLoad = Glide .with(context) .load(item.getPath()); if (PLAY_GIF) { // Play GIFs glideLoad.preload(); } else { // Force bitmap so GIFs don't play glideLoad.asBitmap().preload(); } }
static DrawableTypeRequest createBaseRequest(RequestManager requestManager, Song song, boolean ignoreMediaStore) { if (ignoreMediaStore) { return requestManager.load(new AudioFileCover(song.data)); } else { return requestManager.loadFromMediaStore(MusicUtil.getMediaStoreAlbumCoverUri(song.albumId)); } }
static DrawableTypeRequest createBaseRequest(RequestManager requestManager, Artist artist, boolean noCustomImage, boolean forceDownload) { boolean hasCustomImage = CustomArtistImageUtil.getInstance(RetroApplication.getInstance()).hasCustomArtistImage(artist); if (noCustomImage || !hasCustomImage) { return requestManager.load(new ArtistImage(artist.getName(), forceDownload)); } else { return requestManager.load(CustomArtistImageUtil.getFile(artist)); } }
public void load(Context context, ImageView imageView, File localPath, Drawable defaultImage, Drawable errorImage , boolean isCropCircle){ DrawableTypeRequest request = Glide.with(context).load(localPath); request.centerCrop(); if (isCropCircle) request.bitmapTransform(new CropCircleTransformation(context)); request.thumbnail(0.1f) .placeholder(defaultImage) .crossFade() .priority(Priority.NORMAL) .fallback(null) .error(errorImage) .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.RESULT) .into(imageView); }
/** * 图片加载 * @author leibing * @createTime 2016/8/15 * @lastModify 2016/8/15 * @param context 上下文 * @param imageView 图片显示控件 * @param url 图片链接 * @param defaultImage 默认占位图片 * @param errorImage 加载失败后图片 * @param isCropCircle 是否圆角 * @return */ public static void load(Context context, ImageView imageView, String url, Drawable defaultImage, Drawable errorImage , boolean isCropCircle){ // 图片加载库采用Glide框架 DrawableTypeRequest request = Glide.with(context).load(url); // 设置scaleType request.centerCrop(); // 圆角裁切 if (isCropCircle) request.bitmapTransform(new CropCircleTransformation(context)); request.placeholder(defaultImage) //设置资源加载过程中的占位Drawable .crossFade() //设置加载渐变动画 .priority(Priority.NORMAL) //指定加载的优先级,优先级越高越优先加载, // 但不保证所有图片都按序加载 // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW // 默认为Priority.NORMAL .fallback(null) //设置model为空时要显示的Drawable // 如果没设置fallback,model为空时将显示error的Drawable, // 如果error的Drawable也没设置,就显示placeholder的Drawable .error(errorImage) //设置load失败时显示的Drawable .skipMemoryCache(true) //设置跳过内存缓存,但不保证一定不被缓存 // (比如请求已经在加载资源且没设置跳过内存缓存,这个资源就会被缓存在内存中) .diskCacheStrategy(DiskCacheStrategy.RESULT) //缓存策略DiskCacheStrategy.SOURCE: // 缓存原始数据,DiskCacheStrategy.RESULT: // 缓存变换(如缩放、裁剪等)后的资源数据, // DiskCacheStrategy.NONE:什么都不缓存, // DiskCacheStrategy.ALL:缓存SOURC和RESULT。 // 默认采用DiskCacheStrategy.RESULT策略, // 对于download only操作要使用DiskCacheStrategy.SOURCE .into(imageView); }
private static DrawableTypeRequest<String> getDrawableTypeRequest(Context context, String url) { if (UserSettingConstants.NO_IMAGE_MODE && !NetworkChecker.isWifiConnected(context)) return Glide.with(context) .using(cacheOnlyStreamLoader) .load(url); else return Glide.with(context) .load(url); }
/** * 图片加载 * @author leibing * @createTime 2016/8/15 * @lastModify 2016/8/15 * @param context 上下文 * @param imageView 图片显示控件 * @param localPath 图片本地链接 * @param defaultImage 默认占位图片 * @param errorImage 加载失败后图片 * @param isCropCircle 是否圆角 * @return */ public void load(Context context, ImageView imageView, File localPath, Drawable defaultImage, Drawable errorImage , boolean isCropCircle){ if (!localPath.exists()){ Toast.makeText(context, "not local file resources,please checkout it!", Toast.LENGTH_SHORT).show(); return; } // 图片加载库采用Glide框架 DrawableTypeRequest request = Glide.with(context).load(localPath); // 设置scaleType request.centerCrop(); // 圆角裁切 if (isCropCircle) request.bitmapTransform(new CropCircleTransformation(context)); request.thumbnail(0.1f) //用原图的1/10作为缩略图 .placeholder(defaultImage) //设置资源加载过程中的占位Drawable .crossFade() //设置加载渐变动画 .priority(Priority.NORMAL) //指定加载的优先级,优先级越高越优先加载, // 但不保证所有图片都按序加载 // 枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW // 默认为Priority.NORMAL .fallback(null) //设置model为空时要显示的Drawable // 如果没设置fallback,model为空时将显示error的Drawable, // 如果error的Drawable也没设置,就显示placeholder的Drawable .error(errorImage) //设置load失败时显示的Drawable .skipMemoryCache(true) //设置跳过内存缓存,但不保证一定不被缓存 // (比如请求已经在加载资源且没设置跳过内存缓存,这个资源就会被缓存在内存中) .diskCacheStrategy(DiskCacheStrategy.RESULT) //缓存策略DiskCacheStrategy.SOURCE: // 缓存原始数据,DiskCacheStrategy.RESULT: // 缓存变换(如缩放、裁剪等)后的资源数据, // DiskCacheStrategy.NONE:什么都不缓存, // DiskCacheStrategy.ALL:缓存SOURC和RESULT。 // 默认采用DiskCacheStrategy.RESULT策略, // 对于download only操作要使用DiskCacheStrategy.SOURCE .into(imageView); }
public static void load(Context context, ImageView imageView, String url, Drawable defaultImage, Drawable errorImage , boolean isCropCircle){ DrawableTypeRequest request = Glide.with(context).load(url); request.centerCrop(); if (isCropCircle) request.bitmapTransform(new CropCircleTransformation(context)); request.placeholder(defaultImage) .crossFade() .priority(Priority.NORMAL) .fallback(null) .error(errorImage) .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.RESULT) .into(imageView); }
/** */ @Override protected boolean onLoad(@NonNull RequestManager loader, @Nullable ImageLoader.Callback callback) { ensureHasTargetOrThrow(); final DrawableTypeRequest<String> request = onPrepareRequest(loader); if (callback != null) request.listener(new Listener(this, callback)); if (mView != null) request.into(mView); else request.into(ASYNC_TARGET); return true; }
/** * Prepares loading request with configuration based on this task's parameters. * * @param requestManager Manager used to create the request. * @return Prepared image loading request to be executed. */ @NonNull protected DrawableTypeRequest<String> onPrepareRequest(@NonNull RequestManager requestManager) { final DrawableTypeRequest<String> request = requestManager.load(mTarget); if (mPlaceholderRes != NO_RESOURCE_ID) request.error(mPlaceholderRes); if (mPlaceholder != null) request.error(mPlaceholder); if (mErrorRes != NO_RESOURCE_ID) request.placeholder(mErrorRes); if (mError != null) request.placeholder(mError); if (mTransformation != null) request.transform(mTransformation); if (hasRequest(REQUEST_DO_NOT_ANIMATE)) request.dontAnimate(); return request; }
public static Bitmap loadNotificationIcon(final Recipient recipient) throws ExecutionException, InterruptedException { final RequestManager requestManager = Glide.with(BaseApplication.get()); final DrawableTypeRequest typeRequest = recipient.isGroup() ? requestManager.load(recipient.getGroupAvatar().getBytes()) : requestManager.load(recipient.getUserAvatar()); return (Bitmap) typeRequest .asBitmap() .transform(new CropCircleTransformation(BaseApplication.get())) .into(200, 200) .get(); }
@BindingAdapter(value = {"url","placeholder"},requireAll = false) public static void loadImage(ImageView view, String url,int placeHolder) { DrawableTypeRequest<String> load = Glide.with(view.getContext()).load(url); if (0!= placeHolder){ load.placeholder(placeHolder); } load.into(view); }
@Override public void displayVideoImage(Context activity, String path, ImageView imageView) { DrawableTypeRequest glide; glide = Glide.with(activity).load(new File(path)); glide.override(500, 500); glide.centerCrop(); glide.placeholder(R.drawable.img_default) .into(imageView); }
public static DrawableTypeRequest createBaseRequest(RequestManager requestManager, Artist artist, boolean noCustomImage, boolean forceDownload) { boolean hasCustomImage = CustomArtistImageUtil.getInstance(App.getInstance()).hasCustomArtistImage(artist); if (noCustomImage || !hasCustomImage) { return requestManager.load(new ArtistImage(artist.getName(), forceDownload)); } else { return requestManager.load(CustomArtistImageUtil.getFile(artist)); } }
private void loadNetworkDependent() { RequestManager requestManager = Glide.with(context); DrawableTypeRequest<String> request; // if you need transformations or other options specific for the load, chain them here if (deviceOnWifi()) { request = requestManager.load("http://www.placehold.it/750x750"); } else { request = requestManager.load("http://www.placehold.it/100x100"); } request.into(imageView1); }
/** * load image with Glide */ private void loadNetCache(Context ctx, ImageLoader img) { DrawableTypeRequest<String> urlTypeRequest = getUrlTypeRequest(ctx, img); DrawableRequestBuilder<String> builder = getDrawableRequestBuilder(ctx, img, urlTypeRequest); builder.crossFade(LoaderConfig.DEFAULT_DURATION_MS).into(img.imgView); }
private DrawableTypeRequest<String> getUrlTypeRequest(Context ctx, ImageLoader img) { return Glide.with(ctx).load(img.url); }
/** * load resource image with Glide */ private void loadResource(Context ctx, ImageLoader img) { DrawableTypeRequest<Integer> resTypeRequest = getResTypeRequest(ctx, img); DrawableRequestBuilder<Integer> builder = getDrawableRequestBuilder(ctx, img, resTypeRequest); builder.crossFade(LoaderConfig.DEFAULT_DURATION_MS).into(img.imgView); }
private DrawableTypeRequest<Integer> getResTypeRequest(Context ctx, ImageLoader img) { return Glide.with(ctx).load(img.resId); }