package com.xty.common

import android.content.Context
import android.util.DisplayMetrics

/**
 * 尺寸转换 一般用px专dp
 */
class SizeUtil {
    //px转换为dp
    companion object{

        fun px2dp(context: Context, pxValue: Float): Float {
            val density: Float = context.resources.displayMetrics.density
            return (pxValue / density + 0.5f)
        }

        //dp转换为px
        fun dp2px(context: Context, dpValue: Float): Int {
            val density: Float = context.resources.displayMetrics.density
            return (dpValue * density + 0.5f).toInt()
        }

        //px转换为sp
        fun px2sp(context: Context, pxValue: Float): Int {
            val scaledDensity: Float = context.resources.displayMetrics.scaledDensity
            return (pxValue / scaledDensity + 0.5f).toInt()
        }

        //sp转换为px
        fun sp2px(context: Context, spValue: Float): Int {
            val scaledDensity: Float = context.resources.displayMetrics.scaledDensity
            return (spValue * scaledDensity + 0.5f).toInt()
        }

        //sp转换为px
        fun sp2pxF(context: Context, spValue: Float): Float {
            val scaledDensity: Float = context.resources.displayMetrics.scaledDensity
            return (spValue * scaledDensity + 0.5f)
        }
    }



}