Adding a round frame circle on rounded bitmap

前端 未结 1 1885
执笔经年
执笔经年 2020-12-08 12:14

Im trying to create a round frame around my bitmap!

\"This

With this c

相关标签:
1条回答
  • 2020-12-08 12:49

    Update

    There now is RoundedBitmapDrawable and a corresponding factory in the Support library I recommend to use that, unless more flexibility is required.


    Original Answer

    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.

    0 讨论(0)
提交回复
热议问题