在很多安卓應用中都會涉及到圓形頭像,自定義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>
效果如下: