Android實現狀態欄(statusbar)漸變效果的示例

分類:IT技術 時間:2017-09-26

前言

qq最近更新搞了漸變式狀態欄.然後...新需求就是要加這個.唉

先來張效果圖:


常見的方式:

設置Theme,狀態欄透明.

 <item name="android:windowTranslucentStatus">true</item>

實現起來比較簡單.

比如:systemBarTint

幾行代碼搞定了.

但是.我要潑冷水,這種方式會引起這些問題:

1.軟鍵盤彈起bug
2.fragment不適應

這種方式的優點:

兼容到4.4

我的看法:

1.用全屏模式或者狀態欄透明的方式去實現,感覺很勉強
2.為了一個statusbar的效果,然後去處理一大堆不適應,我認為是不值得的.特別是軟鍵盤彈出的問題
3.如果為了適配4.4,要費這麽大功夫的話...

我的實現方式:

1.拿到StatusBar:

很簡單,翻翻DecorView源碼,就能知道,是通過ColorViewState 配置創建的View.


由於是私有成員,要拿到對應的參數,一般做法是使用反射.

這裏我取了點巧,既然是View,那麽就能findviewbyId找到

只要想辦法拿到com.android.internal.R.id.statusBarBackground這個id值就行了

 private void initStatusBar() {
    if (statusBarView == null) {
      //android系統級的資源id得這麽拿,不然拿不到
      int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
      statusBarView = getWindow().findViewById(identifier);
    }
    if (statusBarView != null) {
      statusBarView.setBackgroundResource("你的漸變drawable資源id");
    }
  }

2.等StatusBar繪制完成

如果你直接在onCreate中調用上面的方法,你會發現,拿到的是null.

這是因為Statusbar還沒繪制完成.

所以,可以在onCreate()中使用Looper.myQueue().addIdleHandler()來保證Statusbar繪制完成後再findview.

然後對DecorView設置addOnLayoutChangeListener監聽

當布局發生變化,就設置statusbar的背景

    Looper.myQueue().addIdleHandler(new messageQueue.IdleHandler() {
      @Override
      public boolean queueIdle() {
        if (isStatusBar()) {
          initStatusBar();
          getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
              initStatusBar();
            }
          });
        }
        return false;
      }
    });

3.為什麽要addOnLayoutChangeListener:

其實不加監聽,也能實現改變statusbar顏色的效果..但是會出現問題

比如彈軟鍵盤後,彈popwindow後,引起window狀態改變時,statusbar的顏色就會復原...

基本完整的代碼

  private View statusBarView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    //延時加載數據.
    Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
      @Override
      public boolean queueIdle() {
        if (isStatusBar()) {
          initStatusBar();
          getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
              initStatusBar();
            }
          });
        }
        //只走一次
        return false;
      }
    });
  }
  private void initStatusBar() {
    if (statusBarView == null) {
      int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
      statusBarView = getWindow().findViewById(identifier);
    }
    if (statusBarView != null) {
        statusBarView.setBackgroundResource("你的漸變drawable資源id");
    }
  }

   protected boolean isStatusBar() {
    return true;
  }

這種方式的缺點:

1.因為沒有使用全屏的模式,所以適配4.4是沒戲了.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持電腦玩物。


Tags: quot statusBarView 方式 漸變 狀態 android

文章來源:


ads
ads

相關文章
ads

相關文章

ad