Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon rounding issue resolved #6770

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
Expand Down Expand Up @@ -74,6 +80,32 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @
return builder;
}

/**
* Creates a canvas to draw a circle and then draws the bitmap avatar within that circle
* to clip off the area of the bitmap outside the circular path and returns a circular
* bitmap.
*
* @param bitmap The bitmap image to modify.
*/
public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int defaultBackgroundColor = 0xff424242;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this color used only for masking? If this is shown on the notification then we need to make it theme-specific.
e.g. Dark mode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This color is the fill color for the circle that will be drawn in the background. I don't think that it will ever be shown as there will always be a bitmap drawn on top of it. But we could make it theme specific just in case.

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

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(defaultBackgroundColor);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}

/**
* Applies the message style to the notification builder. It also takes advantage of the
* notification cache to build conversations.
Expand Down Expand Up @@ -208,7 +240,7 @@ private IconCompat fetchIcon(String urlString) {

try {
Bitmap bitmap = future.get(MAX_ICON_FETCH_WAIT_TIME_SECONDS, TimeUnit.SECONDS);
return IconCompat.createWithBitmap(bitmap);
return IconCompat.createWithBitmap(getCroppedBitmap(bitmap));
} catch (InterruptedException e) {
Log.e(TAG,"Failed to fetch icon", e);
Thread.currentThread().interrupt();
Expand Down