在圆角位图上添加圆框圆 [英] Adding a round frame circle on rounded bitmap
问题描述
我正在尝试围绕我的位图创建一个圆形框架!
Im trying to create a round frame around my bitmap!
使用此代码,我可以使位图变圆:
With this code im able to make my bitmap round:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff4242DB;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = bitmap.getWidth()/2;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//canvas.drawCircle(0, 0, bitmap.getWidth(), paint);
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
我试过的是用画布画一个圆圈(注释掉的线),但没有结果.有谁知道如何在它周围添加圆形边框?
What i've tried is to draw a circle(the outcommented line) with canvas, but It had no result. Does anyone know how I can add a circular border around it?
编辑
当我使用该行时:
canvas.drawCircle(0, 0, bitmap.getWidth(), paint);
效果是,3个角变圆,但左上角保持不变(90度)但是我看不到任何线条或圆圈!
The effect is, that 3 corners get rounded but the upper left stays the same(90 degrees) But I can't see any line or circle!
推荐答案
更新
现在有 RoundedBitmapDrawable 和支持库中的相应工厂我建议使用它,除非需要更大的灵活性.
There now is RoundedBitmapDrawable and a corresponding factory in the Support library I recommend to use that, unless more flexibility is required.
原答案
您必须在位图之后绘制圆圈.这就是对我有用的技巧.
You have to draw the circle after the bitmap. This is what did the trick for me.
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int radius = Math.min(h / 2, w / 2);
Bitmap output = Bitmap.createBitmap(w + 8, h + 8, Config.ARGB_8888);
Paint p = new Paint();
p.setAntiAlias(true);
Canvas c = new Canvas(output);
c.drawARGB(0, 0, 0, 0);
p.setStyle(Style.FILL);
c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);
p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
c.drawBitmap(bitmap, 4, 4, p);
p.setXfermode(null);
p.setStyle(Style.STROKE);
p.setColor(Color.WHITE);
p.setStrokeWidth(3);
c.drawCircle((w / 2) + 4, (h / 2) + 4, radius, p);
return output;
这当然不包括您的示例的花哨阴影.您可能想稍微处理一下额外的像素.
This does of course not include the fancy shadow of your example. You might want to play around a little bit with the additional pixels.
这篇关于在圆角位图上添加圆框圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!