File tree Expand file tree Collapse file tree 1 file changed +7
-2
lines changed
lite/src/main/java/com/pengxh/kt/lite/extensions Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Original file line number Diff line number Diff line change 1
1
package com.pengxh.kt.lite.extensions
2
2
3
3
import android.content.Context
4
+ import android.util.TypedValue
4
5
import java.math.RoundingMode
5
6
import java.text.DecimalFormat
6
7
8
+ private val decimalFormat = DecimalFormat (" #" )
9
+
7
10
/* *
8
11
* px转dp
9
12
*/
@@ -22,8 +25,10 @@ fun Float.dp2px(context: Context): Float {
22
25
* sp转px
23
26
*/
24
27
fun Float.sp2px (context : Context ): Float {
25
- val decimalFormat = DecimalFormat (" #" )
28
+ val floatValue = TypedValue .applyDimension(
29
+ TypedValue .COMPLEX_UNIT_SP , this , context.resources.displayMetrics
30
+ )
26
31
decimalFormat.roundingMode = RoundingMode .CEILING
27
- val result = decimalFormat.format(this / context.getScreenDensity() )
32
+ val result = decimalFormat.format(floatValue )
28
33
return result.toFloat()
29
34
}
You can’t perform that action at this time.
0 commit comments