我正在尝试从整个屏幕的后台辅助功能服务中截取屏幕截图,我尝试从当前应用程序上绘制的服务中插入一个视图,然后尝试使用它来实现。但我得到了什么只是我的黑色背景视图。 感谢任何帮助,谢谢:D
public void addView() {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
if (layout != null) {
windowManager.removeViewImmediate(layout);
} else {
layout = (RelativeLayout) inflater.inflate(R.layout.hoock_layout, null);
layout.setDrawingCacheEnabled(true);
}
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
windowManager.addView(layout, params);
store(getScreenShot(layout), "TEST.png");
}
public Bitmap getScreenShot(View view){
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
screenView.measure(View.MeasureSpec.makeMeasureSpec(1500, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(1800, View.MeasureSpec.UNSPECIFIED));
screenView.layout(0, 0, screenView.getMeasuredWidth(), screenView.getMeasuredHeight());
screenView.buildDrawingCache(true);
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
return bitmap;
}
public void store(Bitmap bm, String fileName) {
final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AccessibilityPOF";
File dir = new File(dirPath);
if (!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}