Android development skills 4: Mobile resolution processing tools and Android development skills
/***** @ Description: Mobile resolution processing tool class * @ author: * @ see: * @ since: * @ copyright©* @ Date: August 13, 2014 */public class DensityUtil {/*** convert the unit from dp to px (pixel) */public static int dip2px (Context context, float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);}/*** based on the resolution of the phone) to dp */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f);}/*** convert the px value to the sp value, ensure that the text size remains unchanged ** @ param pxValue * @ param fontScale (attribute scaledDensity in the DisplayMetrics class) * @ return */public static int px2sp (float pxValue, float fontScale) {return (int) (pxValue/fontScale + 0.5f);}/*** convert the sp value to the px value, ensure that the text size remains unchanged ** @ param spValue * @ param fontScale (attribute scaledDensity in the DisplayMetrics class) * @ return */public static int sp2px (float spValue, float fontScale) {return (int) (spValue * fontScale + 0.5f);}/*** obtain screen density * @ param context * @ return */public static int getScreenDensityDpi (Activity context) {DisplayMetrics metric = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (metric); int density = metric. densityDpi; CLog. d ("DensityUtil density =", density + ""); return density ;} /*** obtain the screen height * @ param context * @ return */public static int getScreenHeight (Activity context) {DisplayMetrics metric = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (metric); int screenHeight = metric. heightPixels; CLog. d ("DensityUtil screenHight =", screenHeight + ""); return screenHeight ;} /*** get screen width * @ param context * @ return */public static int getScreenWidth (Activity context) {DisplayMetrics metric = new DisplayMetrics (); context. getWindowManager (). getdefadisplay display (). getMetrics (metric); int screenWidth = metric. widthPixels; CLog. d ("DensityUtil screenWidth =", screenWidth + ""); return screenWidth ;} /*** return the height of the status bar/notification bar ** @ param activity * @ return */public static int getStatusHeight (Activity activity) {Rect frame = new Rect (); activity. getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int statusBarHeight = frame. top; return statusBarHeight ;}}