Fresco加载gif实现圆角(无动画)

    /**
     * 将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;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值