android 图片圆角
时间: 2023-06-30 11:06:06 浏览: 41
在 Android 中,可以通过以下步骤将图片裁剪成圆角:
1. 创建一个包含圆角的形状:
```java
public class RoundedCornersShape extends Shape {
private final float radius;
public RoundedCornersShape(float radius) {
this.radius = radius;
}
@Override
public void draw(Canvas canvas, Paint paint) {
RectF rect = new RectF(0, 0, getWidth(), getHeight());
canvas.drawRoundRect(rect, radius, radius, paint);
}
}
```
2. 创建一个 BitmapDrawable,并将其设置为 ImageView 的背景:
```java
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(radius);
imageView.setImageDrawable(roundedBitmapDrawable);
```
其中 `radius` 是圆角的半径,可以根据需要进行调整。