将边框设置为圆形图像问题

时间:2014-07-21 11:46:29

标签: android

我找到了几个如何创建圆圈图像并采用最简单的图像的例子。 我试图在圆形图像上创建一个黑色边框,但我认为我无法看到它。 如何在新的圆形图像上绘制黑色边框。

这是我的代码:

  public Bitmap getCircleBitmap(Bitmap bitmap){
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader (bitmap,  Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setShader(shader);
    paint.setAntiAlias(true);
    Canvas c = new Canvas(circleBitmap);

    Path path = new Path();

    path.addCircle(((float) bitmap.getWidth()) / 2,((float) bitmap.getHeight()) / 2 ,
                            (Math.min(((float) bitmap.getWidth()),((float) bitmap.getHeight())) / 2),Path.Direction.CCW);
    c.clipPath(path);
    c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2 , bitmap.getWidth()/2, paint);
    return circleBitmap;
}

感谢。

3 个答案:

答案 0 :(得分:1)

希望它对你有所帮助,这对我有用..

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;

答案 1 :(得分:1)

试试这个..

public static Bitmap getCircularBitmap(Bitmap bitmap) {
if (bitmap == null || bitmap.isRecycled()) {
    return null;
}
float radius = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap
        .getHeight()) / 2f : ((float) bitmap.getWidth()) / 2f;
Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP,
        TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);

Canvas canvas = new Canvas(canvasBitmap);

canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
        radius, paint);

return canvasBitmap;

}

答案 2 :(得分:0)

试试这个,

在drawable中创建circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >


<solid android:color="#00000000" />

<stroke
android:width="1dp"
android:color="#ff000000"/>

</shape>

将此圆圈设为可查看的背景。