/**
* 将gif处理成圆形
*
* @param imageView
* @param url
* @param reqWidth
* @param reqHeight
*/
public static void displayRoundImageSupportGif(final ImageView imageView, String url, int reqWidth, int reqHeight) {
if (imageView == null || TextUtils.isEmpty(url)) {
return;
}
ImageDecodeOptions decodeBuilder = ImageDecodeOptions.newBuilder()
.setDecodeAllFrames(false)
.setDecodePreviewFrame(true)
.build();
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
.setImageDecodeOptions(decodeBuilder)
.setResizeOptions(new ResizeOptions(reqWidth, reqHeight))
.build();
final DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline()
.fetchDecodedImage(imageRequest, null);
BaseDataSubscriber<CloseableReference<CloseableImage>> source =
new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> source) {
}
@Override
protected void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> source) {
Bitmap bitmap = getBitmap(source);
bitmap = makeRoundCorner(bitmap);
if (bitmap == null || bitmap.isRecycled()) {
return;
}
try {
imageView.setImageBitmap(bitmap);
} catch (Throwable e) {
e.printStackTrace();
}
dataSource.close();
}
};
dataSource.subscribe(source, UiThreadImmediateExecutorService.getInstance());
}
/**
* 获取bitmap,如果是gif则提取第一帧
*
* @param source
*
* @return
*/
private static Bitmap getBitmap(DataSource<CloseableReference<CloseableImage>> source) {
if (source == null) {
return null;
}
CloseableReference<CloseableImage> reference = source.getResult();
if (reference == null) {
return null;
}
CloseableImage image = reference.get();
if (image == null) {
return null;
}
Bitmap bitmap = null;
if (image instanceof CloseableBitmap) {
// 普通bitmap
CloseableBitmap cb = (CloseableBitmap) image;
bitmap = cb.getUnderlyingBitmap();
} else if (image instanceof CloseableAnimatedImage) {
// 如果是有动画的image,就是gif
CloseableAnimatedImage bitmapImage = (CloseableAnimatedImage) image;
AnimatedImageResult animatedImageResult = bitmapImage.getImageResult();
if (animatedImageResult == null) {
return null;
}
//图片转为bitmap
CloseableReference<Bitmap> picBitmap = animatedImageResult.getPreviewBitmap();
if (picBitmap == null) {
return null;
}
//获取到bitmap,可是这个bitmap返回的是null
bitmap = picBitmap.get();
}
return bitmap;
}
/**
* 将bitmap变成圆
*
* @param bitmap
*
* @return
*/
public static Bitmap makeRoundCorner(Bitmap bitmap) {
if (bitmap == null || bitmap.isRecycled()) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int left = 0, top = 0, right = width, bottom = height;
float roundPx = height / 2;
if (width > height) {
left = (width - height) / 2;
top = 0;
right = left + height;
bottom = height;
} else if (height > width) {
left = 0;
top = (height - width) / 2;
right = width;
bottom = top + width;
roundPx = width / 2;
}
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Fresco加载gif实现圆角(无动画)
最新推荐文章于 2021-05-27 14:07:57 发布