在日常開發(fā)中,我們經(jīng)常需要用到dp2px、sp2px、常用Resource值(string/color/drawable)獲取等操作,如果全部使用Android自帶的獲取方法來實現(xiàn),會比較冗余,且它們絕大多數(shù)都需要Context,在有些場景會讓我們不得不層層傳遞Context,顯得非常不優(yōu)雅。于是我做了一個開源Library項目,方便大家集成后,不需要再傳入Context,就可以一行代碼簡單實現(xiàn)Android dp2px、sp2px、常用Resource值(string/color/drawable)獲取。
使用方式:
1、集成Library
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
implementation 'com.github.Arcns.arc-fast:core:1.23.1'
2、使用方式
(1)dp2px、px2dp
| 方法 | 功能 | 用法 |
|---|---|---|
| Float.dpToPx | 把dp轉(zhuǎn)換為px(Float格式) | 100f.dpToPx |
| Int.dpToPx | 把dp轉(zhuǎn)換為px(Int格式) | 100.dpToPx |
| Float.pxToDp | 把px轉(zhuǎn)換為dp(Float格式) | 100f.pxToDp |
| Int.pxToDp | 把px轉(zhuǎn)換為dp(Int格式) | 100.pxToDp |
(2)sp2px
| 方法 | 功能 | 用法 |
|---|---|---|
| Float.spToPx | 把sp轉(zhuǎn)換為px(Float格式) | 100f.spToPx |
| Int.spToPx | 把sp轉(zhuǎn)換為px(Int格式) | 100.spToPx |
(3)獲取String資源
| 方法 | 功能 | 用法 |
|---|---|---|
| Int.resToString | 通過StringRes獲取String值 | R.string.test.resToString |
| Int.resToStringOrNull | 通過StringRes獲取String值,獲取失敗時返回null | R.string.test.resToStringOrNull |
| Int.resToString(vararg values: Any?) | 通過StringRes獲取String值,并替換格式參數(shù)(例如%1$s) | R.string.test.resToString("1","2") |
| Int.resToStringOrNull(vararg values: Any?) | 通過StringRes獲取String值,并替換格式參數(shù)(例如%1$s),獲取失敗時返回null | R.string.test.resToStringOrNull("1","2") |
(4)Drawable資源
| 方法 | 功能 | 用法 |
|---|---|---|
| Int.resToDrawable | 通過DrawableRes獲取Drawable值 | R.drawable.test.resToDrawable |
| Int.resToDrawableOrNull | 通過DrawableRes獲取Drawable值,獲取失敗時返回null | R.drawable.test.resToDrawableOrNull |
| Drawable.applyTint(color: Int?) | 為Drawable實現(xiàn)著色效果 | R.drawable.test.applyTint(0x24000000) |
| Drawable.applyRipple(context: Context,rippleColor: Int? = null,rippleColorStateList: ColorStateList? = null) | 為Drawable實現(xiàn)Ripple效果,ColorStateList優(yōu)先級高于rippleColor | R.drawable.test.applyRipple(context,0x00000000) |
(5)Color資源
| 方法 | 功能 | 用法 |
|---|---|---|
| Int.resToColor | 通過ColorRes獲取Color值 | R.color.test.resToColor |
| Int.resToColorOrNull | 通過ColorRes獲取Color值,獲取失敗時返回null | R.color.test.resToColorOrNull |
| String.hexToColor | 把Hex Color轉(zhuǎn)換為Color(Int格式) | "#00000000".hexToColor |
| String.hexToColorOrNull | 把Hex Color轉(zhuǎn)換為Color(Int格式),獲取失敗時返回null | "#000000".hexToColorOrNull |
| Int.colorToHex | 把Color轉(zhuǎn)換為Hex Color(String格式) | 0x00000000.colorToHex |
| Int.colorToHexOrNull | 把Color轉(zhuǎn)換為Hex Color(String格式),獲取失敗時返回null | 0x00000000.colorToHexOrNull |
| Int.lightColorNess | 獲取Color的亮度(0-1) | 0x00000000.lightColorNess |
| Int.isLightColor | 判斷Color是否為亮色調(diào) | 0x00000000.isLightColor |
(6)Dimension資源
| 方法 | 功能 | 用法 |
|---|---|---|
| Int.resToDimenValue | 通過DimenRes獲取Dimen值 | R.dimen.test.resToDimenValue |
| Int.resToDimenValueOrNull | 通過DimenRes獲取Dimen值,獲取失敗時返回null | R.dimen.test.resToDimenValueOrNull |
(7)Attr資源(Attr的Res資源與Context的theme相關(guān),因此此處必須手動傳入Attr對應(yīng)的Context)
| 方法 | 功能 | 用法 |
|---|---|---|
| Context.getAttributeResource(attr: Int, defResId: Int? = null) | 通過AttrResId獲取Res資源 | context.getAttributeResource(R.attr.test) |
| Context.selectableItemBackgroundRes | 獲取selectableItemBackground資源 | context.selectableItemBackgroundRes |
| Context.selectableItemBackgroundBorderlessRes | 獲取selectableItemBackgroundBorderless資源 | context.selectableItemBackgroundBorderlessRes |
| Context.actionBarItemBackgroundRes | 獲取actionBarItemBackground資源 | context.actionBarItemBackgroundRes |