Fresco載入gif實現圓角(無動畫)
阿新 • • 發佈:2019-01-25
/** * 將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; }