如何以编程方式为ImageView添加圆形

时间:2015-10-04 21:19:38

标签: android imageview

我想在我拥有的ImageView圈中添加一个圆形的黑色形状。

我想要这样的事情:

enter image description here

这是我制作ImageView圈子的代码:

 private Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
}

2 个答案:

答案 0 :(得分:0)

如果要使图像四舍五入,请按照此answer中的说明使用RoundedImageView。

答案 1 :(得分:0)

你可以使用CircleImageView:

https://github.com/hdodenhof/CircleImageView

compile 'de.hdodenhof:circleimageview:2.0.0'