WallpaperManager(壁紙管理器),是手機壁紙相關的一個API的相關類。其設置壁紙常有如下三種方法可調用:
- setBitmap(Bitmap bitmap):將壁紙設置為bitmap所代表的位圖
- setResource(int resid):將壁紙設置為resid資源所代表的圖片
- setStream(InputStream data):將壁紙設置為data數據所代表的圖片
這是其余設置不強相關的方法:
- clear():清除壁紙,設置回系統默認的壁紙
- getDesiredMinimumHeight():最小壁紙高度
- getDesiredMinimumWidth():最小壁紙寬度
- getDrawable():獲得當前系統壁紙,如果沒有設置壁紙,則返回系統默認壁紙
- getWallpaperInfo():加入當前壁紙是動態壁紙,返回動態壁紙信息
- peekDrawable():獲得當前系統壁紙,如果沒設置壁紙的話返回null
在設置壁紙前,要在文件中設置權限:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
設置壁紙主要是以下幾個步驟:
1、獲得WallpaperManager對象
主要常用的是以下二種調用辦法:
(1)WallpaperManager wpManager =WallpaperManager.getInstance(this);
(2)WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(Context.WALLPAPER_SERVICE);
2、調用系統自帶的壁紙選擇功能
Intent chooseIntnet = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(chooseIntent, getText(R.string.chooser_wallpaper));
startActivity(chooser);
3.將Activity的背景設置為壁紙背景
一種是在Activity中用代碼進行設置:
setTheme(android.R.style.Theme_Wallpaper_NoTitleBar_Fullscreen)
另一種是在AndroidManifest.xml中修改Activity的主題:
<activity android:name=".MainActivity"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar"/>