Kotlin-經常使用擴展函數

擴展函數是什麼這裏就不過多解釋了,總結了一下項目中經常使用的一些擴展函數若是有須要的能夠在這裏下載 github.com/shiweibsw/A…git

使用方式

選擇你須要的擴展函數類,將對應的.kt文件拷貝到項目中便可。github

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 均爲可選參數 使用前能夠提早設置好placeholderapp

說一下loadImageByProportion這個擴展函數的用法 在項目開發彙總常常會遇到這樣的界面好比網易新聞中框架

圖中紅色框的圖片設計的同窗標註爲寬度爲屏寬的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) 擴展函數就是專門解決這個問題的 解釋一下兩個參數:less

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

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。

舉個例子好比這個界面的佈局一般咱們會使用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。

相關文章
相關標籤/搜索