Kotlin-常用擴充套件函式

Knight_Davion發表於2019-03-02

擴充套件函式是什麼這裡就不過多解釋了,總結了一下專案中常用的一些擴充套件函式如果有需要的可以在這裡下載 github.com/shiweibsw/A…

使用方式

選擇你需要的擴充套件函式類,將對應的.kt檔案拷貝到專案中即可。

1 ImageView的擴充套件

目前的專案中大多數使用Glide作為圖片載入框架,所以以下的這些擴充套件也是通過Glide完成的,如果你正在使用其他圖片載入框架請替換函式中Glide相關的程式碼即可,注適用於Glide版本為4.+

名稱 描述
loadImage 載入圖片
loadCircleImage 載入圓形圖片
loadRoundCornerImage 載入圓角圖片
loadImageByProportion 按照圖片的寬高比載入
loadClear 取消載入
/**
 * 載入圖片
 */
fun ImageView.loadImage(context: Context, path: String, placeholder: Int = R.mipmap.ic_launcher, useCache: Boolean =false) {
    var options = getOptions(placeholder, useCache)
    Glide.with(context).load(path).apply(options).into(this)
}

/**
 * 載入圓形圖片
 */
fun ImageView.loadCircleImage(context: Context, path: String, placeholder: Int = R.mipmap.ic_launcher, useCache: Boolean = false) {
    var options = getOptions(placeholder, useCache)
    options.circleCrop()
    Glide.with(context).load(path).apply(options).into(this)
}

/**
 * 載入圓角圖片
 */
fun ImageView.loadRoundCornerImage(context: Context, path: String, roundingRadius: Int = 32, placeholder: Int = R.mipmap.ic_launcher, useCache: Boolean = false) {
    var options = getOptions(placeholder, useCache)
    Glide.with(context).load(path).apply(RequestOptions.bitmapTransform(RoundedCorners(roundingRadius))).apply(options).into(this)
}
複製程式碼

引數placeholder 及useCache 均為可選引數 使用前可以提前設定好placeholder

說一下loadImageByProportion這個擴充套件函式的用法 在專案開發彙總經常會遇到這樣的介面比如網易新聞中

Kotlin-常用擴充套件函式
圖中紅色框的圖片設計的同學標註為寬度為屏寬的1/3,寬高比為16:9,對於這種按比例顯示的圖片我們怎麼辦,首先不可能寫死寬高值,這樣的話適配是個大麻煩。第二種辦法是通過自定義ImageView實現,寬度可以先獲取螢幕寬度在賦值給ImageView的測量寬度,對於16:9寬高比可以複寫onMeasure()

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    val width = View.getDefaultSize(0, widthMeasureSpec)
    setMeasuredDimension(width, width * 9 / 16)
}
複製程式碼

兩種辦法實現起來都比較麻煩。 loadImageByProportion(widthProportion: Float, heightProportion: Float) 擴充套件函式就是專門解決這個問題的 解釋一下兩個引數:

widthProportion:相對於螢幕寬度的比例取值範圍為0.0f-1.0f,當widthProportion=1.0時,ImageView的寬度為螢幕寬度

heightProportion:相對於圖片寬度的顯示比例

對於上圖的解決辦法可以採用如下設定即可

imageView.loadImageByProportion(1/3f, 9/16f)

imageView為普通的ImageView控制元件

注意:imageView在xml佈局中的width及height屬性必須為WRAP_CONTENT

2 TextView的擴充套件

名稱 描述
setColor 設定顏色
setDrawableLeft 左側Drawable
setDrawableTop 上部Drawable
setDrawableRight 右側Drawable
setDrawableBottom 下部Drawable
/**
 * 設定顏色直接使用colors.xml中定義的顏色即可
 */
fun TextView.setColor(resId: Int) {
    this.setTextColor(resources.getColor(resId))
}

fun TextView.setDrawableLeft(resId: Int) {
    var drawable = this.context.resources.getDrawable(resId)
    drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
    this.setCompoundDrawables(drawable, null, null, null)
}
複製程式碼

setColor 適用於程式中動態修改字型顏色,不用每次都寫resources.getColor(resId)這樣的程式碼 setDrawableLeft(resId: Int)適用於動態修改設定在textView周圍的drawable。

Kotlin-常用擴充套件函式
舉個例子比如這個介面的佈局通常我們會使用TextView 並設定drawableTop屬性來完成,那麼如果我想在點選紅色框按鈕後改變其上部圖片怎麼辦?改變圖片的程式碼如下

 var drawable = this.context.resources.getDrawable(resId)
drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
this.setCompoundDrawables(null, drawable, null, null)
複製程式碼

我們把這段程式碼封裝起來就成了TextView的擴充套件函式setDrawableTop(resId: Int)

其他類的一些擴充套件這裡就不細說了,有需要的請下載原始碼並將相應的擴充套件類匯入到專案中即可。

3.ViewExtends

名稱 描述
view2Bitmap View 轉 bitmap
bottomMargin 底部Margin
leftMargin 左側Margin
topMargin 上部Margin
rightMargin 右側Margin

4.ContextExtends

名稱 描述
toast 展示toast
centerToast 中心展示toast
dp2px dp轉px
px2dp px轉dp
sp2px sp轉px
px2sp px轉sp
getScreenWidth 螢幕寬度
getScreenHeight 螢幕高度
openWirelessSettings 開啟網路設定介面
isConnected 網路是否連線
isMobileData 判斷網路是否是移動資料

5.ActivityExtends

名稱 描述
screenShot 螢幕截圖
isPortrait 是否豎屏
isLandscape 是否橫屏
setPortrait 設定豎屏
setLandscape 設定橫屏
setFullScreen 設定全屏
showKeyboard 顯示軟鍵盤
hideKeyboard 隱藏軟鍵盤

6.BitmapExtends

名稱 描述
scale bitmap 縮放

7.FileExtends

名稱 描述
getBitmap file 轉 bitmap

這裡感謝一下這位大神寫的常用工具類庫,從中參考了很多函式的實現方式。

github.com/Blankj/Andr…

這套程式碼還在不斷的加入新的擴充套件函式,如果你也有比較實用的擴充套件函式歡迎提交PR。

github.com/shiweibsw/A… 歡迎fork和start。

相關文章