android - onCreate 方法内的屏幕截图在 android 中不起作用

标签 android

我在 Android 应用程序中工作,我想在重写方法 onCreate() 中捕获当前 Activity 的屏幕。当我在 Overrided 方法 onCreate() 中编写捕获屏幕的代码时,位图返回 null。但是当我在同一个 Activity 中单击按钮调用相同的代码时,位图返回正确的值。请更正我的代码以在我的 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/

相关文章:

android - ionic 初始加载时间

android - 在后台服务中使用 GoogleAPIClient

android - 隐藏视频但继续播放

Android 5.1.1 开发者选项 - USB 调试不显示

android - 构建时出现简单的应用程序错误

java - 如何在android中将数据从一个 Activity 传输到另一个 Activity

java - Android Studio 闹钟 MP3 歌曲

android - 按 ABI 分割时的后缀/前缀/之间的 .APK 名称

android - Dagger 可以用来在 Content Provider 上执行注入(inject)吗?

安卓应用内结算 : Refunded in-app purchases not cancelled