Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android circular Imageview in App Widget

So I'm trying to make an app widget and display a profile picture there which should look like a circular ImageView. normally I'd just use a circular ImageView library, but for app widgets you can't extend views (so only vanilla ImageView) and it has to work with RemoteViews

So I'm playing around with the idea of using a Drawable to overlay/underlay to achieve this effect. I tried gusridd's solution from here Display FB profile pic in circular image view in Application, but the background which my profile picture sits on is also an image, and this just makes it a white square with a circlular image inside :(

edit: please read bolded portion before answering :X

like image 965
Jason Hu Avatar asked Jan 25 '26 13:01

Jason Hu


1 Answers

I had faced the same issue in Custom Notification using RemoteView then following solution helps me out.

Solution: Use circular cropped bitmap

public Bitmap getCroppedBitmap(Bitmap bitmap) throws Exception {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}
like image 102
Garg's Avatar answered Jan 27 '26 02:01

Garg's



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!