当前位置: 移动技术网 > IT编程>移动开发>Android > Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法

Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

迟华东,订婚戒指戴哪个手指,二手制冰机

1.最近的项目中,有一个activity用到fragment+viewpager,其中一个fragment中实现了视频播放的功能,包含有surfaceview。结果,每次打开程序第一次进入到该activity时都会闪屏黑一下。原因就出在surfaceview。

详解:

  i think i found the reason for the black flash. in my case i'm using a surfaceview inside a fragment and dynamically adding this fragment to the activity after some action. the moment when i add the fragment to the activity, the screen flashes black. i checked out grepcode for the surfaceview source and here's what i found: when the surface view appears in the window the very fist time, it requests the window's parameters changing by calling a private iwindowsession.relayout(..) method. this method “gives” you a new frame, window, and window surface. i think the screen blinks right at that moment.
   the solution is pretty simple: if your window already has appropriate parameters it will not refresh all the window's stuff and the screen will not blink. the simplest solution is to add a 0px height plain surfaceview to the first layout of your activity. this will recreate the window before the activity is shown on the screen, and when you set your second layout it will just continue using the window with the current parameters. i hope this helps.

原因:

surfaceview因为不同于一般的view,它有自己良好的缓冲以及数据存取机制,系统对他有特殊处理。当surfaceview第一次在当前activity上添加的时候,系统会给windowmanager重新排布局,relayout,这样就会黑一下,这个只会出现在第一次,以后再添加surfaceview时就不会黑屏了。

解决:

可以在没有进入surfaceview的界面(比如很多程序一进去就有个loading界面)的时候,在其它界面的layout.xml文件中添加一个surfaceview,宽和高都弄成0dp,这样对布局没影响,而且这个东西使得surfaceview第一次出现了,那么也就是说,下一次我们真正用到surfaceview的时候就不会闪屏了。

2.viewpager中有surfaceview播放视频,来回滑屏时,关于黑屏移动问题

解决:

surfaceview.setzordertop(true);

surfaceview.getholder().setformat(surfaceview.transparent);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网