Kotlin-常用扩展函数

扩展函数是什么这里就不过多解释了,总结了一下项目中常用的一些扩展函数如果有需要的可以在这里下载 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这个扩展函数的用法 在项目开发汇总经常会遇到这样的界面比如网易新闻中

图中红色框的图片设计的同学标注为宽度为屏宽的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。

举个例子比如这个界面的布局通常我们会使用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。

猜你喜欢

转载自juejin.im/post/5b4317d3e51d45194908f6cd