下面列出了com.bumptech.glide.load.resource.bitmap.BitmapTransformation#com.bumptech.glide.DrawableTypeRequest 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void loadImgBackground(Object obj, final View v, SimpleTarget simpleTarget) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, v);
if (drawableTypeRequest == null) {
drawableTypeRequest = getDrawableTypeRequest(sCommonPlaceholder, v);
}
if (drawableTypeRequest != null) {
drawableTypeRequest
.asBitmap()
.centerCrop()
.dontAnimate()
.error(sCommonPlaceholder)
.placeholder(sCommonPlaceholder)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(simpleTarget != null ? simpleTarget : new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
BitmapDrawable drawable = new BitmapDrawable(resource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
v.setBackground(drawable);
} else {
v.setBackgroundDrawable(drawable);
}
}
});
}
}
public static void loadImgBlur(Object obj, ImageView img) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, img);
if (drawableTypeRequest == null) {
drawableTypeRequest = getDrawableTypeRequest(sCommonPlaceholder, img);
}
if (drawableTypeRequest != null) {
drawableTypeRequest
.centerCrop()
.dontAnimate()
.error(sCommonPlaceholder)
.placeholder(sCommonPlaceholder)
.bitmapTransform(new BlurTransformation(img.getContext(), 16, 4))
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(img);
}
}
/**
* 加载圆形图片
*
* @param obj 要加载的资源
* @param img 被加载的图片
*/
public static void loadCircleImg(Object obj, ImageView img) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, img);
if (drawableTypeRequest == null) {
drawableTypeRequest = getDrawableTypeRequest(sCirclePlaceholder, img);
}
if (drawableTypeRequest != null) {
drawableTypeRequest
.centerCrop()
.dontAnimate()
.transform(new AFGlideUtil.GlideCircleTransform(img.getContext()))
.error(sCirclePlaceholder)
.placeholder(sCirclePlaceholder).
diskCacheStrategy(DiskCacheStrategy.RESULT).into(img);
}
}
/**
* 加载圆角图片
*
* @param obj 要加载的资源
* @param img 被加载的图片
* @param dp 圆角大小
*/
public static void loadRoundImg(Object obj, ImageView img, int dp) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, img);
if (drawableTypeRequest == null) {
drawableTypeRequest = getDrawableTypeRequest(sRoundPlaceholder, img);
}
if (drawableTypeRequest != null) {
drawableTypeRequest
.centerCrop()
.dontAnimate()
.transform(new BitmapTransformation[]{new AFGlideUtil.GlideRoundTransform(img.getContext(), dp)})
.error(sRoundPlaceholder)
.placeholder(sRoundPlaceholder).
diskCacheStrategy(DiskCacheStrategy.RESULT).into(img);
}
}
@Nullable
private static DrawableTypeRequest getDrawableTypeRequest(Object obj, View img) {
if (img == null || obj == null) return null;
Context context = img.getContext();
RequestManager manager = Glide.with(context);
DrawableTypeRequest drawableTypeRequest = null;
if (obj instanceof String) {
drawableTypeRequest = manager.load((String) obj);
} else if (obj instanceof Integer) {
drawableTypeRequest = manager.load((Integer) obj);
} else if (obj instanceof Uri) {
drawableTypeRequest = manager.load((Uri) obj);
} else if (obj instanceof File) {
drawableTypeRequest = manager.load((File) obj);
}
return drawableTypeRequest;
}
/**
* 图片加载
* @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);
}
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));
}
}
public static DrawableTypeRequest createBaseRequest(RequestManager requestManager, Artist artist, boolean noCustomImage) {
boolean hasCustomImage = CustomArtistImageUtil.getInstance(App.getInstance()).hasCustomArtistImage(artist);
if (noCustomImage || !hasCustomImage) {
final List<AlbumCover> songs = new ArrayList<>();
for (final Album album : artist.albums) {
final Song song = album.safeGetFirstSong();
songs.add(new AlbumCover(album.getYear(), song.data));
}
return requestManager.load(new ArtistImage(artist.getName(), songs)); } else {
return requestManager.load(CustomArtistImageUtil.getFile(artist));
}
}
public static void loadImage(Object obj, ImageView img) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, img);
if (drawableTypeRequest == null) {
drawableTypeRequest = getDrawableTypeRequest(sCommonPlaceholder, img);
}
if (drawableTypeRequest != null) {
drawableTypeRequest
.centerCrop()
.dontAnimate()
.error(sCommonPlaceholder)
.placeholder(sCommonPlaceholder)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(img);
}
}
/**
* 加载圆形图片
*
* @param obj 要加载的资源
* @param img 被加载的图片
*/
public static void loadCircleImgNoPlace(Object obj, ImageView img) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, img);
if (drawableTypeRequest != null) {
drawableTypeRequest
.centerCrop()
.dontAnimate()
.transform(new AFGlideUtil.GlideCircleTransform(img.getContext()))
.diskCacheStrategy(DiskCacheStrategy.RESULT).into(img);
}
}
public static void loadImgBlurNoPlace(Object obj, ImageView img) {
DrawableTypeRequest drawableTypeRequest = getDrawableTypeRequest(obj, img);
if (drawableTypeRequest != null) {
drawableTypeRequest
.centerCrop()
.dontAnimate()
.bitmapTransform(new BlurTransformation(img.getContext(), 16, 4))
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(img);
}
}
@Nullable
private DrawableTypeRequest getDrawableTypeRequest(SingleConfig config, RequestManager requestManager) {
DrawableTypeRequest request = null;
if(!TextUtils.isEmpty(config.getUrl())){
request= requestManager.load(MyUtil.appendUrl(config.getUrl()));
}else if(!TextUtils.isEmpty(config.getFilePath())){
request= requestManager.load(config.getFilePath());
}else if(!TextUtils.isEmpty(config.getContentProvider())){
request= requestManager.loadFromMediaStore(Uri.parse(config.getContentProvider()));
}else if(config.getResId() != 0){
request= requestManager.load(config.getResId());
}else if(config.getBytes() != null){
request = requestManager.load(config.getBytes());
} else {
//request= requestManager.load("http://www.baidu.com/1.jpg");//故意失败
int resId = getUseableResId(config);
if(resId != 0){
request = requestManager.load(resId);
}else {
request= requestManager.load("");
}
}
/* if(!TextUtils.isEmpty(config.getUrl()) && config.getUrl().contains(".gif")){
request.diskCacheStrategy(DiskCacheStrategy.ALL);//只缓存result
}else{
request.diskCacheStrategy(DiskCacheStrategy.SOURCE);//只缓存原图
}*/
request.diskCacheStrategy(DiskCacheStrategy.SOURCE);//只缓存原图
return request;
}
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));
}
}
@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();
}
}
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));
}
}
public 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));
}
}
/**
* 图片加载
* @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);
}
/**
* 图片加载
* @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);
}
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));
}
}
public static DrawableTypeRequest createBaseRequest(RequestManager requestManager, Artist artist, boolean noCustomImage) {
boolean hasCustomImage = CustomArtistImageUtil.getInstance(App.getInstance()).hasCustomArtistImage(artist);
if (noCustomImage || !hasCustomImage) {
final List<AlbumCover> songs = new ArrayList<>();
for (final Album album : artist.albums) {
final Song song = album.safeGetFirstSong();
songs.add(new AlbumCover(album.getYear(), song.data));
}
return requestManager.load(new ArtistImage(artist.getName(), songs));
} 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);
}
@OnMount
static void onMount(ComponentContext c, ImageView imageView,
@Prop(optional = true) String imageUrl, @Prop(optional = true) File file,
@Prop(optional = true) Uri uri, @Prop(optional = true) Integer resourceId,
@Prop(optional = true) RequestManager glideRequestManager,
@Prop(optional = true, resType = DRAWABLE) Drawable failureImage,
@Prop(optional = true, resType = DRAWABLE) Drawable fallbackImage,
@Prop(optional = true, resType = DRAWABLE) Drawable placeholderImage,
@Prop(optional = true) DiskCacheStrategy diskCacheStrategy,
@Prop(optional = true) RequestListener requestListener,
@Prop(optional = true) boolean asBitmap, @Prop(optional = true) boolean asGif,
@Prop(optional = true) boolean crossFade, @Prop(optional = true) int crossFadeDuration,
@Prop(optional = true) boolean centerCrop, @Prop(optional = true) boolean fitCenter,
@Prop(optional = true) boolean skipMemoryCache, @Prop(optional = true) Target target) {
if (imageUrl == null && file == null && uri == null && resourceId == null) {
throw new IllegalArgumentException(
"You must provide at least one of String, File, Uri or ResourceId");
}
if (glideRequestManager == null) {
glideRequestManager = Glide.with(c.getAndroidContext());
}
DrawableTypeRequest request;
if (imageUrl != null) {
request = glideRequestManager.load(imageUrl);
} else if (file != null) {
request = glideRequestManager.load(file);
} else if (uri != null) {
request = glideRequestManager.load(uri);
} else {
request = glideRequestManager.load(resourceId);
}
if (request == null) {
throw new IllegalStateException("Could not instantiate DrawableTypeRequest");
}
if (diskCacheStrategy != null) {
request.diskCacheStrategy(diskCacheStrategy);
}
if (asBitmap) {
request.asBitmap();
}
if (asGif) {
request.asGif();
}
if (crossFade) {
request.crossFade();
}
if (crossFadeDuration != DEFAULT_INT_VALUE) {
request.crossFade(crossFadeDuration);
}
if (centerCrop) {
request.centerCrop();
}
if (failureImage != null) {
request.error(failureImage);
}
if (fallbackImage != null) {
request.fallback(fallbackImage);
}
if (fitCenter) {
request.fitCenter();
}
if (requestListener != null) {
request.listener(requestListener);
}
if (placeholderImage != null) {
request.placeholder(placeholderImage);
}
request.skipMemoryCache(skipMemoryCache);
if (target != null) {
request.into(target);
} else {
request.into(imageView);
}
}
private void initMockGlideRequestManager() {
when(mockGlideRequestManager.load(TEST_CAT_IMAGE.getLink()))
.thenReturn(mock(DrawableTypeRequest.class));
}
@Override
public Drawable getDrawable(ImageHolder holder, final RichTextConfig config, TextView textView) {
final ImageTarget target;
final GenericRequestBuilder load;
DrawableTypeRequest dtr;
DrawableWrapper drawableWrapper = new DrawableWrapper();
byte[] src = Base64.decode(holder.getSource());
if (src != null) {
dtr = Glide.with(textView.getContext()).load(src);
} else {
dtr = Glide.with(textView.getContext()).load(holder.getSource());
}
Rect rect = null;
if (config.cacheType >= CacheType.LAYOUT) {
rect = loadCache(holder.getSource());
if (rect != null) {
drawableWrapper.setBounds(rect);
}
} else {
drawableWrapper.setBounds(0, 0, holder.getWidth(), holder.getHeight());
}
if (holder.isGif()) {
target = new ImageTargetGif(textView, drawableWrapper, holder, config, this, rect);
load = dtr.asGif();
} else {
target = new ImageTargetBitmap(textView, drawableWrapper, holder, config, this, rect);
load = dtr.asBitmap().atMost();
}
checkTag(textView);
targets.add(target);
if (!config.resetSize && holder.isInvalidateSize()) {
load.override(holder.getWidth(), holder.getHeight());
}
if (holder.getScaleType() == ImageHolder.ScaleType.CENTER_CROP) {
if (holder.isGif()) {
//noinspection ConstantConditions
((GifTypeRequest) load).centerCrop();
} else {
//noinspection ConstantConditions
((BitmapTypeRequest) load).centerCrop();
}
} else if (holder.getScaleType() == ImageHolder.ScaleType.FIT_CENTER) {
if (holder.isGif()) {
//noinspection ConstantConditions
((GifTypeRequest) load).fitCenter();
} else {
//noinspection ConstantConditions
((BitmapTypeRequest) load).fitCenter();
}
}
textView.post(new Runnable() {
@Override
public void run() {
load.placeholder(config.placeHolder).error(config.errorImage).into(target);
}
});
drawableWrapper.setCallback(textView);
return drawableWrapper;
}