在很多安卓应用中都会涉及到圆形头像,自定义imageview可能稍微复杂一点,这里介绍一个开源库,可以很方便的制作圆形,方形圆角等图片。使用 RoundedImageView 需要添加依赖,这里介绍怎么在 AS 中添加。使用效果图在文章结尾。
1.跟随下面图片箭头即可,不同版本的 AS 可能放置的位置不一样,但图标没有改变,找到相应的图标即可。
2.双击上图最后一个选项后,进入下面的界面,输入 RoundedImageView 。按下图操作。
这样 RoundedImageView 就添加到我们的项目中了。
2. RoundedImageView 的属性。使用 RoundedImageView 只需要在你需要的布局文件中使用即可。但要想获得圆形的图片就得保证你提供的图片是正方形的。
riv_border_width: 边框宽度 riv_border_color: 边框颜色 riv_oval: 是否圆形 riv_corner_radius: 圆角弧度 riv_corner_radius_top_left:左上角弧度 riv_corner_radius_top_right: 右上角弧度 riv_corner_radius_bottom_left:左下角弧度 riv_corner_radius_bottom_right:右下角弧度
这里附上我在实际中使用的部分代码。
<RelativeLayout android:id="@+id/top" android:layout_width="match_parent" android:layout_height="180dp" android:background="@drawable/girl2"> <com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/head" android:layout_width="200dp" android:layout_height="100dp" android:layout_marginTop="30dp" android:layout_centerHorizontal="true" android:src="@drawable/head" app:riv_border_color="#333333" app:riv_border_width="1dp" app:riv_oval="true" /> </RelativeLayout>效果如下: