Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Notification Large Icon Round

I want to display a circular avatar from the user's contacts as the large icon of the notification - like when receiving a text or mail. When I set the large icon as that contact's image, it results in a square icon.

I'm looking to turn something that looks like the top icon (the square avatar), look like the large icon on the email notification (the round avatar):

enter image description here

How do I make it round?

like image 871
Itai Hanski Avatar asked Feb 02 '15 15:02

Itai Hanski


People also ask

How do you make a big picture notification on Android?

Provide the bitmap to be used as the payload for the BigPicture notification. Provide the content Uri to be used as the payload for the BigPicture notification. Overrides ContentTitle in the big form of the template. Set the content description of the big picture.


3 Answers

Since setLargeIcon() accepts a Bitmap, all you need to do is create a circular Bitmap from the source.

Following is the code from Create a circle bitmap in Android (haven't tried myself).

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;
}
like image 166
Egor Avatar answered Oct 17 '22 16:10

Egor


The accepted answer requires the input bitmap to be a perfect square (same height and width). If your bitmap is rectangle-shaped, it will return an oval. I have modified the code to accept any-shaped bitmaps and return circles centered in the middle of the input bitmap.

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;
}
like image 26
Ridzwan Avatar answered Oct 17 '22 14:10

Ridzwan


Egor's answer worked great. Posting code here incase link disappears:

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;
}
like image 9
Micro Avatar answered Oct 17 '22 15:10

Micro