Android 狀態欄背景模糊透明效果實現

1.整體思路:

更換壁紙時,獲取當前桌面壁紙(已適應屏幕後的壁紙),裁剪當前壁紙適應statusbar

將其設置爲statusbar背景,再採用高斯模糊處理算法,實現模糊效果。關於透明度,可以通過設置background實現。

2.實現:

首先調整壁紙鋪滿屏幕,當前壁紙未充滿屏幕。修改offset值爲0,壁紙窗口繪製出現y軸偏移,導致屏幕未能鋪滿。

再來看背景處理,分爲兩種情況,一個是靜態壁紙,一個是動態壁紙。先來看靜態壁紙,當tabletstatusbar收到替換壁紙intent時,分別有以下四個過程:

1) mBackground = mWallpaperManager.getBitmap();

獲取當前桌面處理後的壁紙。

2)根據屏幕大小,以及statusbar長寬,裁剪當前桌面壁紙

Bitmap result = Bitmap.createBitmap(mBackground, clipX, clipY, clipW, clipH, null, false);

3)利用高斯模糊算法,處理裁剪後壁紙爲模糊狀態,並可調整實際模糊度。

關於高斯模糊算法可參考:http://blog.csdn.net/luohai859/article/details/25039795

Drawable drawable = BoxBlurFilter(result);

/** 水平方向模糊度 */

private static float hRadius = 10;

/** 豎直方向模糊度 */

private static float vRadius = 10;

/** 模糊迭代度 */

private static int iterations = 7;

4)設置背景

mStatusBarView.setBackgroundDrawable(drawable);

靜態壁紙先告一段落,來看動態壁紙,對於動態壁紙,我們的方案是儘量尋找一張靜態圖片來替代。

動態壁紙的設置時,與靜態壁紙設置不同,首先會setWallpaperComponent,所以在每次更換動態壁紙時,發送廣播,來到code 2,當tabletstatusbar 收到爲動態壁紙時,則設置背景爲靜態圖片。

以上是壁紙替換動作時,相關處理邏輯。繼續來看,用戶開機後,statusbar初始化時,設置背景。

tabletstatusbar  makeStatusBarView()方法中,發送MSG_SET_WZ_STATUSBAR_BLUR消息,handler收到並處理:

關鍵方法是mWallpaperManager.getWallpaperInfo() != null 來判斷當前壁紙是否爲動態壁紙或爲靜態壁紙,判斷後按code2相同邏輯,處理之,並最終實現statusbar 背景模糊效果。

最後,還有一點是,透明背景效果的實現,在此用了一個取巧的方法:

Code 5:

frameworks/base/packages/SystemUI/res/layout/system_bar.xml

 

         <RelativeLayout

             android:id="@+id/bar_contents"

             android:layout_width="match_parent"

             android:layout_height="match_parent"

-            android:clipChildren="false" >

+            android:clipChildren="false"

+            android:background="#30000000" >

到此爲止,statusbar 背景模糊透明效果基本實現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章