OGeek|极客世界-中国程序员成长平台

标题: Android - 不截取当前屏幕的屏幕截图 [打印本页]

作者: 菜鸟教程小白    时间: 2022-11-6 16:12
标题: Android - 不截取当前屏幕的屏幕截图

代码对于第一个屏幕截图工作正常,并且无论移动到另一个 View ,都可以继续拍摄相同的屏幕截图。

如何获取当前截图?

public void saveBitmap(Bitmap bitmap) {

    File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + new SimpleDateFormat("yyyyMMddhhmmss'.jpg'").format(new Date()) );
    FileOutputStream fos =null;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

点击信息:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iSave:
          Bitmap bitmap = null;
          bitmap = takeScreenshot();
          saveBitmap(bitmap);
        break;
    } 
}

这里:

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}



Best Answer-推荐答案


截屏后调用rootView.setDrawingCacheEnabled(false);。将其关闭然后再次打开会强制它正确更新。

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   Bitmap bitmap = rootView.getDrawingCache();
   rootView.setDrawingCacheEnabled(false);
   return bitmap;
}

关于Android - 不截取当前屏幕的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031019/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4