忽略特定视图的暗淡背后

时间:2017-11-06 00:05:46

标签: android window opacity popupwindow android-windowmanager

可以在不指定视图的情况下调暗所有屏幕。 这是我的代码。

显示弹出窗口后,我想在没有用户点击的视图的情况下调暗所有屏幕。

    private void showMessageBoxDialog(View view, String title, String message) {
                hideMessageBoxDialog();

                if (messageBox != null && !messageBox.isShowing()){
                    messageBox.setTitle(title);
                    messageBox.setMessage(message);
                    messageBox.show(view);
                }else {
                    messageBox = new PopupMessageBox(VocabularyBuilder.this, title, message);
                    messageBox.setTitleColorAttr(android.R.attr.textColorPrimary);
                    messageBox.setMessageColorAttr(android.R.attr.textColorSecondary);
                    messageBox.setBackgroundColorAttr(R.attr.backgroundColor);
                    messageBox.setDimAmount(0.5f);
                    messageBox.setCornerRadius(8);
                    messageBox.setStrokeWidth(2);
                    messageBox.setOnStateChangeListener(this);
                    messageBox.show(view);
                }
            }

   private void hideMessageBoxDialog() {
            if (messageBox != null && messageBox.isShowing()){
                messageBox.hide();
                mTabBarItem = null;
            }
    }

    @Override
    public void onShowing(View view) {
        // magic here
        view.ignoreDim(); or view.setAlpha(1);
    }

    @Override
    public void onDismiss(View view) {
          //
    }

这是我的PopupMessageBox类,我创建了Window管理器。如果金额有效值,我添加FLAG_DIM_BEHIND。

public class PopupMessageBox {

    *********

    private boolean isDimEnable() {
        return mDimAmount != -1;
    }

    public void setDimAmount(@FloatRange(from = 0, to = 1) float amount) {
        if (mDimAmount == amount )
            return;
        mDimAmount = amount;
    }

    public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener){
        mOnStateChangeListener = onStateChangeListener;
    }

    private WindowManager.LayoutParams createPopupLayoutParams(IBinder token) {
        final WindowManager.LayoutParams p = new WindowManager.LayoutParams();

        *****

        if (isDimEnable()){
            p.dimAmount = mDimAmount;
        }

        p.width = WindowManager.LayoutParams.MATCH_PARENT;
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;

        return p;
    }

    private int computeFlags(int curFlags) {
        curFlags &= ~(
                WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES |
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                        WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
                        WindowManager.LayoutParams.FLAG_SPLIT_TOUCH);
        if (!mFocusable) {
            curFlags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            if (mInputMethodMode == INPUT_METHOD_NEEDED) {
                curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
            }
        } else if (mInputMethodMode == INPUT_METHOD_NOT_NEEDED) {
            curFlags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        }
        if (!mTouchable) {
            curFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        }
        if (mOutsideTouchable) {
            curFlags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        }
        if (isDimEnable()){
            curFlags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        }
        if (mAttachedInDecor) {
            curFlags |= WindowManager.LayoutParams.FLAG_LAYOUT_ATTACHED_IN_DECOR;
        }
        return curFlags;
    }
}

我已经看到了其他方法。 示例是创建父视图和父级的chnage alpha。乙 但我想了解是否可以使用FLAG_DIM_BEHIND并获得相同的结果。

结果是。

enter image description here

0 个答案:

没有答案