对Android源码中常见的一些flag的运算的理解

来源:互联网 发布:淘宝网品牌女鞋 编辑:程序博客网 时间:2024/05/16 11:11

http://blog.csdn.net/dabaoonline/article/details/50163977


在Android源码中,包括一些比较规范的源码中,通常会出现flag(我理解我标志位)。

可以这么认为:

    a&~b:   清除标志位b;
    a|b:       添加标志位b;
    a&b:     取出标志位b;
      a^b:    取出a与b的不同部分;

例如在View的setFlags方法中有如下代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void setFlags(int flags, int mask) {  
  2.        int old = mViewFlags;//将标记赋值给old  
  3.        mViewFlags = (mViewFlags & ~mask) | (flags & mask);//mViewFlags清除mask后添加从flags中取出的mask标志  
  4.   
  5.        int changed = mViewFlags ^ old;//取出新旧标记的不同部分。  
  6.        if (changed == 0) {  
  7.            return;  
  8.        }  
  9.        int privateFlags = mPrivateFlags;  
  10.   
  11.        /* Check if the FOCUSABLE bit has changed */  
  12.        if (((changed & FOCUSABLE_MASK) != 0) &&  
  13.                ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {  
  14.            if (((old & FOCUSABLE_MASK) == FOCUSABLE)  
  15.                    && ((privateFlags & PFLAG_FOCUSED) != 0)) {  
  16.                /* Give up focus if we are no longer focusable */  
  17.                clearFocus();  
  18.            } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)  
  19.                    && ((privateFlags & PFLAG_FOCUSED) == 0)) {  
  20.                /* 
  21.                 * Tell the view system that we are now available to take focus 
  22.                 * if no one else already has it. 
  23.                 */  
  24.                if (mParent != null) mParent.focusableViewAvailable(this);  
  25.            }  
  26.            if (AccessibilityManager.getInstance(mContext).isEnabled()) {  
  27.                notifyAccessibilityStateChanged();  
  28.            }  
  29.        }  
  30.   
  31.        if ((flags & VISIBILITY_MASK) == VISIBLE) {  
  32.            if ((changed & VISIBILITY_MASK) != 0) {  
  33.                /* 
  34.                 * If this view is becoming visible, invalidate it in case it changed while 
  35.                 * it was not visible. Marking it drawn ensures that the invalidation will 
  36.                 * go through. 
  37.                 */  
  38.                mPrivateFlags |= PFLAG_DRAWN;  
  39.                invalidate(true);  
  40.   
  41.                needGlobalAttributesUpdate(true);  
  42.   
  43.                // a view becoming visible is worth notifying the parent  
  44.                // about in case nothing has focus.  even if this specific view  
  45.                // isn't focusable, it may contain something that is, so let  
  46.                // the root view try to give this focus if nothing else does.  
  47.                if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {  
  48.                    mParent.focusableViewAvailable(this);  
  49.                }  
  50.            }  
  51.        }  
  52.   
  53.        /* Check if the GONE bit has changed */  
  54.        if ((changed & GONE) != 0) {  
  55.            needGlobalAttributesUpdate(false);  
  56.            requestLayout();  
  57.   
  58.            if (((mViewFlags & VISIBILITY_MASK) == GONE)) {  
  59.                if (hasFocus()) clearFocus();  
  60.                clearAccessibilityFocus();  
  61.                destroyDrawingCache();  
  62.                if (mParent instanceof View) {  
  63.                    // GONE views noop invalidation, so invalidate the parent  
  64.                    ((View) mParent).invalidate(true);  
  65.                }  
  66.                // Mark the view drawn to ensure that it gets invalidated properly the next  
  67.                // time it is visible and gets invalidated  
  68.                mPrivateFlags |= PFLAG_DRAWN;  
  69.            }  
  70.            if (mAttachInfo != null) {  
  71.                mAttachInfo.mViewVisibilityChanged = true;  
  72.            }  
  73.        }  
  74.   
  75.        /* Check if the VISIBLE bit has changed */  
  76.        if ((changed & INVISIBLE) != 0) {  
  77.            needGlobalAttributesUpdate(false);  
  78.            /* 
  79.             * If this view is becoming invisible, set the DRAWN flag so that 
  80.             * the next invalidate() will not be skipped. 
  81.             */  
  82.            mPrivateFlags |= PFLAG_DRAWN;  
  83.   
  84.            if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {  
  85.                // root view becoming invisible shouldn't clear focus and accessibility focus  
  86.                if (getRootView() != this) {  
  87.                    clearFocus();  
  88.                    clearAccessibilityFocus();  
  89.                }  
  90.            }  
  91.            if (mAttachInfo != null) {  
  92.                mAttachInfo.mViewVisibilityChanged = true;  
  93.            }  
  94.        }  
  95.   
  96.        if ((changed & VISIBILITY_MASK) != 0) {  
  97.            if (mParent instanceof ViewGroup) {  
  98.                ((ViewGroup) mParent).onChildVisibilityChanged(this,  
  99.                        (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));  
  100.                ((View) mParent).invalidate(true);  
  101.            } else if (mParent != null) {  
  102.                mParent.invalidateChild(thisnull);  
  103.            }  
  104.            dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));  
  105.        }  
  106.   
  107.        if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {  
  108.            destroyDrawingCache();  
  109.        }  
  110.   
  111.        if ((changed & DRAWING_CACHE_ENABLED) != 0) {  
  112.            destroyDrawingCache();  
  113.            mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;  
  114.            invalidateParentCaches();  
  115.        }  
  116.   
  117.        if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {  
  118.            destroyDrawingCache();  
  119.            mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;  
  120.        }  
  121.   
  122.        if ((changed & DRAW_MASK) != 0) {  
  123.            if ((mViewFlags & WILL_NOT_DRAW) != 0) {  
  124.                if (mBackground != null) {  
  125.                    mPrivateFlags &= ~PFLAG_SKIP_DRAW;  
  126.                    mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;  
  127.                } else {  
  128.                    mPrivateFlags |= PFLAG_SKIP_DRAW;  
  129.                }  
  130.            } else {  
  131.                mPrivateFlags &= ~PFLAG_SKIP_DRAW;  
  132.            }  
  133.            requestLayout();  
  134.            invalidate(true);  
  135.        }  
  136.   
  137.        if ((changed & KEEP_SCREEN_ON) != 0) {  
  138.            if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {  
  139.                mParent.recomputeViewAttributes(this);  
  140.            }  
  141.        }  
  142.   
  143.        if (AccessibilityManager.getInstance(mContext).isEnabled()  
  144.                && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0  
  145.                        || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {  
  146.            notifyAccessibilityStateChanged();  
  147.        }  
  148.    }  
0 0