Android WindowManager 背景暗化

Android About 1,232 words

主要代码

WindowManager.LayoutParams.FLAG_DIM_BEHIND

mParams.dimAmount = 0.5f;

示例

mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mLayoutParams = new LayoutParams();
mLayoutParams.height = LayoutParams.WRAP_CONTENT;
mLayoutParams.width = LayoutParams.MATCH_PARENT;
mLayoutParams.gravity = Gravity.BOTTOM;
mLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | LayoutParams.FLAG_DIM_BEHIND;
mLayoutParams.dimAmount = 0.5f;
mLayoutParams.format = PixelFormat.TRANSLUCENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    mLayoutParams.type = LayoutParams.TYPE_TOAST;
    Log.d(TAG, "initWindowManager: TYPE_TOAST");
} else {
    mLayoutParams.type = LayoutParams.TYPE_PRIORITY_PHONE;
    Log.d(TAG, "initWindowManager: TYPE_PRIORITY_PHONE");
}
mLayoutParams.setTitle("BaseFloatWindowView");
mLayoutParams.windowAnimations = R.style.anim_MarkPhoneNumWindowView;

设置了FlagLayoutParams.FLAG_WATCH_OUTSIDE_TOUCH后监听addView中的View的触摸事件

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_OUTSIDE:
            //相应逻辑
            hide();
            return true;
    }
    return false;
}
Views: 4,964 · Posted: 2019-04-13

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh