0

I am using Jetpack's GameActivity to render my game in Android, and I am adding some native views on the top of it in the Kotlin code. I would like a way to capture the entire screen as a Bitmap, but somehow the OpenGL rendered things do not appear on the saved Bitmap, just the "normal" Android View's.

This is how I am doing to capture the Bitmap:

private fun captureGameImage(v: View): Bitmap {
    val b = Bitmap.createBitmap(v.width, v.height, Bitmap.Config.ARGB_8888)
    val c = Canvas(b)
    v.draw(c)
    return b
}

on my onCreate this is how I get the rootView, which I later on send to this method. I inflate the overlay layout and add to it:

val overlayViews = layoutInflater.inflate(R.layout.widgets, null)
rootView = (findViewById<View>(android.R.id.content) as ViewGroup)
rootView.addView(overlayViews)

Then at some point I invoke:

captureGameImage(rootView)

Again, the created Bitmap has all the views that are on the overlayViews, but the game itself is not there. I even debugged the captureGameImage function and indeed the v has two children views, the game one and the overlay one, but somehow the game one doesn't get captured in the Bitmap.

Any ideas how to capture the entire view, including the game rendered stuff?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • GameActivity renders to a SurfaceView, which is normally a hardware Overlay. your capture is capturing a View, not the game content. You might refer something from this one https://stackoverflow.com/questions/25086263/take-screenshot-of-surfaceview? – Gerry Nov 28 '22 at 02:16

1 Answers1

0

You might be able to disable SurfaceView rendering by overriding the GameActivity.onCreateSurfaceView() in your own derived Activity:

  final var mContentView: View? = null

  /**
   * Creates an empty View for the launching activity, prevent GameActivity from creating the
   * default SurfaceView.
   */
  @Override
  protected void onCreateSurfaceView() {
    mSurfaceView = null;

    getWindow().takeSurface(this);

    mContentView = new View(this);
    setContentView(mContentView);
    mContentView.requestFocus();
  }

Then do your capturing, and put it back for your production version for the performance gains with the SurfaceView.

Gerry
  • 1,223
  • 9
  • 17