/** * 工具类 */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 getScreenHeight() {        WindowManager wm = (WindowManager) sContext.getSystemService(Context.WINDOW_SERVICE);        return wm.getDefaultDisplay().getHeight();    }    /**     * 获取状态栏高度     */    public static int getStatusBarHeight() {        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);    }    /**     * 设置屏幕透明度     * @param activity     * @param f     */    public static void setScreentAlpha(Activity activity, float f) {        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();        lp.alpha = f;        if (f == 1) {            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,可能出现黑屏的bug        }else {            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);        }        activity.getWindow().setAttributes(lp);    }    /**     * 判断是否是全面屏     */    private volatile static boolean mHasCheckAllScreen;    private volatile static boolean mIsAllScreenDevice;    public static boolean isAllScreenDevice(Context context) {        if (mHasCheckAllScreen) {            return mIsAllScreenDevice;        }        mHasCheckAllScreen = true;        mIsAllScreenDevice = false;        // 低于 API 21的,都不会是全面屏。。。        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {            return false;        }        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);        if (windowManager != null) {            Display display = windowManager.getDefaultDisplay();            Point point = new Point();            display.getRealSize(point);            float width, height;            if (point.x < point.y) {                width = point.x;                height = point.y;            } else {                width = point.y;                height = point.x;            }            if (height / width >= 1.97f) {                mIsAllScreenDevice = true;            }        }        return mIsAllScreenDevice;    }}

 

更多相关文章

  1. Android 透明度数值
  2. Android 颜色设置:透明度换算
  3. Android加载FlutterView黑屏问题
  4. android 黑屏状态下,开屏代码
  5. Mac环境Android 6.0虚拟机启动黑屏解决办法
  6. Android中颜色透明度对应16进制值
  7. 设置控件的透明度 ,android:layout_centerInParent="true" 失效
  8. Android颜色透明度设置
  9. Android之activity跳转显示桌面及切换黑屏问题解决方案

随机推荐

  1. [Android]文本框实现搜索和清空效果
  2. android的Handler
  3. 新手拥抱Android
  4. 理解 Android(安卓)Build 系统
  5. 中国Android应用商店汇总介绍
  6. Android(安卓)第七天重置版_BroadCastRec
  7. Android(安卓)xliff和字符串资源[Android
  8. Android(安卓)利用广播实现黑名单【指定
  9. Android(安卓)framework学习
  10. 关于Android弹出软键盘“顶起”View的问