如何使用Glide库将Bitmap加载到我的ImageView中?我想用文本创建自定义图像,并使用Glide将其加载到imageview中.
这是我用文本创建自定义位图的方法
public Bitmap imageWithText(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.BLACK);
tv.setTypeface(null, Typeface.BOLD);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
tv.setPadding(0, 25, 0, 0);
Bitmap testB = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, 100, 100);
tv.draw(c);
return testB;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用滑动加载此位图时,我收到错误
Glide.with(getContext()).load(imageWithText("Random text")).into(holder.imgPhoto);
Run Code Online (Sandbox Code Playgroud) 我有图像文件(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;
}
Run Code Online (Sandbox Code Playgroud)
其中BitmapUtils是一个实现类的按位方法的自定义类。
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 …Run Code Online (Sandbox Code Playgroud)