我知道setflags做的是用新的替换旧的标志。addflags正在追加更多标志。我只是很困惑,为什么我在setflags方法中看到的参数通常是相同的?
例如:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//or
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

在看了android.view.window类之后,我不清楚为什么它们必须做很多二进制操作符(not,and,or)。这是为了什么?
public void setFlags(int flags, int mask) {
        final WindowManager.LayoutParams attrs = getAttributes();
        attrs.flags = (attrs.flags & ~mask) | (flags & mask);
        mForcedWindowFlags |= mask;
        dispatchWindowAttributesChanged(attrs);
    }

还有一个问题
//argument is a flag
getWindow().addFlags(flag1);


//argument is the result of OR operator of 2 identical flags
getWindow().addFlags(flag1 | flag1);


//argument is the result of OR operator of 2 different flags
getWindow().addFlags(flag1 | flag 2);


//argument is the result of AND operator of 2 identical flags
getWindow().addFlags(flag1 & flag1);


//argument is the result of AND operator of 2 different flags
getWindow().addFlags(flag1 & flag2);

任何帮助都将不胜感激。

最佳答案

二进制运算符是因为字段是位字段。它们使用一个整数来保存许多设置,每个设置都分配给不同的位。然后使用二进制操作组合它们并正确设置位。这是一种常见的硬件技术,非常节省空间。通常,您将通过使用或对其启用一点(一个设置),并通过对其逆运算将其移除。这两个操作都保持其余设置不变。
你永远不会看到setflags(foo foo),因为它是多余的。所发生的一切就是foo会被设置好。您将看到setflags(foo bar),它将设置foo和bar。
当您看到setflags(foo,foo)时,第二个参数是一个掩码。它允许您同时打开和关闭字段,并且只有掩码中的位会改变。所以其他的设置都会保持原样。数学基本上是((getflags&~mask)(value&mask))。如果只想更改某些设置,并且希望将它们全部打开,则在这两个值中会看到相同的情况。setflags(x,x)等同于addflags(x)

07-28 01:27