[英]Bitmap inside white circle
I'm trying to fill Bitmap
inside white circle, more specifically: 我正在尝试在白色圆圈内填充
Bitmap
,更具体地说:
I've a Bitmap
for example this: 我有一个
Bitmap
,例如:
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;
}
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). 我猜您用来绘制椭圆的颜色的ALPHA =0。尝试用Color(1.0f,1.0f,1.0f,1.0f)替换Color.WHITE。 Tell me if that solved your problem, and take a look at: What does PorterDuff.Mode mean in android graphics.What does it do?
告诉我这是否解决了您的问题,然后看一下: PorterDuff.Mode在android图形中的含义是什么?
In this case, for SRC_IN: [Sa * Da, Sc * Da] (Taken from android reference, PorterDuff.Mode) 在这种情况下,对于SRC_IN:[Sa * Da,Sc * Da](摘自android参考,PorterDuff.Mode)
Here's an example that uses a shape made in XML. 这是一个使用XML制成的形状的示例。
1) Right-click on your drawable resource folder and create a new Drawable resource file. 1)右键单击您的drawable资源文件夹,然后创建一个新的Drawable资源文件。
2) Paste the following code. 2)粘贴以下代码。 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. 3)使用布局中的资源。 Substitute my placeholder Android checkbox with your 'tick' resource file.
用您的“ tick”资源文件替换我的Android占位符复选框。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.