DisplayUtil.懂得都懂

Crazy Coding

import android.content.Context
import android.content.res.Resources
import android.graphics.Point
import android.util.TypedValue
import android.view.WindowManager

/**
* @ClassName DisplayUtil
* @Description TODO
* @Author Kolin Zhao
* @Date 2021/10/15 15:05
* @Version 1.0
*/
object DisplayUtil {
fun dp2px(dp: Float, resources: Resources): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics)
.toInt()
}

fun getDisplayWithInPx(context: Context): Int {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val size = Point()
display.getSize(size)
return size.x
}

fun getDisplayHeightInPx(context: Context): Int {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val size = Point()
display.getSize(size)
return size.y
}
}