常用单位转化
import android.content.Context
import android.util.TypedValue
object DisplayUtils {
fun dp2px(context: Context , dpVal:Float): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal,context.resources.displayMetrics).toInt()
}
fun sp2px(context:Context,spVal:Float):Int{
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,context.resources.displayMetrics).toInt()
}
fun px2dp(context: Context,pxValue:Float):Int{
val scale = context.resources.displayMetrics.density
return (pxValue/scale+0.5F).toInt()
}
fun px2sp(context:Context,pxValue:Float):Int{
val fontScale = context.resources.displayMetrics.scaledDensity
return (pxValue/fontScale + 0.5).toInt()
}
}