好吧,我不得不说没有单一的API提供这个功能,用户必须复合两个位图,一个是壁纸部分,另一个是启动器的桌面视图。通常,启动器会将壁纸扩展到多个屏幕,因此我们必须获取壁纸的可见部分。
以下是我的做法:
//get wallpaper's visible part
//@param screenCount how many screens launcher has
//@param screenCurrent current screen index
Bitmap wallpaper =
((BitmapDrawable) WallpaperManager.getInstance(context).getDrawable()).getBitmap();
float step = 0;
step = (wallpaper.getWidth() - w) / (screenCount - 1);
Bitmap wallpaperCurrent =
Bitmap.createBitmap(wallpaper, (int)(screenCurrent * step), 0, w, h);
//get launcher's visible view bitmap
View decorview = context.getWindow().getDecorView();
decorview.buildDrawingCache();
Bitmap desktopBitmap = decorview.getDrawingCache();
//compound bitmaps
Bitmap compoundBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(compoundBitmap);
canvas.drawBitmap(wallpaperCurrent, 0, 0, null);
canvas.drawBitmap(desktopBitmap, 0, 0, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
// return compoundBitmap