public class OrImageGetter implements Html.ImageGetter {
private DkDrawable mOrDrawable = null;
private TextView mTargetTextView;
private Context mContext;
private int mWidth;
private int mHeight;
public OrImageGetter(Context context, TextView targetTextView, int width, int height) {
this(context, targetTextView);
this.mWidth = width;
this.mHeight = height;
}
public DkImageGetter(Context context, TextView targetTextView) {
this.mTargetTextView = targetTextView;
this.mContext = context;
}
@Override
public Drawable getDrawable(final String source) {
mOrDrawable = new OrDrawable();
Glide.with(mContext)
.load(source)
.asBitmap()
.fitCenter()
.into(new SimpleTarget() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation super Bitmap> glideAnimation) {
Bitmap scaleBitmap = ((BitmapDrawable) mContext.getResources().getDrawable(R.drawable.test_drawable)).getBitmap();
scaleBitmap = toScaleBitmap(scaleBitmap, mWidth, mHeight);
mOrDrawable.setDkBitmap(scaleBitmap);
mOrDrawable.setBounds(0, 0, scaleBitmap.getWidth(), scaleBitmap.getHeight());
mTargetTextView.setText(mTargetTextView.getText());
mTargetTextView.invalidate();
}
});
return mOrDrawable;
}
public class OrDrawable extends BitmapDrawable {
public Bitmap mOrBitmap;
public Bitmap getOrBitmap() {
return mOrBitmap;
}
public void setOrBitmap(Bitmap mBitmap) {
this.mOrBitmap = mBitmap;在这里插入代码片
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (mOrBitmap != null) {
canvas.drawBitmap(mOrBitmap, 0, 0, getPaint());
}
}
}
/* 根据给定的高宽,对位图进行缩放操作*/
public Bitmap toScaleBitmap(Bitmap bitmap, int width, int height) {
if (width == 0 && height == 0) {
return bitmap;
}
int originalWidth = bitmap.getWidth();
int originalHeight = bitmap.getHeight();
float scaleHeight = ((float) height) / originalHeight;
float scaleWidth = ((float) width) / originalWidth;
float minScale = scaleWidth;
if (scaleWidth > scaleHeight) {
minScale = scaleHeight;
}
Matrix matrix = new Matrix();
matrix.postScale(minScale, minScale);
return Bitmap.createBitmap(bitmap, 0, 0, originalWidth, originalHeight, matrix, true);
}
}