Android Multiple SurfaceViews

前端 未结 3 1646
一整个雨季
一整个雨季 2020-11-30 01:23

I\'m trying to work with 3 SurfaceViews on one screen, one on top half (BoardView), one on bottom half (StatusView), and the last one as an extra layer above the top half (T

相关标签:
3条回答
  • 2020-11-30 01:57

    It looks like you are not supposed to create multiple SurfaceViews on one Layout. According to this two posts written by Android framework engineer:

    The way surface view is implemented is that a separate surface is created and Z-ordered behind its containing window, and transparent pixels drawn into the rectangle where the SurfaceView is so you can see the surface behind. We never intended to allow for multiple surface view.

    and

    you should effectively think of SurfaceView as an overlay you embed inside your window, giving you an area in which you can directly draw independently of the normal view update system.

    So, what you can do, is use one SurfaceView to draw all the graphics you want.

    0 讨论(0)
  • 2020-11-30 02:08

    It sounds like the SurfaceViews are being drawn, but transparency is not enabled for whichever one is on top. In your MySurfaceView class in the surfaceCreated() method, make sure you are calling holder.setFormat(PixelFormat.TRANSPARENT);

    0 讨论(0)
  • 2020-11-30 02:12

    You can have multiple SurfaceViewsin one layout. The "Multi-surface test" activity in Grafika has three.

    The first post cited in @nonsleepr's answer was followed up 9 months later with this post by the same author, which mentioned the existence of SurfaceView#setZOrderMediaOverlay().

    The key thing to understand is that SurfaceView is not a regular view. When your app comes to the foreground it gets a surface to draw on. Everything in your app's UI is rendered onto the app's surface by the app, and then that surface is composited with other surfaces (like the status bar and navigation bar) by the system compositor. When you create a SurfaceView, it's actually creating an entirely new surface that is composited by the system, not by your app.

    You can control the Z-ordering (i.e. "depth") of the SurfaceView surface very loosely. There are four positions, from top to bottom:

    • SurfaceView + ZOrderOnTop
    • (app UI goes here)
    • SurfaceView + ZOrderMediaOverlay
    • SurfaceView (default)

    If you have two SurfaceViews at the same depth, and they overlap, the results are undefined -- one will "win", but you can't control which.

    The system compositor on modern devices is very efficient when you have N surfaces. At N+1 surfaces you hit a performance cliff. So while you can have three SurfaceViews, you're generally better off keeping the number down. The value of N varies from device to device.

    Update: if you really want to understand how SurfaceView works, see the Android System-Level Graphics doc.

    0 讨论(0)
提交回复
热议问题