简体   繁体   English

Android 捕获一个 GameActivity View 作为 Bitmap

[英]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.我正在使用 Jetpack 的 GameActivity 在 Android 中呈现我的游戏,并且我在 Kotlin 代码中在其顶部添加了一些本机视图。 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.我想要一种将整个屏幕捕获为 Bitmap 的方法,但不知何故,OpenGL 渲染的东西不会出现在保存的 Bitmap 上,只是“正常”的 Android 视图。

This is how I am doing to capture the Bitmap:这就是我捕获 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.在我的onCreate上,这就是我获取rootView的方式,稍后我将其发送到此方法。 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.同样,创建的 Bitmap 具有overlayViews上的所有视图,但游戏本身不存在。 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.我什至调试了captureGameImage function,实际上v有两个子视图,游戏一和覆盖图,但不知何故游戏一没有在 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:您可以通过在您自己派生的 Activity 中覆盖GameActivity.onCreateSurfaceView()来禁用 SurfaceView 渲染:

  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.然后进行捕获,并将其放回生产版本以使用 SurfaceView 提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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