添加在圆形的位图圆框圈位图、圆形、圆框圈

2023-09-03 22:04:43 作者:狂卩龙灬巅丷峰

我试着创建在我的位图圆框!

Im trying to create a round frame around my bitmap!

通过这个code即时通讯能够让我的位图圆:

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;
}

我已经试过是画一个圆(在outcommented线)用帆布,但没有结果。 有谁知道我可以添加一个圆形的边框呢?

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!

推荐答案

您必须绘制的位图后圆的。这是什么做的伎俩我。

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.