Android获取当前桌面壁纸

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

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

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

// 获取壁纸管理器  
            WallpaperManager wallpaperManager = WallpaperManager  
                    .getInstance(mContext);  
            // 获取当前壁纸  
            Drawable wallpaperDrawable = wallpaperManager.getDrawable();  
            // 将Drawable,转成Bitmap  
            Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();  
  
            // 需要详细说明一下,mScreenCount、getCurrentWorkspaceScreen()、mScreenWidth、mScreenHeight分别  
            //对应于Launcher中的桌面屏幕总数、当前屏幕下标、屏幕宽度、屏幕高度.等下拿Demo的哥们稍微要注意一下  
            float step = 0;  
            // 计算出屏幕的偏移量  
            step = (bm.getWidth() - LauncherPreferenceModel.mScreenWidth)  
                    / (LauncherPreferenceModel.mScreenCount - 1);  
            // 截取相应屏幕的Bitmap  
            Bitmap pbm = Bitmap.createBitmap(bm, (int) (mLauncher  
                    .getCurrentWorkspaceScreen() * step), 0,  
                    (int) (LauncherPreferenceModel.mScreenWidth),  
                    (int) (LauncherPreferenceModel.mScreenHeight));  
            // 设置 背景  
            layout.setBackgroundDrawable(new BitmapDrawable(pbm));  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值