Search code examples
androidandroid-framelayoutvitamio

VideoView not visible in FrameLayout when placed over a SurfaceView


My VideoView seems to only be visible when it is the only child of my FrameLayout. When there is a second child -- a SurfaceView (even when the VideoView is created afterwards, giving it a higher z-index) -- then the VideoView is no longer visible.

I have an extension of Vitamio's VideoView:

public class AnimationCanvas extends VideoView{
public static AnimationCanvas animationCanvas;
public AnimationCanvas(Context context, AttributeSet attrs){
    super(context, attrs);
    animationCanvas = this;
    setVideoURI(Uri.parse(MainActivity.toRightPath));
    requestFocus();
    start();
    this.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_UP:
                    System.out.println("UP");
                    setVideoURI(Uri.parse(MainActivity.animationFilePath));
                    requestFocus();
                    start();
                    break;
                case MotionEvent.ACTION_DOWN:
                    System.out.println("Down");
                    break;
            }
            return true;
        }
    });
}

}

And here's the main.xml file:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <com.temporary.alexcameronelijahapp.gui.MainCanvas
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <com.temporary.alexcameronelijahapp.gui.AnimationCanvas
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

The AnimationCanvas works fine when I delete or comment out the MainCanvas view of the main.xml file (these lines):

<com.temporary.gui.MainCanvas
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

I was under the impression that, in a FrameLayout, the Z-indices are assigned in the order in which they were added to the layout (the last one added has the highest z-index). However, the AnimationCanvas is not visible so long as the MainCanvas is there as well...

Any ideas?

EDIT: It would be good to know that I am, in fact, drawing many Bitmaps on my instance of MainCanvas.

Secondly, if I set android:background="#000000" in the xml code of my AnimationCanvas, it becomes visible over the MainCanvas, but the Video itself still won't show (as if the black background is somehow covering up the video?)

EDIT #2: I believe I have found the issue, but still no solution. When I comment out my paint method within my MainCanvas class, meaning that no Bitmaps are painted to the canvas, then the AnimationCanvas is visible. It's almost as if, even though the AnimationCanvas is on top of the MainCanvas, the MainCanvas's locked canvas is somehow on top of the AnimationCanvas's VideoView canvas.


Solution

  • SurfaceView behaves differently from other widgets, when it comes to things like visibility and z-order. For details, see GL Surface and Visibility: Gone. Most importantly, they just ignore visibility. Also note that VideoView is just another example of SurfaceView.

    Try one of the suggestions in the post above. In your case, you might also try using the same SurfaceView as the destination for both your drawing and your video.