3

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

Community
  • 1
  • 1
Jason Hu
  • 1,237
  • 2
  • 15
  • 29

1 Answers1

2

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;
}
Garg
  • 2,731
  • 2
  • 36
  • 47