Skip to content

Commit

Permalink
'新增BackgroundLibrary,添加sp2px,sp2px,px2dp'
Browse files Browse the repository at this point in the history
  • Loading branch information
oooo7777777 committed Jul 13, 2021
1 parent a44c8e6 commit de6709d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
6 changes: 3 additions & 3 deletions vlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 326
versionName "3.2.6"
versionCode 327
versionName "3.2.7"
}

compileOptions {
Expand Down Expand Up @@ -55,7 +55,7 @@ dependencies {
api "com.scwang.smart:refresh-header-material:2.0.3"
api "com.scwang.smart:refresh-footer-classics:2.0.0-alpha-1"
api "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4"
api "com.noober.background:core:1.6.5"//BackgroundLibrary (通过标签直接生成shape,无需再写shape.xml)
api "com.github.JavaNoober.BackgroundLibrary:libraryx:1.7.2"//BackgroundLibrary (通过标签直接生成shape,无需再写shape.xml)

}

48 changes: 42 additions & 6 deletions vlibrary/src/main/java/com/v/base/utils/BaseUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.v.base.utils

import android.app.Application
import android.util.TypedValue
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -35,17 +36,52 @@ fun <T : ViewModel?> getApplicationViewModel(
}



/**
* dp转px
*/

fun Int.dp2px(): Int =
run {
val scale = BaseApplication.getContext().resources.displayMetrics.density
return (this * scale + 0.5f).toInt()
}
//fun Int.dp2px(): Int =
// run {
// val scale = BaseApplication.getContext().resources.displayMetrics.density
// return (this * scale + 0.5f).toInt()
// }


/**
* dp2px
*/
fun Int.dp2px(): Int = run {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(), BaseApplication.getContext().resources.displayMetrics
).toInt()
}

/**
* sp2px
*/
fun Int.sp2px(): Int = run {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
this.toFloat(), BaseApplication.getContext().resources.displayMetrics
).toInt()
}

/**
* px2dp
*/
fun Int.px2dp(): Int = run {
val scale: Float = BaseApplication.getContext().resources.displayMetrics.density
return (this / scale + 0.5f).toInt()
}

/**
* px2sp
*/
fun Int.px2sp(): Int = run {
val fontScale: Float = BaseApplication.getContext().resources.displayMetrics.scaledDensity
return (this / fontScale + 0.5).toInt()
}

/**
* 随机颜色
Expand Down

0 comments on commit de6709d

Please sign in to comment.