参考文章:http://blog.csdn.net/shenshizhong/article/details/74011549
设置壁纸
在Andorid中,设置壁纸大小一般通过默认的WallpaperManager:
//初始化WallpaperManager
private WallpaperManager mWpm;public void initData(){// 初始化WallpaperManagermWpm = WallpaperManager.getInstance(this);// 是表示设置目前的像素是图片的像素,最好先获取手机屏幕像素大小,然后传递宽度和高度作为参数。mWpm.suggestDesiredDimensions(1280, 720);
}/*** 设置墙纸*/
private void wallpaper() {Tools.showWarningToast(getResources().getString(R.string.wallpaper_loading), this);mIb_wallpaper.setEnabled(false);//线程中设置壁纸new Thread() {@Overridepublic void run() {super.run();try {ImageView iv = mAdapter.getCurrentView(); //这是图片iv.setDrawingCacheEnabled(true);mWpm.setBitmap(iv.getDrawingCache());//设置壁纸mHandler.sendEmptyMessage(SET_WALLPAPER_SUCCESS);} catch (IOException e) {e.printStackTrace();}}}.start();}
设置壁纸的大小
但是以上代码设置的壁纸没有填充满整个屏幕,显示的是原图片的大小,那我们该怎么设置壁纸的大小呢???其实我们通过Bitmap改变图片大小即可:
/*** 设置为壁纸的图片应该填充满整个屏幕,所以需要先剪裁* @param bitMap* @return*/
private Bitmap imageCropper(Bitmap bitMap){int width = bitMap.getWidth();int height = bitMap.getHeight();Logger.e(PhotoPlayerActivity.this,"imageCropper() width:"+width);Logger.e(PhotoPlayerActivity.this,"imageCropper() height:"+height);// 设置想要的大小int newWidth = 1280;int newHeight = 720;// 计算缩放比例float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 取得想要缩放的matrix参数Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// 得到新的图片bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix,true);Logger.e(PhotoPlayerActivity.this,"imageCropper() newWidth"+bitMap.getWidth());Logger.e(PhotoPlayerActivity.this,"imageCropper() newHeight"+bitMap.getHeight());return bitMap;
}
那么完整的代码便是:
//初始化WallpaperManager
private WallpaperManager mWpm;public void initData(){// 初始化WallpaperManagermWpm = WallpaperManager.getInstance(this);// 是表示设置目前的像素是图片的像素,最好先获取手机屏幕像素大小,然后传递宽度和高度作为参数。mWpm.suggestDesiredDimensions(1280, 720);
}/*** 设置墙纸*/
private void wallpaper() {Tools.showWarningToast(getResources().getString(R.string.wallpaper_loading), this);mIb_wallpaper.setEnabled(false);//线程中设置壁纸new Thread() {@Overridepublic void run() {super.run();try {ImageView iv = mAdapter.getCurrentView(); //这是你图片,可用imageLoader转成imageViewiv.setDrawingCacheEnabled(true);Bitmap bitMap = imageCropper(iv.getDrawingCache());mWpm.setBitmap(bitMap);//设置壁纸mHandler.sendEmptyMessage(SET_WALLPAPER_SUCCESS);} catch (IOException e) {e.printStackTrace();}}}.start();}/*** 设置为壁纸的图片应该填充满整个屏幕,所以需要先剪裁* @param bitMap* @return*/
private Bitmap imageCropper(Bitmap bitMap){int width = bitMap.getWidth();int height = bitMap.getHeight();Logger.e(PhotoPlayerActivity.this,"imageCropper() width:"+width);Logger.e(PhotoPlayerActivity.this,"imageCropper() height:"+height);// 设置想要的大小int newWidth = 1280;int newHeight = 720;// 计算缩放比例float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 取得想要缩放的matrix参数Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// 得到新的图片bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix,true);Logger.e(PhotoPlayerActivity.this,"imageCropper() newWidth"+bitMap.getWidth());Logger.e(PhotoPlayerActivity.this,"imageCropper() newHeight"+bitMap.getHeight());return bitMap;
}