//使用WallpaperManager类
@TargetApi(Build.VERSION_CODES.N)
private Bitmap getLockWallpaper(){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);//获取WallpaperManager实例
ParcelFileDescriptor mParcelFileDescriptor = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);//获取锁屏壁纸
if(mParcelFileDescriptor ==null){
mParcelFileDescriptor = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);//获取桌面壁纸
//在未主动设置锁屏壁纸时,锁屏壁纸为null,默认取的是桌面壁纸,
}
FileDescriptor fileDescriptor = mParcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);//获取Bitmap类型返回值
try {
mParcelFileDescriptor.close();
} catch(Exception e) {
android.util.Log.d("TAG","mParcelFileDescriptor.close() error");
}
return image;
}
private void setBackground(){ // 获取壁纸后设置为view的背景
try {
Drawable drawable =new BitmapDrawable(getLockWallpaper());//将Bitmap类型转换为Drawable类型
getWindow().getDecorView().setBackgroundDrawable(drawable);//设置背景
drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);//设置背景灰度
} catch(Exception e) {
android.util.Log.d("TAG","set Background fail");
}
}
网友评论