Why is a SurfaceView in a Fragment still visible w

2019-05-18 09:58发布

问题:

Just a general question: I have a Fragment which has a SurfaceView. When I hide the Fragment by the FragmentManager, the SurfaceView is still visible.

I found a workaround by just setting the View to INVISIBLE/GONE/VISIBLE when the Fragment is hidden/visible - but I'm wondering: Why is the SurfaceView still shown? Is it because the SurfaceView basically is a "punched hole", which means, it's not in the Layout-Hierarchy of the Fragment and therefore can't be hidden when the Layout is hidden?

What is the hiding-procedure doing with the Fragment? Does it simply sets the created view to gone?

回答1:

A SurfaceView creates an entirely separate graphics layer, composited by the system. The "hole" is included in the view hierarchy so the layout comes out right. There's a lot of things that don't quite work right when SurfaceView is involved.

A TextureView, by contrast, is actually part of the View itself, and will act appropriately. The disadvantage is that there's an extra step where pixels are copied from an off-screen buffer into the view. This is done by the GPU, so it's very fast, but if you're trying to animate a large portion of the screen at 60fps or minimize battery drain while playing a full-length movie then SurfaceView may be a better choice.

If using a TextureView is an option, doing so may make your life a bit simpler.