Bitmap inside white circle

你说的曾经没有我的故事 提交于 2019-12-12 02:17:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!