public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue * @param fontScale(DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(float pxValue, float fontScale) { return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值,保证文字大小不变 * * @param spValue * @param fontScale(DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(float spValue, float fontScale) { return (int) (spValue * fontScale + 0.5f); } /** * 获得屏幕密度 * @param context * @return */ public static int getScreenDensityDpi(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); int density = metric.densityDpi; CLog.d("DensityUtil density=", density+""); return density; } /** * 获得屏幕高度 * @param context * @return */ public static int getScreenHeight(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); int screenHeight = metric.heightPixels; CLog.d("DensityUtil screenHight=", screenHeight+""); return screenHeight; } /** * 获得屏幕宽度 * @param context * @return */ public static int getScreenWidth(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); int screenWidth = metric.widthPixels; CLog.d("DensityUtil screenWidth=",screenWidth+""); return screenWidth; } /** * 返回状态栏/通知栏的高度 * * @param activity * @return */ public static int getStatusHeight(Activity activity) { Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; return statusBarHeight; } }
收藏
0人收藏
- 2018-03-30 18:52:19获取Android设备屏幕宽、高、dpi密度及横竖屏等参数 by demon
- 2018-03-30 18:42:32Java-- join源代码测试 by liuyan814
- 2018-03-30 18:29:19Android自定义 ListView 圆角 by clt
- 2018-03-30 18:10:13Android Http相关辅助类 HttpUtils by clt
- 2018-03-30 17:58:27SD卡相关辅助类 SDCardUtils by Hugh
- 2018-03-30 17:50:01Android单位转换类 DensityUtils by 朱凯迪
- 2018-03-30 17:31:18SharedPreferences封装类SPUtils by liuyan814
- 2018-03-30 17:24:29使用FileUpload上传文件的主要Java代码 by Koon.LY
- 2018-03-30 17:03:11android 网络判断工具类(APN+WIFI) by 朱凯迪
- 2018-03-30 16:43:05Android App在线更新的代码 by 云香水识
- 2018-03-30 18:59:31Android分辨率处理工具类 by 法名空虚
相关聚客文章