【问题标题】:Adding a round frame circle on rounded bitmap在圆形位图上添加圆形框圈
【发布时间】:2013-06-07 02:07:10
【问题描述】:

我正在尝试围绕我的位图创建一个圆形框架!

使用此代码,我可以使我的位图变圆:

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

我尝试用画布画一个圆圈(注释掉的线),但没有结果。 有谁知道如何在它周围添加一个圆形边框?

编辑

当我使用线路时:

canvas.drawCircle(0, 0, bitmap.getWidth(), paint);

效果是,3 个角变圆,但左上角保持不变(90 度) 但是我看不到任何线条或圆圈!

【问题讨论】:

  • 你有什么错误吗?如果没有是什么问题?
  • @Sam 哦对不起,我会更新问题!
  • 后面的小影子是怎么做出来的?

标签: java android bitmap android-canvas


【解决方案1】:

更新

现在有RoundedBitmapDrawable 和Support library 中的相应工厂,我建议使用它,除非需要更大的灵活性。


原答案

你必须在位图之后画圆。这就是我的诀窍。

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;   

这当然不包括你的例子的花哨的影子。 您可能想稍微调整一下额外的像素。

【讨论】:

  • 如何从这里获得更大的图像?
  • 嗨@lovis,我知道常数8和4在不同的绘图语句中增加宽度和高度有什么用,你能解释一下吗?还有一件事,我们添加了这些值(8 和 4)以像素为单位,与屏幕密度 dp 无关
  • @AymanMahgoub 嘿,这些值是随机选择的。但他们将决定边界的大小。左右 4px 边框为 8,顶部和底部相同。使用像素,因为它对示例更容易。
  • 谢谢@lovis,知道了)
猜你喜欢
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
相关资源
最近更新 更多