Android切圓角的幾種常見方式總結(jié)
Android 中有哪些可以切圓角的實(shí)現(xiàn)方式呢?
本文總結(jié)一下常用的方式。
以下內(nèi)容分為以下幾部分:
- 利用 Drawable 的 shape xml 實(shí)現(xiàn)
- CardView 實(shí)現(xiàn)圓角
- fresco 中的 SimpleDraweeView 實(shí)現(xiàn)圓角
- 利用 View 的 ViewOutlineProvider 實(shí)現(xiàn)圓角
- 總結(jié)
1. 利用 Drawable 的 shape xml 實(shí)現(xiàn)
很多時(shí)候,我們可以自定義一些 drawable , 代碼如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="15dp"/> <solid android:color="#FFFFFF"/> <stroke android:width="1dp" android:color="#EBEBEB"/> </shape>
其中,corners 就是我們實(shí)現(xiàn)的圓角,這里指定圓角的半徑為 15dp。
solid 是指填充色,這里為白色;
stroke 為drawable 的邊緣寬度和顏色設(shè)置,這里為 1dp 顏色比白色黑一點(diǎn)。
如果知識想要 「圓角」的話,可以不需要指定 stroke
然后在我們需要的 View 上,設(shè)置它的 background 為該 drawable 即可.
效果為:
drawable 圓角
本質(zhì)是在 background 上加了圓角。
2. CardView 的圓角
CardView 是自帶圓角實(shí)現(xiàn)的,我們只需要在它的定義中加一句 app:cardCornerRadius="8dp" 即可。
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.cardview.widget.CardView android:layout_width="256dp" android:layout_height="128dp" app:cardBackgroundColor="#0084FF" app:cardCornerRadius="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
設(shè)置該 CardView 圓角半徑為 16dp,
效果圖如下:
cardView 圓角
3. fresco 中的 SimpleDraweeView
fresco 是一個(gè)強(qiáng)大的圖片庫,里面的 SimpleDraweeView 常用來加載圖片。
SimpleDraweeView 實(shí)現(xiàn)了很多功能,其中一個(gè)就是實(shí)現(xiàn)了圓角屬性 roundedCornerRadius
實(shí)現(xiàn)代碼:
<com.facebook.drawee.view.SimpleDraweeView android:layout_width="256dp" android:layout_height="128dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:actualImageScaleType="centerCrop" app:roundedCornerRadius="3dp" />
這里設(shè)置圖片圓形邊角為 3dp
實(shí)現(xiàn)效果為:
SimpleDraweeView 圓角
4. 利用 View 的 ViewOutlineProvider 實(shí)現(xiàn)圓角
這種實(shí)現(xiàn)方式,本質(zhì)上是修改了 View 的輪廓。
代碼實(shí)現(xiàn):
itemView.outlineProvider = object : ViewOutlineProvider() { override fun getOutline(view: View, outline: Outline) { outline.setRoundRect(0, 0, view.width, view.height, 5.dp.toFloat()) } } // 打開開關(guān) itemView.clipToOutline = true
為整個(gè) View 添加上圓角。
實(shí)現(xiàn)效果為:
outlineProvider 圓角
這樣的好處是,不需要給里面的子 view 設(shè)置圓角,在最外層的 View 設(shè)置為圓角即可。
更大的好處是:比使用了第一種方式 drawable 的 xml 少了一層過度繪制。因?yàn)槭∪チ嗽O(shè)置的 background
利用 ViewOutlineProvider 的實(shí)現(xiàn)圓角,本質(zhì)上是在 View 的畫布上畫了一個(gè)圓角的矩形。
setRoundRect(xxx)
同時(shí) outline 還可以畫其他的一些內(nèi)容。
outline.setRect(xxx)// 畫矩形 outline.setRoundRect(xxx)// 畫圓角矩形 outline.setOval(xxx) // 畫橢圓
同時(shí),因?yàn)?outline.setRoundRect(0, 0, view.width, view.height, 5.dp.toFloat()) 是在一個(gè)矩形上畫的圓角。因?yàn)?,?dāng)我們的矩形減小或增大時(shí),有些圓角是沒有區(qū)域可畫,會(huì)形成部分圓角存在的情況。
既然提到了 ViewOutlineProvider , 那就得提一下 StateListAnimator 這個(gè)動(dòng)畫得效果, 感興趣得自己去搜索一下??蓞⒖?StateListAnimator
5.總結(jié)
上面總結(jié)了一下常見的 Android 中實(shí)現(xiàn)圓角的方式,在使用過程中,怎么方便怎么來。
我個(gè)人最近比較喜歡用 ViewOutlineProvider, 對輪廓進(jìn)行剪切,高效且方便。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對我們的支持。
欄 目:Android
下一篇:Flutter 實(shí)現(xiàn)下拉刷新上拉加載的示例代碼
本文標(biāo)題:Android切圓角的幾種常見方式總結(jié)
本文地址:http://mengdiqiu.com.cn/a1/Android/9015.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文