VideoView inside fragment causes black screen flicking

后端 未结 4 1195
[愿得一人]
[愿得一人] 2020-12-03 13:38

We have an Android application running on Android API 4.0. One of the activities has a layout that divides the screen in 2 parts. On the left side we have a static part with

相关标签:
4条回答
  • 2020-12-03 14:07

    Just add transparent background color of VideoView. It will solve the problem.

    android:background="@color/transparent"
    
    0 讨论(0)
  • 2020-12-03 14:19

    I was able to fix this by adding a 0px by 0px SurfaceView in the layout of the parent activity of the fragment:

    <SurfaceView
            android:layout_width="0px"
            android:layout_height="0px" />
    
    0 讨论(0)
  • 2020-12-03 14:24

    If you wonder why adding a zero sized SurfaceView solves the problem, you can take a look at this question SurfaceView flashes black on load. It explains well.

    Quoted from Evos's answer

    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.

    And here is a cleaner way to solve this problem, call this before you call setContentView(), just tested on my ViewPager with some Fragments containing VideoView, it works as good as adding zero-sized SurfaceView:

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    
    0 讨论(0)
  • 2020-12-03 14:32

    KennyDs is right, but better be sure to hide the SurfaceView, otherwise you'll have really weird behavior if you animate fragments, then you won't see anything but black screen in the place of the animated fragment. Fortunately this collateral issue has a easy fix too:

    <SurfaceView
        android:layout_width="0px"
        android:layout_height="0px"
        android:visibility="gone" />
    
    0 讨论(0)
提交回复
热议问题