问题
I'm trying to fill Bitmap
inside white circle, more specifically:
I've a Bitmap
for example this:
And I want this:
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;
}
回答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)
回答2:
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>
来源:https://stackoverflow.com/questions/41749852/bitmap-inside-white-circle