Back to project page FrameLite.
The source code is released under:
GNU General Public License
If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.miku.framelite.utils; /*from www .j av a 2 s . co m*/ import java.lang.reflect.Method; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.util.DisplayMetrics; import android.view.WindowManager; public class DimensionUtils { private static final String TAG = DimensionUtils.class.getSimpleName(); private DimensionUtils(){/** singleInstance **/} /** * ??????????? dp ?????? ???? px(???) */ public static int dip2px(Context context, float dpValue) { float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * ??????????? px(???) ?????? ???? dp */ public static int px2dip(Context context, float pxValue) { float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * ???????????sp ?????? ???? px(???) */ public static int sp2px(Context context, float spValue) { float scale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * scale + 0.5f); } /** * ??????????? px(???) ?????? ???? sp */ public static int px2sp(Context context,float pxValue){ float scale=context.getResources().getDisplayMetrics().scaledDensity; return (int)(pxValue/scale+0.5f); } /** * ?????????? * * @param context * @return ??[??,??] */ public static int[] getDisplayScreenResolution(Context context) { int ver = Build.VERSION.SDK_INT; int screen[] = new int[2]; DisplayMetrics dm = new DisplayMetrics(); WindowManager wmanager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); android.view.Display display = wmanager.getDefaultDisplay(); display.getMetrics(dm); screen[0] = dm.widthPixels; screen[1] = dm.heightPixels; Log.e(TAG, "Run1 first get resolution:" + dm.widthPixels + " * " + dm.heightPixels + ", ver " + ver); if (ver < 13) { screen[1] = dm.heightPixels; } else if (ver == 13) { try { Method mt = display.getClass().getMethod("getRealHeight"); screen[1] = (Integer) mt.invoke(display); } catch (Exception e) { e.printStackTrace(); } } else if (ver > 13) { try { Method mt = display.getClass().getMethod("getRealMetrics",DisplayMetrics.class); mt.invoke(display, dm); screen[0] = dm.widthPixels; screen[1] = dm.heightPixels; } catch (Exception e) { e.printStackTrace(); } } Log.e(TAG, "Run2 Calibration resolution:" + screen[0] + " * " + screen[1]); return screen; } }