SurfaceView在屏蔽HOME键出现的黑屏现象
引言
今天上头让我们下个SurFaceView预览的小Demo,静态XML写完,改成动态加载SurfaceView。本来一切都很顺利,但拿到公司的机子上,添加了更改后的HOME屏蔽效果,SurfaceView就黑屏不显示了。后来拿了一个别人封装的工具类解决了。然后上头就叫我们找下发生黑屏的原因。
过程
可能时这类问题遇到的较少,这一时半会没有比较有步骤的检查方法。自己摸索了半天(各种注释+各种测试)也没整明白原因。后来一个同事说可能是其中的一句代码的原因。我一看,傻眼了,方法说明已经说的很清楚了。是哪一句代码呢?
解决
先看代码:
SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView);
sv.setZOrderOnTop(true);//就是这句!!!
这方法是做什么呢?请看说明:
Control whether the surface view’s surface is placed on top of its window. Normally it is placed behind the window, to allow it to (for the most part) appear to composite with the views in the hierarchy. By setting this, you cause it to be placed above the window. This means that none of the contents of the window this SurfaceView is in will be visible on top of its surface.
大体意思其实就是说这个surface默认是在window下的,而通过
sv.setZOrderOnTop(true);
设置就能够将该surface放置在window最上方显示出来。而之前黑屏就是因为surface在window、下方