0

I'm trying to fill Bitmap inside white circle, more specifically:

I've a Bitmap for example this:

enter image description here

And I want this:

enter image description here

I've make background gray for understand image.

I use this method, but it doesn't make what I want..

public static Bitmap getRoundedBitmap(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.WHITE;
    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;
}
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54

2 Answers2

1

I guess the color you use to paint the oval has ALPHA = 0. Try replacing Color.WHITE with Color(1.0f, 1.0f, 1.0f, 1.0f). Tell me if that solved your problem, and take a look at: What does PorterDuff.Mode mean in android graphics.What does it do?

In this case, for SRC_IN: [Sa * Da, Sc * Da] (Taken from android reference, PorterDuff.Mode)

Community
  • 1
  • 1
NicoBerrogorry
  • 446
  • 4
  • 15
-1

Here's an example that uses a shape made in XML.

1) Right-click on your drawable resource folder and create a new Drawable resource file.

2) Paste the following code. This creates a drawable resource which has a pretty similar look to the background that you wanted.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size android:width="50dp" android:height="50dp"/>
            <solid android:color="@android:color/darker_gray"/>
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <size android:width="50dp" android:height="50dp"/>
            <solid android:color="@android:color/white"/>
        </shape>
    </item>
</layer-list>

3) Use the resource in your layout. Substitute my placeholder Android checkbox with your 'tick' resource file.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/highlight_circle">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@android:drawable/checkbox_on_background"/>
</RelativeLayout>
Charlie
  • 2,876
  • 19
  • 26
  • Can you clarify what you would like? Are you looking to place the bitmap into a circle / grey background in order to use in a view? – Charlie Jan 19 '17 at 21:43
  • No, I've a Bitmap (first image) and I want to wrap it into white circle (like second image, but another Bitmap, not resource file needed) – Michele Lacorte Jan 20 '17 at 00:04