RoundedImageView - A RoundedImageView library that supports rounding any corner or circular shape. RoundedImageView is extended from AppCompatImageView.
官網(wǎng):https://rishabh876.github.io/RoundedImageView/
github:https://github.com/rishabh876/RoundedImageView
本節(jié)視頻演示 https://www.bilibili.com/video/av62877946/
圓角圖片是很常用的缓苛,但是身為Android開發(fā)萌新的我卻折騰了半天拘领,我只想簡單做個圓角,那么多教程都在講怎么封裝。米母。至于嗎易茬,這種功能寫個P的輪子啊疫蔓,除非有特殊需求腐巢。下面我來介紹一下怎么用RoundedImageView來實(shí)現(xiàn)圓角圖片。
看下本例最終實(shí)現(xiàn)的效果:
先講一下流程:
1腻脏、引入依賴 implementation 'com.rishabhharit.roundedimageview:RoundedImageView:0.8.4'
2鸦泳、在layout的XML里設(shè)置好RoundedImageView的各項(xiàng)顯示屬性
引入依賴
打開項(xiàng)目里的 Gradle Script/build.gradle(Moduel:app),加入下面這行依賴
implementation 'com.rishabhharit.roundedimageview:RoundedImageView:0.8.4'
拷貝圖片到資源目錄
隨便找一張.jpg圖片拷貝到資源目錄的drawable里
設(shè)置layout
打開主界面的layout,顯示其XML內(nèi)容,添加一個RoundedImageView并設(shè)置其顯示屬性永品。說明一點(diǎn)做鹰,RoundedImageView在預(yù)覽模式下不顯示圓角,需要運(yùn)行的時候才能看到鼎姐。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.rishabhharit.roundedimageview.RoundedImageView
android:layout_width="0dp"
android:layout_height="200dp"
app:cornerRadius="16dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/cat" android:layout_marginTop="188dp" android:scaleType="centerCrop"/>
</androidx.constraintlayout.widget.ConstraintLayout>
你可以先復(fù)制上面的XML然后根據(jù)自己的需要修改調(diào)整钾麸。