我在一个android应用程序中工作,我想在覆盖的oncreate()方法中捕获当前活动的屏幕。当我在重写的oncreate()方法中编写捕获屏幕的代码时,位图返回null。但是,当我在同一活动中的按钮单击中调用同一代码时,位图会返回正确的值。请更正我的代码,使其在occreate()方法本身中按位图返回。
我的代码是:

View v1 = findViewById(R.id.printpreviewBaseScrollView);
v1.setDrawingCacheEnabled(true);
Bitmap bmv = v1.getDrawingCache();

最佳答案

View printlayout = findViewById(R.id.printpreviewBaseRelativeLayout);
        printlayout.setDrawingCacheEnabled(true);

        printlayout.measure(printlayout.getMeasuredWidth(),
                printlayout.getMeasuredHeight());

         printlayout.layout(0, 0, 700, 1755);



        Log.e("width", "" + printlayout.getMeasuredWidth());
        Log.e("hight", "" + printlayout.getMeasuredHeight());

        printlayout.buildDrawingCache(true);

        bitmap = Bitmap.createBitmap(printlayout.getDrawingCache());
        printlayout.setDrawingCacheEnabled(false); // clear drawing cache

关于android - 方法onCreate内的屏幕截图在android中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13173022/

10-11 22:29