• 判断状态栏是否显示以及获取状态栏高度的方法,及工具类列子【续:及OnGlobalLayoutListener的利用】


    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0731/1640.html

    本篇博客是http://www.cnblogs.com/longjunhao/p/8973729.html的后续

    问题:上篇博客中,如果在onCreate中就需要获取statusbar的高度,当kill进程之后再次进入oncreate时,由于新增的statusBarHelperView还没有绘制完成导致statusbar获取失败。

    解决方案:

    方案1:在onCreate中调用statusbar时,做个延迟操作。但是这种方案不太好,毕竟是如果手机比较卡的时候可能也会失败

    方案2:给statusHelperView增加绘制完的监听:mStatusBarHelperView.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);当绘制完成后会调用该监听中的onGlobalLayout方法,然后再通过回调,当执行onGlobalLayout时再获取statusbar的高度,在上面博客中的工具类中新增如下方法:

    package com.smartisanos.ime.util;
    
    import android.content.Context;
    import android.graphics.PixelFormat;
    import android.os.Handler;
    import android.os.Message;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewTreeObserver;
    import android.view.WindowManager;
    
    import com.smartisanos.ime.IMEApp;
    
    public class StatusBarHelper {
        private Context mContext;
        private View mStatusBarHelperView;
        private static StatusBarHelper mStatusBarHelper = new StatusBarHelper();
        private static final int MSG_INIT_STATUS_BAR = 1;
        private static final int MSG_REMOVE_STATUS_BAR = 2;
        private static final long MSG_REMOVE_DELAY_TIME = 500L;
    
        private StatusBarHelperViewLayoutListener mStatusBarHelperViewLayoutListener;
    
        private Handler mStatusBarHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_INIT_STATUS_BAR:
                        mStatusBarHandler.removeMessages(MSG_REMOVE_STATUS_BAR);
                        initStatusBarHelperView();
                        break;
                    case MSG_REMOVE_STATUS_BAR:
                        removeStatusBarView();
                        break;
                }
            }
        };
    
        private StatusBarHelper() {
            mContext = IMEApp.getContext();
        }
    
        public static StatusBarHelper getInstance() {
            return mStatusBarHelper;
        }
    
        private void initStatusBarHelperView() {
            if (mStatusBarHelperView != null) {
                return;
            }
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            mStatusBarHelperView = new View(mContext);
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
            lp.gravity = Gravity.LEFT | Gravity.TOP;
            lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
            lp.format = PixelFormat.TRANSLUCENT;
            mStatusBarHelperView.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);
            wm.addView(mStatusBarHelperView, lp);
        }
    
        public void addStatusBarHelperView() {
            Message message = mStatusBarHandler.obtainMessage(StatusBarHelper.MSG_INIT_STATUS_BAR);
            mStatusBarHandler.sendMessage(message);
        }
    
        public void removeStatusBarHelperView() {
            Message message = mStatusBarHandler.obtainMessage(StatusBarHelper.MSG_REMOVE_STATUS_BAR);
            mStatusBarHandler.sendMessageDelayed(message, StatusBarHelper.MSG_REMOVE_DELAY_TIME);
        }
    
        public int getStatusBarHeight() {
            if (mStatusBarHelperView == null) {
                return 0;
            }
            int[] windowParams = new int[2];
            int[] screenParams = new int[2];
            mStatusBarHelperView.getLocationInWindow(windowParams);
            mStatusBarHelperView.getLocationOnScreen(screenParams);
            return screenParams[1] - windowParams[1];
        }
    
        private void removeStatusBarView() {
            if (mStatusBarHelperView != null) {
                mStatusBarHelperView.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener);
                WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                wm.removeView(mStatusBarHelperView);
                mStatusBarHelperView = null;
            }
        }
    
        private ViewTreeObserver.OnGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mStatusBarHelperViewLayoutListener != null) {
                    mStatusBarHelperViewLayoutListener.updateStatusBarHeightWhenGlobalLayout();
                }
            }
        };
    
        public interface StatusBarHelperViewLayoutListener {
            void updateStatusBarHeightWhenGlobalLayout();
        }
    
        public void setStatusBarHelperViewLayoutListener(StatusBarHelperViewLayoutListener listener) {
            mStatusBarHelperViewLayoutListener = listener;
        }
    
        public void onConfigurationChanged() {//此处和本博客无关
            removeStatusBarView();
            initStatusBarHelperView();
        }
    }

     这样在onCreate中调用的时候,只需要回调方法即可:

            StatusBarHelper.getInstance().setStatusBarHelperViewLayoutListener(new StatusBarHelper.StatusBarHelperViewLayoutListener() {
            @Override
            public void updateStatusBarHeightWhenGlobalLayout() {
                if (mStatusBarHeight != IMEContext.config().getStatusBarHeight() && isShowing()) {
                    mStatusBarHeight = IMEContext.config().getStatusBarHeight();
                    //当helpeView绘制完成之后,判断mStatusBarHeight(此全局变量是onCreate中获取的)和新的不相等后,执行相应的操作(eg:update操作)
                }
            }
        });
    我的GitHub:https://github.com/lelelongwang
  • 相关阅读:
    Dubbo介绍以及Dubbo Admin的安装
    CentOS下安Zookeeper服务
    Linux定时备份mysql数据库
    Spring Boot 整合Mybatis 框架以及使用
    Sprint Boot日志功能的使用
    Centos下安装Redis服务
    Java 通用md5工具类
    Screen 用法简述
    解决zabbix的cannot allocate shared memory of size错误
    Python 之禅
  • 原文地址:https://www.cnblogs.com/longjunhao/p/9055087.html
Copyright © 2020-2023  润新知