Android获取当前桌面壁纸

来源:互联网 发布:淘宝手机端链接 编辑:程序博客网 时间:2024/04/28 19:17

http://blog.csdn.net/lnb333666/article/details/7772344

这段时间项目有需求要获取桌面的壁纸来设成背景,发现网上很多同学都通过WallpaperManager的getDrawable等API来获取,这样的确能获得到壁纸,但是要稍微注意一下。

桌面的壁纸可是很大很大的一张哦,你要直接用来设置成背景,那可是很丑的。所以我们需要稍微处理一下。

大致思路就是把一张壁纸切成几片,当前屏幕对应那片,我们就要那片。上个图:



[java] view plaincopy
  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));  

测试环境,我的手机是moto 526,屏幕共7屏,现在咱取第六屏,效果如下



原创粉丝点击