package me.wcy.music.utils; import android.content.Context; import android.view.WindowManager; /** * 工具类 * Created by hzwangchenyan on 2016/1/6. */ public class ScreenUtils { private static Context sContext; public static void init(Context context) { sContext = context.getApplicationContext(); } public static int getScreenWidth() { WindowManager wm = (WindowManager) sContext.getSystemService(Context.WINDOW_SERVICE); return wm.getDefaultDisplay().getWidth(); } /** * 获取状态栏高度 */ public static int getSystemBarHeight() { int result = 0; int resourceId = sContext.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = sContext.getResources().getDimensionPixelSize(resourceId); } return result; } public static int dp2px(float dpValue) { final float scale = sContext.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int px2dp(float pxValue) { final float scale = sContext.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } public static int sp2px(float spValue) { final float fontScale = sContext.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } }