获取桌面的壁纸来设成背景

第一种方法,可直接设置主题,

下面重点介绍第二种方法:

  1. // 获取壁纸管理器   
  2.             WallpaperManager wallpaperManager = WallpaperManager  
  3.                     .getInstance(mContext);  
  4.             // 获取当前壁纸   
  5.             Drawable wallpaperDrawable = wallpaperManager.getDrawable();  
  6.             // 将Drawable,转成Bitmap   
  7.             Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();  
  8.   
  9.             // 需要详细说明一下,mScreenCount、getCurrentWorkspaceScreen()、mScreenWidth、mScreenHeight分别   
  10.             //对应于Launcher中的桌面屏幕总数、当前屏幕下标、屏幕宽度、屏幕高度.等下拿Demo的哥们稍微要注意一下   
  11.             float step = 0;  
  12.             // 计算出屏幕的偏移量   
  13.             step = (bm.getWidth() - LauncherPreferenceModel.mScreenWidth)  
  14.                     / (LauncherPreferenceModel.mScreenCount - 1);  
  15.             // 截取相应屏幕的Bitmap   
  16.             Bitmap pbm = Bitmap.createBitmap(bm, (int) (mLauncher  
  17.                     .getCurrentWorkspaceScreen() * step), 0,  
  18.                     (int) (LauncherPreferenceModel.mScreenWidth),  
  19.                     (int) (LauncherPreferenceModel.mScreenHeight));  
  20.             // 设置 背景   
  21.             layout.setBackgroundDrawable(new BitmapDrawable(pbm));  
posted @ 2012-12-01 15:31  罗小姿  阅读(388)  评论(0编辑  收藏  举报