简体   繁体   中英

Android capture a GameActivity View as Bitmap

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM