Android Glide加载图片文件apply overlay并设置为imageview
Android Glide load picture file apply overlay and set to imageview
我有图像文件(png/jpg)。当加载到列表视图时,其中一些我需要用另一个透明图像覆盖。我使用类似以下的方法来执行此操作:
public Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){
Bitmap bitmap = null;
try{
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
Resources r = context.getResources();
Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage);
Drawable[] layers = new Drawable[2];
layers[0] = imageAsDrawable;
layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromResource(r, overlayDrawableResourceId, width, height));
LayerDrawable layerDrawable = new LayerDrawable(layers);
bitmap = BitmapUtils.drawableToBitmap(layerDrawable);
}catch (Exception ex){}
return bitmap;
}
其中 BitmapUtils 是自定义的 class,它实现位图方式的方法。
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.media.ThumbnailUtils;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
public class BitmapUtils {
public static Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){
Bitmap bitmap = null;
try{
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
Resources r = context.getResources();
Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage);
Drawable[] layers = new Drawable[2];
layers[0] = imageAsDrawable;
layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromResource(r, overlayDrawableResourceId, width, height));
LayerDrawable layerDrawable = new LayerDrawable(layers);
bitmap = BitmapUtils.drawableToBitmap(layerDrawable);
}catch (Exception ex){}
return bitmap;
}
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Compute inSampleSize
options.inSampleSize = computeInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
使用 Android(UIL) 的通用图像加载器库,通过编写自定义 ImageDecoder 并将其应用于 ImageLoaderConfiguration 来实现此效果。
问题是,我如何使用 Glide 做到这一点?
谢谢。
对我有用的东西:
我使用了以下答案:
public void display(final Context context, String mediaPath, final ImageView imageView, final boolean doApplyOverlay){
try{
Glide.with(context)
.load(mediaPath)
.asBitmap()
.placeholder(R.drawable.placeholder_default)
.error(R.drawable.error_default)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (doApplyOverlay) {
resource = BitmapUtils.applyOverlay(context, resource, R.drawable.overlay);
}
imageView.setImageBitmap(resource);
}
});
}catch (Exception ex){}
}
因此,要加载位图并在显示给用户之前与其交互,您需要实现自定义目标并在那里进行交互。 Glide 提供的 SimpleTarget class 实现了 Target 接口并允许此类事情。有关详细信息,您可以在 github 上访问 Glide 的 CustomTargets wiki 页面:https://github.com/bumptech/glide/wiki/Custom-targets
我有图像文件(png/jpg)。当加载到列表视图时,其中一些我需要用另一个透明图像覆盖。我使用类似以下的方法来执行此操作:
public Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){
Bitmap bitmap = null;
try{
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
Resources r = context.getResources();
Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage);
Drawable[] layers = new Drawable[2];
layers[0] = imageAsDrawable;
layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromResource(r, overlayDrawableResourceId, width, height));
LayerDrawable layerDrawable = new LayerDrawable(layers);
bitmap = BitmapUtils.drawableToBitmap(layerDrawable);
}catch (Exception ex){}
return bitmap;
}
其中 BitmapUtils 是自定义的 class,它实现位图方式的方法。
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.media.ThumbnailUtils;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
public class BitmapUtils {
public static Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){
Bitmap bitmap = null;
try{
int width = sourceImage.getWidth();
int height = sourceImage.getHeight();
Resources r = context.getResources();
Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage);
Drawable[] layers = new Drawable[2];
layers[0] = imageAsDrawable;
layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromResource(r, overlayDrawableResourceId, width, height));
LayerDrawable layerDrawable = new LayerDrawable(layers);
bitmap = BitmapUtils.drawableToBitmap(layerDrawable);
}catch (Exception ex){}
return bitmap;
}
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Compute inSampleSize
options.inSampleSize = computeInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
使用 Android(UIL) 的通用图像加载器库,通过编写自定义 ImageDecoder 并将其应用于 ImageLoaderConfiguration 来实现此效果。
问题是,我如何使用 Glide 做到这一点?
谢谢。
对我有用的东西:
我使用了以下答案:
public void display(final Context context, String mediaPath, final ImageView imageView, final boolean doApplyOverlay){
try{
Glide.with(context)
.load(mediaPath)
.asBitmap()
.placeholder(R.drawable.placeholder_default)
.error(R.drawable.error_default)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (doApplyOverlay) {
resource = BitmapUtils.applyOverlay(context, resource, R.drawable.overlay);
}
imageView.setImageBitmap(resource);
}
});
}catch (Exception ex){}
}
因此,要加载位图并在显示给用户之前与其交互,您需要实现自定义目标并在那里进行交互。 Glide 提供的 SimpleTarget class 实现了 Target 接口并允许此类事情。有关详细信息,您可以在 github 上访问 Glide 的 CustomTargets wiki 页面:https://github.com/bumptech/glide/wiki/Custom-targets