当前位置 : 主页 > 编程语言 > java >

【Android】DisplayUtil

来源:互联网 收集:自由互联 发布时间:2022-06-30
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 * @Descrip


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
}
}


网友评论