Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法
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);
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
您可能感兴趣的文章:
- Android如何避免切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题
- Android解决Fragment中使用SurfaceView切换时闪一下黑屏的问题
- Android Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
- 【转】Android Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
- [Android Pro] Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
- Android 解决SurfaceView切换导致界面闪烁,短暂黑屏问题。
- 【android】把view保存为图片的方法以及解决保存后图片背景变黑色的问题
- 解决TextView数据不能更新的问题以及Android Button事件响应函数的两种方法
- 解决 Android 中 View 的 setPivotX 和 setPivotY 不生效的问题以及设置缩放中心的方法
- 解决Android百度地图MapView在ViewPager中Fragment切换黑屏问题
- Android WebView问题汇总以及解决方法
- 解决Android使用SurfaceView播放视频结束后黑屏问题
- android 切换至surfaceView闪屏(黑屏)
- SurfaceView左右滑动切换黑屏问题解决方式
- Android截屏SurfaceView黑屏问题的解决办法
- 解决Android SurfaceView绘制触摸轨迹闪烁问题的方法
- android 解决fragment中有百度高德等地图,切换出现闪一下黑屏的问题
- Android 切换至surfaceView闪屏(黑屏)
- Fragment中包含surfaceView出现闪屏问题解决方法
- Android截屏SurfaceView黑屏问题的解决办法