Android将当前屏幕转换为位图

Adi*_*fyr 5 android screen android-imageview android-bitmap

我想转换我的应用程序的屏幕并将其转换为位图.我已经知道如何将视图转换为位图,但这不是我想要的,因为我的屏幕由许多片段组成.因此,我希望能够采取屏幕并将其转换为位图...帮助非常感谢谢谢.

jac*_*ris 9

您可以使用这样的东西并将布局作为视图传递;)

public Bitmap takeScreenShot(View view) {
        // configuramos para que la view almacene la cache en una imagen
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        view.buildDrawingCache();

        if(view.getDrawingCache() == null) return null; // Verificamos antes de que no sea null

        // utilizamos esa cache, para crear el bitmap que tendra la imagen de la view actual
        Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();

        return snapshot;
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您传递布局根目录或主目录,它会拍摄屏幕上内容的快照;) (2认同)