3

In my application I have a Translucent Activity with android:windowIsTranslucent is set to true. Activity contains a FrameLayout to which I programmatically add SurfaceView.

By default SurfaceView is drawing bitmap, as per the Activity background Theme, which in my case in Translucent, so SurfaceView is showing transparent bitmap.

For setting background I used SurfaceHolder callbacks in following way:

surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Canvas canvas = holder.lockCanvas();
                if (canvas != null) {
                    canvas.drawRGB(255, 255, 255);
                    holder.unlockCanvasAndPost(canvas);
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Canvas canvas = holder.lockCanvas();
                if (canvas != null) {
                    canvas.drawRGB(255, 255, 255);
                    holder.unlockCanvasAndPost(canvas);
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }
        });

In surfaceCreated()/surfaceChanged() I am drawing a color.

When the FrameLayout containing SurfaceView gets resized then surfaceDestroyed() callbacks invokes and default Transparent background shows to user until surfaceCreated() invokes again.

So user sees Transparent background for a moment.

My questions is:

  1. Is SurfaceView uses Activity window color to draw default bitmap?
  2. Can we change the color that SurfaceViewuses to draw default bitmap?
Sagar Trehan
  • 2,401
  • 2
  • 24
  • 32
  • Doesn't really answer your question, but use a TextureView instead of a SurfaceView. SurfaceViews are terrible and cause nothing but problems. – Xaver Kapeller May 13 '16 at 13:46
  • @XaverKapeller: Thanks for suggestion. But using TextureView instead of SurfaceView is not in my hand. – Sagar Trehan May 13 '16 at 13:48
  • You may be able to fix your issue by just assigning a background to the SurfaceView. By default the Surfaceview actually draws nothing (hence why you "see" the transparent Activity behind it)." It is though, just a regular View at heart. – DeeV May 13 '16 at 14:32
  • @DeeV:My SurfaceView is playing video content when I set "setBackgroundColor()" to it then video is not visible, but playing and I think it is behind the background. Please help – Sagar Trehan May 13 '16 at 14:51
  • Two ideas. Use either `SurfaceView#setZOrderMediaOverlay()` or `SurfaceView#setZOrderOnTop()`. Be warned, using this will put the video on top of *everything* means you can't overlay Views over the SurfaceView. The second idea is to set the background to your color, but then set it to "null" when `onSurfaceCreated()` is called. Then set the background again when `onSurfaceDestroyed()` is called. – DeeV May 13 '16 at 15:18
  • I'm not setting these as answers since I'm not 100% sure they will fix your problem. – DeeV May 13 '16 at 15:18
  • @DeeV: Thanks for help. I already tried SurfaceView#setZOrderMediaOverlay() or SurfaceView#setZOrderOnTop() approach but as you I need overlay so I ignore this approach. But I am trying second solution. Will update in some time. Thanks – Sagar Trehan May 13 '16 at 15:22
  • Bear in mind that SurfaceViews have two parts, the Surface and the View. These are manipulated and updated independently. You can treat the View part like a custom View, but the Surface part is an independent layer that is partially managed by a different process, so there can be visual glitches when things move around. @XaverKapeller: SurfaceViews are actually very useful, but they are widely misunderstood, and the multitude of bad tutorials and examples do not help. See [Grafika](https://github.com/google/grafika) for examples of proper use. – fadden May 13 '16 at 20:59
  • @fadden/@Deev: Attempted to fix by following approach but it didn't help: Setting background to SurfaceView --> Drawing color of Surface in onSurfaceCreated() and setting background to null --> When surface destroyed then setting background to black color. It works well, but as @fadden said there are some glitches while resizing the view that transparent view visible for a moment. – Sagar Trehan May 14 '16 at 03:22
  • @Deev/@fadden: One query: When surface view gets destroyed than all content that we drawn on it will also gone? – Sagar Trehan May 17 '16 at 13:14
  • @SagarTrehan did you find a solution in the end? I'm currently facing the exact same issue. – jenzz Jul 26 '16 at 10:12
  • @jenzz: No I didn't find any solution yet. I add a hack to fix this issue. Hack is "While loading content in player I add a black background view in z order on top of player view and once onPlaying() callback invokes I set visibility of z order view to GONE". This is DEFINTELY NOT RIGHT SOLUTION I am still struggling. Please let me know as well if found something. – Sagar Trehan Jul 26 '16 at 11:41

0 Answers0