Android dp to pix resources.getDimension(R.dimen.xxx) ,kotlin
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_size_dp">20dp</dimen>
<dimen name="my_size_px">20px</dimen>
</resources>
var dp = resources.getDimension(R.dimen.my_size_dp) //20dp
var px = resources.getDimension(R.dimen.my_size_px) //20pix
Log.d("fly", "20dp $dp")
Log.d("fly", "20px $px")
Log.d("fly", "20 ${dip2px(20)}")
private fun dip2px(dps: Int): Int {
return (resources.displayMetrics.density * dps).roundToInt()
}
Android官方dip值到pix值转换:dip2pix,dip2px,dp2px实现_zhangphil的博客-CSDN博客Android官方的dip to pix,dip2pix,dp2px实现网上流传的一个常用的把dip值转换为pix像素值的方法通常是这样的: public static int dip2px(Context context, float dpValue) { float scale = context.getResources().getDisplayMetrics().de..._dip2pxhttps://blog.csdn.net/zhangphil/article/details/80613879Android pix转换为sp_pix转sp_zhangphil的博客-CSDN博客/** * 把pix值转换为sp * * @return */ public static float px2sp(Context context, float pixValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDe_pix转sphttps://blog.csdn.net/zhangphil/article/details/52667671