使通知大图标圆形

Make Notification Large Icon Round

我想将用户联系人的圆形头像显示为通知的大图标 - 比如在接收短信或邮件时。当我将大图标设置为该联系人的图像时,结果是一个方形图标。

我正在寻找看起来像顶部图标(方形头像),看起来像电子邮件通知上的大图标(圆形头像)的东西:

如何让它变圆?

由于 setLargeIcon() 接受 Bitmap,您需要做的就是从源创建一个循环 Bitmap

以下是来自 Create a circle bitmap in Android 的代码(我自己没试过)。

private Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
}

Egor 的回答很有效。在此处发布代码,以防 link 消失:

private Bitmap getCircleBitmap(Bitmap bitmap) {
 final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
  bitmap.getHeight(), Bitmap.Config.ARGB_8888);
 final Canvas canvas = new Canvas(output);

 final int color = Color.RED;
 final Paint paint = new Paint();
 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
 final RectF rectF = new RectF(rect);

 paint.setAntiAlias(true);
 canvas.drawARGB(0, 0, 0, 0);
 paint.setColor(color);
 canvas.drawOval(rectF, paint);

 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
 canvas.drawBitmap(bitmap, rect, rect, paint);

 bitmap.recycle();

 return output;
}

接受的答案要求输入位图是一个完美的正方形(相同的高度和宽度)。如果您的位图是矩形的,它将 return 变成椭圆形。我修改了代码以接受任何形状的位图和 return 以输入位图中间为中心的圆圈。

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    Bitmap output;
    Rect srcRect, dstRect;
    float r;
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    if (width > height){
        output = Bitmap.createBitmap(height, height, Bitmap.Config.ARGB_8888);
        int left = (width - height) / 2;
        int right = left + height;
        srcRect = new Rect(left, 0, right, height);
        dstRect = new Rect(0, 0, height, height);
        r = height / 2;
    }else{
        output = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        int top = (height - width)/2;
        int bottom = top + width;
        srcRect = new Rect(0, top, width, bottom);
        dstRect = new Rect(0, 0, width, width);
        r = width / 2;
    }

    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, srcRect, dstRect, paint);

    bitmap.recycle();

    return output;
}

我认为当输入位图是一个完美的正方形(相同的高度和宽度)并给出完美的圆时它会工作正常但是如果你的位图在 rectangle-shaped 或者宽度大于它创建的高度一个问题。我已将代码修改为 kotlin 还为其添加了边框,只需要将输入作为位图

private fun getCircleBitmap(bitmap: Bitmap): Bitmap 

  {

    var srcRect: Rect

    var dstRect: Rect

    var r: Float

    var paint = Paint();

    var width: Int = bitmap.getWidth()

    var height: Int = bitmap.getHeight()

    var widthToGenerate = 100F

    var heightToGenerate = 100F

    var borderWidth: Float = 1.toFloat()

    var output: Bitmap

    var canvas: Canvas

    if (width > height) {

        output = Bitmap.createBitmap(widthToGenerate.toInt(), heightToGenerate.toInt(), Bitmap.Config.ARGB_8888);

        canvas = Canvas(output);
        val scale: Float = (widthToGenerate / width)


        var xTranslation = 0.0f
        var yTranslation: Float = (heightToGenerate - height * scale) / 2.0f;


        var transformation = Matrix();
        transformation.postTranslate(xTranslation, yTranslation)
        transformation.preScale(scale, scale)

        var color: Int = Color.WHITE
        paint.setAntiAlias(true)
        paint.setColor(color)

        canvas.drawBitmap(bitmap, transformation, paint)

    } else {
        output = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888)
        canvas = Canvas(output);
        var top: Int = (height - width) / 2
        var bottom: Int = top + width
        srcRect = Rect(0, top, width, bottom)
        dstRect = Rect(0, 0, width, width);
        r = (width / 2).toFloat()


        var color: Int = Color.GRAY
        paint.setAntiAlias(true)
        canvas.drawARGB(0, 0, 0, 0)
        paint.setColor(color)


        canvas.drawCircle(r + borderWidth, r + borderWidth, r + borderWidth, paint)
        canvas.drawCircle(r, r, r, paint)
        paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))

        canvas.drawBitmap(bitmap, srcRect, dstRect, paint)

        bitmap.recycle()
    }

    return output

}

notifiation_drawable.xml 边框文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2.0"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
    android:width="2dp"
    android:color="@android:color/darker_gray" />

</shape>