實(shí)現(xiàn)仿微信查看大圖的時(shí)候StatusBar和NavigationBar的狀態(tài)變化
兩篇不錯(cuò)的關(guān)于這兩個(gè)Bar狀態(tài)的文章:
1.http://angeldevil.me/2014/09/02/About-Status-Bar-and-Navigation-Bar/
2.http://vitiy.info/small-guide-how-to-support-immersive-mode-under-android-4-4/
3.http://developer.android.com/training/system-ui/immersive.html
//得到contentView的父布局,是一個(gè)FrameLayout
rootContent = (ViewGroup) findViewById(android.R.id.content);
//add 大圖控件到 rootContent
root.addView(bview);
System UI的顯示和隱藏通過(guò)下面兩個(gè)函數(shù)實(shí)現(xiàn)。在hideSystemUI()方法中,通過(guò)SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN和SYSTEM_UI_FLAG_FULLSCREEN來(lái)隱藏Status Bar,讓Activity的UI占滿全屏(不會(huì)占據(jù)Navigation Bar的空間),通過(guò)SYSTEM_UI_FLAG_IMMERSIVE_STICKY模式,使得系統(tǒng)以半透明方法顯示System UI方便用戶操作,并會(huì)在一段時(shí)間后自動(dòng)隱藏,此時(shí)并不會(huì)引起onSystemUiVibilityChanged的調(diào)用.
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void hideSystemUI() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
在showSystemUI()方法中,通過(guò)SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN使得Activity的UI占據(jù)全屏,但是這個(gè)時(shí)候Status Bar和Navigation Bar都沒(méi)有被隱藏,所以Status Bar會(huì)遮擋住Activity UI的部分空間。解決辦法就是給ContentView設(shè)置fitSystemWindows為true。這樣系統(tǒng)會(huì)把被Status Bar遮擋住的區(qū)域自動(dòng)轉(zhuǎn)化成Activity UI的內(nèi)邊距,這樣被StatusBar遮擋的問(wèn)題就得到了解決。
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void showSystemUI() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
但是當(dāng)Window的焦點(diǎn)發(fā)生變化的時(shí)候,SYSTEM_UI_FLAG_IMMERSIVE_STICKY就會(huì)失效,Status Bar會(huì)重新出現(xiàn),并且不是透明的??梢员O(jiān)聽(tīng)onWindowFocusChanged事件來(lái)重新調(diào)用hideSystemUI()方法。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && hasView) {
mPhotoView.postDelayed(new Runnable() {
@Override
public void run() {
hideSystemUI();
}
}, 500);
}
}