【Android】SystemUI设置透明状态栏

将系统状态栏背景设置为透明

加载流程
StatusBar —> PhoneStatusBarView —> PhoneStatusBarTransitions —> BarTransitions
StatusBar.java

//makeStatusBarView
protected void makeStatusBarView(){
    inflateStatusBarWindow(context);
    FragmentHostManager.get(mStatusBarWindow).addTagListener(CollapsedStatusBarFragment.TAG,(tag,fragment)->{
            CollapsedStatusBarFragment statusBarFragment = (CollapsedStatusBarFragment) fragment;
            statusBarFragment.initNotificationIconArea(mNotificationIconAreaController);
            mStatusBarView = (PhoneStatusBarView) fragment.getView();//通过CollapsedStatusBarFragment.onCreateView获取PhoneStatusBarView对象
            
    });
}

CollapsedStatusBarFragment.java

@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,Bundle savedInstanceState){
    return inflater.inflate(R.layout.status_bar,container,false);
}

status_bar.xml

<com.android.systemui.statusbar.phone.PhoneStatusBarView>
...
com.android.systemui.statusbar.phone.PhoneStatusBarView>

PhoneStatusBarView.java

public PhoneStatusBarView(Context context,AttributeSet attrs){
    super(context,attrs);
    mBarTransitions = new PhoneStatusBarTransitions(this);
}

@Override
public void onFinishInflate(){
    mBarTransitions.init();
}

PhoneStatusBarTransitions.java

public void init(){
    ...
    applyModeBackground(-1,getMode(),false);
    applyMode(getMode(),false);
}

BarTransitions.java

protected void applyModeBackground(int oldMode,int newMode,boolean animate){
    mBackground.applyModeBackground(oldMode,newMode,animate);//跳转至内部类BarBackgroundDrawable中
}


private static class BarBackgroundDrawable extends Drawable {
    public BarBackgroundBrawable(Context context,int gradientResourceId){
        if(DEBUG_COLORS){
            mOpaque = 0x00000000;//设置透明颜色代码
        }else{
            mOpaque = 0x00000000;        
        }
    }
    
    public void applyModeBackground(int oldMode,int newMode,boolean animate){
        if (mMode == newMode) return;
        mMode = newMode;
        mAnimating = animate;
        if (animate) {
            long now = SystemClock.elapsedRealtime();
            mStartTime = now;
            mEndTime = now + BACKGROUND_DURATION;
            mGradientAlphaStart = mGradientAlpha;
            mColorStart = mColor;
        }
        invalidateSelf();//最终走向draw
    }
    @Override
    public void draw(Canvas canvas) {
        mMode = -1;//add,强制走设定颜色
        ...
        else{
            targetColor = mOpaque;        
        }            
    }
}

你可能感兴趣的:(SystemUI,Android,android,java,android,studio)