带有 Universal Image Loader 的圆形位图

Circular bitmap with Universal Image Loader

我们正在显示来自 URL 的带有大图标的通知。因此,为了加载图像,我们使用带有以下代码的 Universal Image Loader:

mImageLoader.loadImage(imagePath, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            notificationBuilder.setLargeIcon(loadedImage);
            notificationManager.notify(notification.getId(), notificationBuilder.build());
        }
});

此代码运行良好,图像已成功显示。我们的问题是我们想要显示一个圆形的大图标,所以我们需要位图是圆形的。我们试过设置:

    final DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
            .displayer(new RoundedBitmapDisplayer(1000))
            .build();

但这不起作用,我们已经尝试使用 RoundedBitmapDrawable,但我们需要位图而不是位图可绘制对象。 请问如何才能得到圆形位图?

提前致谢。

试试这个:

 public static Bitmap getCircularBitmap(Bitmap bitmap) {
    Bitmap output;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

您尝试设置选项 .displayer(new CircleBitmapDisplayer()) 了吗?例如:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
 .displayer(new CircleBitmapDisplayer())
 .build();