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

名称描述
view2BitmapView 转 bitmap
bottomMargin底部Margin
leftMargin左侧Margin
topMargin上部Margin
rightMargin右侧Margin

4.ContextExtends

名称描述
toast展示toast
centerToast中心展示toast
dp2pxdp转px
px2dppx转dp
sp2pxsp转px
px2sppx转sp
getScreenWidth屏幕宽度
getScreenHeight屏幕高度
openWirelessSettings打开网络设置界面
isConnected网络是否连接
isMobileData判断网络是否是移动数据

5.ActivityExtends

名称描述
screenShot屏幕截图
isPortrait是否竖屏
isLandscape是否横屏
setPortrait设置竖屏
setLandscape设置横屏
setFullScreen设置全屏
showKeyboard显示软键盘
hideKeyboard隐藏软键盘

6.BitmapExtends

名称描述
scalebitmap 缩放

7.FileExtends

名称描述
getBitmapfile 转 bitmap

这里感谢一下这位大神写的常用工具类库,从中参考了很多函数的实现方式。

github.com/Blankj/Andr…

这套代码还在不断的加入新的扩展函数,如果你也有比较实用的扩展函数欢迎提交PR。

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值