使通知大图标圆

时间:2015-02-02 15:03:52

标签: android android-notifications android-5.0-lollipop avatar

我希望将来自用户联系人的圆形头像显示为通知的大图标 - 例如在接收文本或邮件时。当我将大图标设置为该联系人的图像时,会生成一个方形图标。

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

enter image description here

如何完成它?

4 个答案:

答案 0 :(得分:16)

由于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;
}

答案 1 :(得分:8)

Egor的回答很有效。在此处发布代码链接消失:

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;
}

答案 2 :(得分:7)

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

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;
}

答案 3 :(得分:0)

我认为当输入位图是一个完美的正方形(相同的高度和宽度)可以得到完美的圆形但是如果你的位图是矩形或宽度高于它产生问题时它会工作正常。我已将代码修改为 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>