imageview设置圆角
时间: 2023-07-04 12:07:37 浏览: 56
你可以通过以下方式设置 ImageView 的圆角:
1. 在 xml 文件中设置:
使用 `shape` 标签创建一个圆角矩形 Shape,然后将其作为 ImageView 的背景。
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp" />
</shape>
<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_rounded_corner_shape"
android:src="@drawable/my_image" />
```
2. 在 Java 代码中设置:
使用 `Bitmap` 和 `Canvas` 创建一个圆角矩形 Bitmap,然后将其设置为 ImageView 的图片。
```java
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(10f);
imageView.setImageDrawable(roundedBitmapDrawable);
```