7

This answer describes how to make a SurfaceView with a transparent background.

However it requires setZOrderOnTop(true) to be called on the SurfaceView, which unsurprisingly puts the SurfaceView on top of the other views.

This isn't exactly what I want. Given Views A, C, and SurfaceView B I would like to order the views as A behind B behind C.

If B weren't a SurfaceView with a transparent background this would simply be:

<RelativeLayout>
  <ViewA>
  <ViewB>
  <ViewC>
</RelativeLayout>

Is it possible to achieve this when B is a SurfaceView with a transparent background?

Community
  • 1
  • 1
GDanger
  • 1,641
  • 2
  • 22
  • 35

1 Answers1

8

There are four possible layers with the current SurfaceView API. From top to bottom:

  • setZOrderOnTop
  • (Views)
  • setZOrderMediaOverlay
  • default

The SurfaceView surface, and the surface with all of the Views, are composited by the system compositor. You cannot Z-order sandwich a SurfaceView surface between two Views. (If you want the full details, read this article. See also the multi-surface test activity in Grafika, which software-renders to three overlapping transparent SurfaceViews.)

What you may want to do is use a TextureView (API 14+), which has many of the same properties but interacts fully with Views. If you were using Canvas to draw on your SurfaceView, you may be able to use a custom View, which has the added advantage of hardware accelerated rendering.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • Thank you. Which do you think will have better performance for a paint-esque overlay, TextureView with Canvas and a rendering Thread or a custom View? – GDanger Sep 23 '14 at 16:39
  • 2
    Rendering with Canvas on a surface is never hardware accelerated. That applies to both SurfaceView and TextureView. You'll get better performance with a custom View. There are some limitations to the hardware-accelerated renderer, so do look over the lists of supported features on the hardware acceleration page linked in the answer. – fadden Sep 23 '14 at 19:32