/* * Copyright (C) 2016 android@19code.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.code19.library; import android.annotation.TargetApi; import android.content.Context; import android.content.res.Resources; import android.os.Build; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.Display; import android.view.WindowManager; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * Create by h4de5ing 2016/5/7 007 */ public class DensityUtil { public static int dip2px(Context c, float dpValue) { final float scale = c.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int dip2sp(Context c, float dpValue) { return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, c.getResources().getDisplayMetrics())); } public static int px2dip(Context c, float pxValue) { final float scale = c.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } public static int px2sp(Context c, float pxValue) { float fontScale = c.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } public static int sp2px(Context c, float spValue) { float fontScale = c.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } public static int sp2dip(Context c, float spValue) { return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, c.getResources().getDisplayMetrics())); } public static int getScreenW(Context c) { return c.getResources().getDisplayMetrics().widthPixels; } public static int getScreenH(Context c) { return c.getResources().getDisplayMetrics().heightPixels; } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static int getScreenRealH(Context context) { int h; WindowManager winMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = winMgr.getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= 17) { display.getRealMetrics(dm); h = dm.heightPixels; } else { try { Method method = Class.forName("android.view.Display").getMethod("getRealMetrics", DisplayMetrics.class); method.invoke(display, dm); h = dm.heightPixels; } catch (Exception e) { display.getMetrics(dm); h = dm.heightPixels; } } return h; } public static int getStatusBarH(Context context) { Class<?> c; Object obj; Field field; int statusBarHeight = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); int x = Integer.parseInt(field.get(obj).toString()); statusBarHeight = context.getResources().getDimensionPixelSize(x); } catch (Exception e1) { e1.printStackTrace(); } return statusBarHeight; } public static int getNavigationBarrH(Context c) { Resources resources = c.getResources(); int identifier = resources.getIdentifier("navigation_bar_height", "dimen", "android"); return resources.getDimensionPixelOffset(identifier); } }