一行代碼簡(jiǎn)單實(shí)現(xiàn)Android常用View的圓角邊框

在日常開發(fā)中,我們經(jīng)常需要為各種View實(shí)現(xiàn)圓角邊框,例如圓角邊框的ImageView、圓角邊框的TextView、圓角邊框的ConstraintLayout等等。通常情況下我們會(huì)使用shape drawable或自定義View去實(shí)現(xiàn),使用shape drawable會(huì)造成項(xiàng)目中存在大量的drawable文件,使用自定義View會(huì)造成相同代碼的冗余,所以我做了一個(gè)開源Library項(xiàng)目,方便大家集成后,一行代碼簡(jiǎn)單實(shí)現(xiàn)Android常用View的圓角邊框。

1.集成方式:

allprojects {
    repositories {
        ...
        maven { url 'https://www.jitpack.io' }
    }
}
 implementation 'com.github.Arcns.arc-fast:rounded:1.23.1'
// 可選:如果你需要使用RoundedConstraintLayout,但你的項(xiàng)目中未引入ConstraintLayout,那么你還需要:
 implementation 'androidx.constraintlayout:constraintlayout:yourversion'

2.使用方式

方式一:通過Library內(nèi)置的View實(shí)現(xiàn)圓角邊框

為方便使用,Library中內(nèi)置了RoundedView、RoundedImageView、RoundedConstraintLayout、RoundedTextView四款支持圓角邊框的View,適應(yīng)絕大多數(shù)的使用場(chǎng)景。以RoundedView為例(其他View的使用方式相同),使用方式如下:

<com.arc.fast.view.rounded.RoundedView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:rounded_background_color="@android:color/holo_red_light" //背景顏色
            app:rounded_border_color="@android:color/holo_blue_light" //邊框顏色
            app:rounded_border_size="2dp"http://邊框大小
            app:rounded_radius="16dp" />//圓角大小

-支持的圓角邊框參數(shù)

參數(shù) 說明 類型 默認(rèn)值
rounded_radius 圓角的大小,優(yōu)先度最低 dimension,例如100dp 0
rounded_radius_top_left 左上的圓角大小 dimension,例如100dp 0
rounded_radius_top_right 右上的圓角大小 dimension,例如100dp 0
rounded_radius_bottom_left 左下的圓角大小 dimension,例如100dp 0
rounded_radius_bottom_right 右下的圓角大小 dimension,例如100dp 0
rounded_background_color 背景顏色 color,例如#FFFFFF
rounded_border_color 邊框顏色 color,例如#FFFFFF
rounded_border_size 邊框大小 dimension,例如10dp 0
方式二:通過IRoundedView實(shí)現(xiàn)任意的圓角邊框控件

如果Library中內(nèi)置的View無法滿足項(xiàng)目的使用需求,那么你也可以通過IRoundedView實(shí)現(xiàn)任意的圓角邊框控件。

要讓控件支持圓角邊框只需4個(gè)步驟:
第一步:在控件中實(shí)現(xiàn)IRoundedView
第二步:實(shí)現(xiàn)IRoundedView的參數(shù)
第三步:在init中調(diào)用initRoundedRadius(context, attrs)獲取xml中配置的參數(shù)
第四步:在draw中調(diào)用onDrawBefore和onDrawAfter

具體實(shí)現(xiàn)方式如下:

class CustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr), IRoundedView { //第一步:在控件中實(shí)現(xiàn)IRoundedView

   // 第二步:實(shí)現(xiàn)IRoundedView的參數(shù)
    override var _config = RoundedViewConfig()
    override var _temporarilyConfig: RoundedViewConfig? = null

    init {
        // 第三步:在init中調(diào)用initRoundedRadius(context, attrs)獲取xml中配置的參數(shù)
        if (attrs != null) initRoundedRadius(context, attrs)
    }

  // 第四步:在draw中調(diào)用onDrawBefore和onDrawAfter
    override fun draw(canvas: Canvas) {
        onDrawBefore(canvas)
        super.draw(canvas)
        onDrawAfter(canvas)
    }
}

// 使用方式與Library內(nèi)置的View相同
<yourpackage.CustomView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:rounded_background_color="@android:color/holo_red_light" //背景顏色
            app:rounded_border_color="@android:color/holo_blue_light" //邊框顏色
            app:rounded_border_size="2dp"http://邊框大小
            app:rounded_radius="16dp" />//圓角大小

項(xiàng)目地址:
https://github.com/Arcns/arc-fast

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容