Android擷取當前案頭壁紙

來源:互聯網
上載者:User

這段時間項目有需求要擷取案頭的壁紙來設成背景,發現網上很多同學都通過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屏,現在咱取第六屏,效果如下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.