dp与px相互转换
package com.xt.deeptest5g.Utils;import android.content.res.Resources;/*** author : ZJS* e-mail : 15574564770@163.com* date : 20-7-30下午9:02* desc : dp與px相互轉換* version : 1.0*/
public class DensityUtil {public float density;public DensityUtil() {density = Resources.getSystem().getDisplayMetrics().density;}/*** 根據手機的分辨率從 dp 的單位 轉成為 px(像素)** @param dpValue 虛擬像素* @return 像素*/public static int dp2px(float dpValue) {return (int) (0.5f + dpValue * Resources.getSystem().getDisplayMetrics().density);}/*** 根據手機的分辨率從 px(像素) 的單位 轉成為 dp** @param pxValue 像素* @return 虛擬像素*/public static float px2dp(int pxValue) {return (pxValue / Resources.getSystem().getDisplayMetrics().density);}/*** 根據手機的分辨率從 dp 的單位 轉成為 px(像素)** @param dpValue 虛擬像素* @return 像素*/public int dip2px(float dpValue) {return (int) (0.5f + dpValue * density);}/*** 根據手機的分辨率從 px(像素) 的單位 轉成為 dp** @param pxValue 像素* @return 虛擬像素*/public float px2dip(int pxValue) {return (pxValue / density);}
}
總結
- 上一篇: 笔记本电脑配置主要看哪几个参数?
- 下一篇: 手持ActivityLifecycleC