美文网首页
全面屏手机dialog弹窗适配,解决上下空白间距的问题

全面屏手机dialog弹窗适配,解决上下空白间距的问题

作者: 追逐未来2016 | 来源:发表于2018-01-18 12:46 被阅读0次

1.目前项目中常用通过WindowManager获取手机屏幕的宽高来做一些UI展示,但是不兼容全面屏手机,手机分辨率2160 * 1080, 比如小米Mix2、锤子坚果Pro2等机型

Display display = activity.getWindowManager().getDefaultDisplay();
int widths = display.getWidth();
int heights = display.getHeight(); //全面屏手机获取的高度是2030,不包含底部的虚拟按键高度
解决的方法是通过获取顶层Window宽高
View view = activity.getWindow().getDecorView();
int widths = view.getWidth();
int heights = view.getHeight(); //高度是2160

2.自定义Dialog可以通过获取Window设置宽高

Window dialogWindow = dialog.getWindow();
lp = dialogWindow.getAttributes();
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.MATCH_PARENT; //注意一定要是MATCH_PARENT
dialogWindow.setAttributes(lp);
处理高斯模糊背景的dialog截屏的核心代码
    /**
     * 截屏
     */
    public static Bitmap takeScreenShot(Activity activity) {
        Bitmap b = null;
        try {
            // 获取windows中最顶层的view
            View view = activity.getWindow().getDecorView();
            view.buildDrawingCache();

            // 获取状态栏高度
            Rect rect = new Rect();
            view.getWindowVisibleDisplayFrame(rect);
            int statusBarHeights = rect.top;

            int widths = view.getWidth();
            int heights = view.getHeight();

            //此获取宽高的方式不适用全名屏, 不会包含底部的虚拟按键高度, 导致截屏过程中缺少这部分高度
            //Display display = activity.getWindowManager().getDefaultDisplay();
            // 获取屏幕宽和高
            //int widths = display.getWidth();
            //int heights = display.getHeight();

            // 允许当前窗口保存缓存信息
            view.setDrawingCacheEnabled(true);

            // 去掉状态栏
            b = Bitmap.createBitmap(view.getDrawingCache(), 0,
                    statusBarHeights, widths, heights - statusBarHeights);

            // 销毁缓存信息
            view.destroyDrawingCache();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

3.处理效果对比:
上下空白间距


对比照01.png

底部模糊未覆盖


对比照3.png

相关文章

网友评论

      本文标题:全面屏手机dialog弹窗适配,解决上下空白间距的问题

      本文链接:https://www.haomeiwen.com/subject/xwwioxtx.html