imageview设置圆角
时间: 2024-04-17 16:23:26 浏览: 18
可以通过以下步骤设置 Android ImageView 的圆角:
1. 创建一个 xml 文件,例如 "rounded_corner.xml",并将以下代码复制到文件中:
```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" />
</shape>
```
2. 在布局文件中,将 ImageView 的背景设置为刚才创建的 xml 文件:
```
<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner"
android:src="@drawable/my_image" />
```
3. 如果需要在代码中设置圆角,可以使用以下代码:
```
ImageView imageView = findViewById(R.id.my_image_view);
GradientDrawable drawable = (GradientDrawable) imageView.getBackground();
drawable.setCornerRadius(10f);
```
其中,10f 是圆角的半径,可以根据需要进行调整。