您可以使用这个替代 WrapContentDraweeView 类:
public class WrapContentDraweeView extends GenericDraweeView {
private PipelineDraweeControllerBuilderSupplier mDraweeControllerBuilderSupplier;
private PipelineDraweeControllerBuilder mSimpleDraweeControllerBuilder;
public WrapContentDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
super(context, hierarchy);
this.init(context, (AttributeSet) null);
}
public WrapContentDraweeView(Context context) {
super(context);
this.init(context, (AttributeSet) null);
}
public WrapContentDraweeView(Context context, AttributeSet attrs) {
super(context, attrs);
this.init(context, attrs);
}
public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.init(context, attrs);
}
@TargetApi(21)
public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.init(context, attrs);
}
private void init(Context context, @Nullable AttributeSet attrs) {
if (!this.isInEditMode()) {
mDraweeControllerBuilderSupplier = Fresco.getDraweeControllerBuilderSupplier();
Preconditions.checkNotNull(mDraweeControllerBuilderSupplier,
"SimpleDraweeView was not initialized!");
this.mSimpleDraweeControllerBuilder = mDraweeControllerBuilderSupplier.get();
if (attrs != null) {
TypedArray gdhAttrs = context.obtainStyledAttributes(attrs,
R.styleable.SimpleDraweeView);
try {
if (gdhAttrs.hasValue(
R.styleable.SimpleDraweeView_actualImageUri)) {
this.setImageURI((Uri) Uri.parse(gdhAttrs.getString(
R.styleable.SimpleDraweeView_actualImageUri)),
(Object) null);
} else if (gdhAttrs.hasValue(
R.styleable.SimpleDraweeView_actualImageResource)) {
int resId = gdhAttrs.getResourceId(
R.styleable.SimpleDraweeView_actualImageResource,
-1);
}
} finally {
gdhAttrs.recycle();
}
}
}
}
protected SimpleDraweeControllerBuilder getControllerBuilder() {
return this.mSimpleDraweeControllerBuilder;
}
public void setImageURI(Uri uri) {
this.setImageURI((Uri) uri, (Object) null);
}
public void setImageURI(@Nullable String uriString) {
this.setImageURI((String) uriString, (Object) null);
}
public void setImageURI(Uri uri, @Nullable Object callerContext) {
setAspectRatio(FrescoUtils.getAspectRatio(uri));
ImageDecodeOptions decoderOptions = ImageDecodeOptions.newBuilder()
.setDecodePreviewFrame(true)
.build();
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri)
.setLocalThumbnailPreviewsEnabled(true)
.setImageDecodeOptions(decoderOptions)
.setPostprocessor(new BasePostprocessor() {
@Override
public CloseableReference<Bitmap> process(Bitmap sourceBitmap,
PlatformBitmapFactory bitmapFactory) {
int height = sourceBitmap.getHeight()/2;
int width = sourceBitmap.getWidth()/2;
if (width / (float) height < 0.5f) {
CloseableReference<Bitmap> cropReference = null;
CloseableReference<Bitmap> scaledReference = null;
try {
cropReference = bitmapFactory
.createBitmap(sourceBitmap, 0, 0, width,
(int) (width / 1.66f));
scaledReference = bitmapFactory
.createScaledBitmap(cropReference.get(), getWidth(),
(int) (getWidth() / 1.66f), false);
Bitmap bm = scaledReference.get();
Canvas canvas = new Canvas(bm);
Bitmap badge = getBadgeBitmap("长图");
int padding = (int) (getResources().getDisplayMetrics().density
* 16);
canvas.drawBitmap(badge, bm.getWidth() - badge.getWidth() - padding,
bm.getHeight() - badge.getHeight() - padding, new Paint());
return super.process(bm, bitmapFactory);
} finally {
if (cropReference != null) {
cropReference.close();
}
if (scaledReference != null) {
scaledReference.close();
}
}
}
return super.process(sourceBitmap, bitmapFactory);
}
})
.build();
DraweeController controller = this.mSimpleDraweeControllerBuilder
.setImageRequest(imageRequest)
.setCallerContext(callerContext)
.setOldController(this.getController())
.setAutoPlayAnimations(true)
.setControllerListener(FrescoUtils.createControllerListener(this, uri))
.setOldController(this.getController())
.build();
this.setController(controller);
}
public void setImageURI(@Nullable String uriString, @Nullable Object callerContext) {
Uri uri = uriString != null ? Uri.parse(uriString) : null;
this.setImageURI(uri, callerContext);
}
public void setImageResource(int resId) {
super.setImageResource(resId);
}
private Bitmap getBadgeBitmap(String badge) {
final DisplayMetrics dm = getResources().getDisplayMetrics();
final float density = dm.density;
final float scaledDensity = dm.scaledDensity;
final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint
.SUBPIXEL_TEXT_FLAG);
textPaint.setColor(Color.WHITE);
textPaint.setTypeface(Typeface.create("sans-serif-black", Typeface.NORMAL));
textPaint.setTextSize(12 * scaledDensity);
final float padding = 4 * density;
final float cornerRadius = 2 * density;
final Rect textBounds = new Rect();
textPaint.getTextBounds(badge, 0, badge.length(), textBounds);
final int height = (int) (padding + textBounds.height() + padding);
final int width = (int) (padding + textBounds.width() + padding);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
final Canvas canvas = new Canvas(bitmap);
final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
backgroundPaint.setColor(Color.DKGRAY);
RectF rectF = new RectF(0, 0, width, height);
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, backgroundPaint);
// punch out the badge word, leaving transparency
// textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// 居中
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
int baseline = (int) (rectF.centerY() - (fontMetrics.top + fontMetrics.bottom) / 2);
textPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(badge, rectF.centerX(), baseline, textPaint);
return bitmap;
}
}
然后在您的布局中使用 WrapContentDraweeView 而不是 SimpleDraweeView 并使用您的包名称而不是 fresco 包
<com.package.info.WrapContentDraweeView
android:id="@+id/gridThumbnail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
fresco:placeholderImage="@drawable/placeholder"
/>
这比后端实现要好。