实现圆形头像方法有很多,此处使用个人认为最为方便,以及最为快速的一种方法,使用第三方开源库。此开源库提供了一个第三方ImageView的控件:RoundedImageView。
实现步骤:
第一步:引入第三方开源库(使用的开发工具为Android Studio)
在build.gradle中引入需要的开源库
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.makeramen:roundedimageview:2.2.1' // 此处为引入开源库代码
}
接着点击同步(Sync now)即可引入成功
第二步:布局中使用此第三方控件
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/home_listview_logo"
android:src="@mipmap/icon"
android:layout_gravity="center"
app:riv_oval="true"
android:layout_width="50dp"
android:layout_height="50dp" />
注意:由于是第三方的控件,所以需要带上长长的 com.makeramen.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:右下角弧度