扩展函数是什么这里就不过多解释了,总结了一下项目中经常使用的一些扩展函数若是有须要的能够在这里下载 github.com/shiweibsw/A…git
选择你须要的扩展函数类,将对应的.kt文件拷贝到项目中便可。github
目前的项目中大多数使用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这个扩展函数的用法 在项目开发汇总常常会遇到这样的界面好比网易新闻中框架
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
名称 | 描述 |
---|---|
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。
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)
其余类的一些扩展这里就不细说了,有须要的请下载源码并将相应的扩展类导入到项目中便可。
名称 | 描述 |
---|---|
view2Bitmap | View 转 bitmap |
bottomMargin | 底部Margin |
leftMargin | 左侧Margin |
topMargin | 上部Margin |
rightMargin | 右侧Margin |
名称 | 描述 |
---|---|
toast | 展现toast |
centerToast | 中心展现toast |
dp2px | dp转px |
px2dp | px转dp |
sp2px | sp转px |
px2sp | px转sp |
getScreenWidth | 屏幕宽度 |
getScreenHeight | 屏幕高度 |
openWirelessSettings | 打开网络设置界面 |
isConnected | 网络是否链接 |
isMobileData | 判断网络是不是移动数据 |
名称 | 描述 |
---|---|
screenShot | 屏幕截图 |
isPortrait | 是否竖屏 |
isLandscape | 是否横屏 |
setPortrait | 设置竖屏 |
setLandscape | 设置横屏 |
setFullScreen | 设置全屏 |
showKeyboard | 显示软键盘 |
hideKeyboard | 隐藏软键盘 |
名称 | 描述 |
---|---|
scale | bitmap 缩放 |
名称 | 描述 |
---|---|
getBitmap | file 转 bitmap |
这里感谢一下这位大神写的经常使用工具类库,从中参考了不少函数的实现方式。
这套代码还在不断的加入新的扩展函数,若是你也有比较实用的扩展函数欢迎提交PR。
github.com/shiweibsw/A… 欢迎fork和start。