Android切換至SurfaceView時(shí)閃屏(黑屏閃一下)以及黑屏移動(dòng)問(wèn)題的解決方法
1.最近的項(xiàng)目中,有一個(gè)Activity用到Fragment+ViewPager,其中一個(gè)fragment中實(shí)現(xiàn)了視頻播放的功能,包含有SurfaceView。結(jié)果,每次打開(kāi)程序第一次進(jìn)入到該Activity時(shí)都會(huì)閃屏黑一下。原因就出在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因?yàn)椴煌谝话愕膙iew,它有自己良好的緩沖以及數(shù)據(jù)存取機(jī)制,系統(tǒng)對(duì)他有特殊處理。當(dāng)surfaceview第一次在當(dāng)前activity上添加的時(shí)候,系統(tǒng)會(huì)給WindowManager重新排布局,relayout,這樣就會(huì)黑一下,這個(gè)只會(huì)出現(xiàn)在第一次,以后再添加surfaceview時(shí)就不會(huì)黑屏了。
解決:
可以在沒(méi)有進(jìn)入surfaceview的界面(比如很多程序一進(jìn)去就有個(gè)Loading界面)的時(shí)候,在其它界面的layout.xml文件中添加一個(gè)SurfaceView,寬和高都弄成0dp,這樣對(duì)布局沒(méi)影響,而且這個(gè)東西使得surfaceview第一次出現(xiàn)了,那么也就是說(shuō),下一次我們真正用到SurfaceView的時(shí)候就不會(huì)閃屏了。
2.viewpager中有surfaceview播放視頻,來(lái)回滑屏?xí)r,關(guān)于黑屏移動(dòng)問(wèn)題
解決:
surfaceview.setZorderTop(true);
surfaceview.getHolder().setFormat(SurfaceView.TRANSPARENT);
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android跟隨手指移動(dòng)的控件demo實(shí)例
- Android自定義View實(shí)現(xiàn)跟隨手指移動(dòng)的小兔子
- Android繪制跟隨手指移動(dòng)的小球
- Android自定義圓形View實(shí)現(xiàn)小球跟隨手指移動(dòng)效果
- Android實(shí)現(xiàn)拖動(dòng)小球跟隨手指移動(dòng)效果
- Android實(shí)現(xiàn)View拖拽跟隨手指移動(dòng)效果
- Android中View跟隨手指移動(dòng)效果
- Android View移動(dòng)的六種方法小結(jié)
- Android View移動(dòng)的3種方式總結(jié)
- Android自定義View實(shí)現(xiàn)跟隨手指移動(dòng)
相關(guān)文章
詳解Android Material Design自定義動(dòng)畫(huà)的編寫(xiě)
這篇文章主要介紹了詳解Android Material Design自定義動(dòng)畫(huà)的編寫(xiě),其中對(duì)Activity的過(guò)渡動(dòng)畫(huà)進(jìn)行了重點(diǎn)講解,需要的朋友可以參考下2016-04-04android studio 4.0 新建類(lèi)沒(méi)有修飾符的方法
這篇文章主要介紹了android studio 4.0 新建類(lèi)沒(méi)有修飾符的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10關(guān)于Android Fragment對(duì)回退棧的詳細(xì)理解
這篇文章主要介紹了Android Fragment的回退棧示例詳細(xì)介紹的相關(guān)資料,在Android中Fragment回退棧是由Activity管理的,每個(gè)Activity都有自己的回退棧,其中保存了已經(jīng)停止(處于后臺(tái))的Fragment實(shí)例,需要的朋友可以參考下2016-12-12android 把float轉(zhuǎn)換成Int的實(shí)例講解
今天小編就為大家分享一篇android 把float轉(zhuǎn)換成Int的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07解決android報(bào)錯(cuò):Intel HAXM is required to run this AVD
這篇文章主要介紹了解決android報(bào)錯(cuò):Intel HAXM is required to run this AVD,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11Android中的Intent Filter匹配規(guī)則簡(jiǎn)介
這篇文章主要為大家詳細(xì)介紹了Android中的Intent Filter匹配規(guī)則,感興趣的小伙伴們可以參考一下2016-04-04Android實(shí)現(xiàn)自動(dòng)點(diǎn)擊無(wú)障礙服務(wù)功能的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)自動(dòng)點(diǎn)擊無(wú)障礙服務(wù)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04ActivityManagerService廣播并行發(fā)送與串行發(fā)送示例解析
這篇文章主要為大家介紹了ActivityManagerService廣播并行發(fā)送與串行發(fā)送示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03