Android studio中如何用bitmap将图片设置成圆形?请给出代码
时间: 2023-04-09 15:00:54 浏览: 165
可以使用以下代码将图片设置成圆形:
```
public Bitmap getCircleBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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);
return output;
}
```
使用方法:
```
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Bitmap circleBitmap = getCircleBitmap(bitmap);
imageView.setImageBitmap(circleBitmap);
```
其中,`imageView` 是你要显示圆形图片的 `ImageView`。
阅读全文