介绍
不同UI大小尺寸不同需要在特定区域进行高斯模糊适配,例如system ui下拉高度不同背景高斯模糊不同,或弹框背景区域做高斯模糊处理。
原理
1.截取当前屏幕图片(使用截图接口)
2.根据当前UI计算位置,大小
3.在截图bitmap上截取图片
4.高斯模糊处理截取后的图片
5.将图片设置为背景
具体实现
1.截取当前屏幕图片(使用截图接口)
public static String screenShotByShell(Context context){
// 获取内置当前应用路径
String sdCardPath = context.getCacheDir().getAbsolutePath()+screenShotPath;
File dir = new File(sdCardPath);
if(!dir.exists()){
dir.mkdirs();
}else{
delteDirectoryFiles(sdCardPath);
}
// 图片文件路径
String filePath = sdCardPath + File.separator +"screenshot-"+System.currentTimeMillis()+".png";
String shotCmd = "screencap -p " + filePath + " \n";
try {
Runtime.getRuntime().exec(shotCmd);
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
2.计算位置剪切图片
public static Bitmap cropShotBitmap(String path,View view){
int outWidth = view.getWidth();
int outHeight = view.getHeight();
int[] result = new int[2];
view.getLocationOnScreen(result);
int x = result[0];
int y = result[1];
Bitmap bitmap = ScreenShotUtil.cropBitmap(path,outWidth,outHeight,x,y);
Log.d(TAG,"screenShotByShell path = "+path+" x = "+x+" y = "+y+" outWidth = "+outWidth+" outHeight = "+outHeight);
return bitmap;
}
public static Bitmap cropBitmap(String screenShotPath,int outWidth, int outHeight, int x, int y){
Bitmap bitmap = BitmapFactory.decodeFile(screenShotPath);
if(bitmap != null){
//从屏幕整张图片中截取指定区域
try {
if(outHeight > bitmap.getHeight()){
outHeight = bitmap.getHeight();
}
if(outWidth > bitmap.getWidth()){
outWidth = bitmap.getWidth();
}
if(x < 0){
x = 0;
}
if(y < 0){
y = 0;
}
bitmap = Bitmap.createBitmap(bitmap, x, y, outWidth, outHeight);
//File file = new File(screenShotPath);
//file.delete();
// String path = screenShotPath.replace(".png",System.currentTimeMillis()+".png");
//onSaveBitmap(bitmap,screenShotPath);
return bitmap;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
if(bitmap != null){
Log.d(TAG,"cropBitmap sucess");
}else{
Log.d(TAG,"cropBitmap fail");
}
return null;
}
3.高斯模糊处理
/**
* 使用RenderScript实现高斯模糊的算法
* @param bitmap
* @return
*/
public static Bitmap blur(Context context,Bitmap bitmap,int radius){
//Let's create an empty bitmap with the same size of