扩展函数是什么这里就不过多解释了, 总结了一下项目中常用的一些扩展函数如果有需要的可以在这里下载 https://github.com/shiweibsw/Android-kotlin-extend-utils
使用方式
选择你需要的扩展函数类, 将对应的. 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 |
这里感谢一下这位大神写的常用工具类库, 从中参考了很多函数的实现方式.
https://github.com/Blankj/AndroidUtilCode
这套代码还在不断的加入新的扩展函数, 如果你也有比较实用的扩展函数欢迎提交 PR.
https://github.com/shiweibsw/Android-kotlin-extend-utils 欢迎 fork 和 start.
来源: https://juejin.im/post/5b4317d3e51d45194908f6cd