這段時間項目有需求要擷取案頭的壁紙來設成背景,發現網上很多同學都通過WallpaperManager的getDrawable等API來擷取,這樣的確能獲得到壁紙,但是要稍微注意一下。
案頭的壁紙可是很大很大的一張哦,你要直接用來設定成背景,那可是很醜的。所以我們需要稍微處理一下。
大致思路就是把一張壁紙切成幾片,當前螢幕對應那片,我們就要那片。上個圖:
// 擷取壁紙管理器WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);// 擷取當前壁紙Drawable wallpaperDrawable = wallpaperManager.getDrawable();// 將Drawable,轉成BitmapBitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();// 需要詳細說明一下,mScreenCount、getCurrentWorkspaceScreen()、mScreenWidth、mScreenHeight分別//對應於Launcher中的案頭螢幕總數、當前螢幕下標、螢幕寬度、螢幕高度.等下拿Demo的哥們稍微要注意一下float step = 0;// 計算出螢幕的位移量step = (bm.getWidth() - LauncherPreferenceModel.mScreenWidth)/ (LauncherPreferenceModel.mScreenCount - 1);// 截取相應螢幕的BitmapBitmap pbm = Bitmap.createBitmap(bm, (int) (mLauncher.getCurrentWorkspaceScreen() * step), 0,(int) (LauncherPreferenceModel.mScreenWidth),(int) (LauncherPreferenceModel.mScreenHeight));// 設定 背景layout.setBackgroundDrawable(new BitmapDrawable(pbm));
測試環境,My Phone是moto 526,螢幕共7屏,現在咱取第六屏,效果如下