문제

I'm trying to show a SurfaceView inside an Activity that has a theme that extends Theme.Dialog (you can think as I'm trying to show it inside a Dialog class), but I'm having some layout glitch.

I couldn't make a picture because the glitch vanish when taking a screenshot :/ but anyway what you see is that the "hole" of SurfaceView is moved from its original position and it create some weird overlay effect...

The problem seems to be related with the flag windowIsFloating, if I set it to false the glitch goes away...

Any idea for a possible workaround to keep using windowIsFloating flag?


Simple layout to reproduce the issue:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff">

<FrameLayout android:id="@+id/container"
             android:layout_width="250dp"
             android:layout_height="250dp"
             android:layout_centerInParent="true"
             android:padding="30dp"
             android:background="#ff0">

    <SurfaceView android:id="@+id/surface"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent" />

</FrameLayout>

and modify your activity's theme like this to reproduce the issue:

<style name="MyTheme" parent="@android:style/Theme">
    <item name="android:windowIsFloating">true</item>
</style>
도움이 되었습니까?

해결책

It seems like there is no way to solve the glitch, simply windowIsFloating flag is very buggy with SurfaceView...

So, following also the answer here How to create a transparent activity WITHOUT windowIsFloating, I ended up by creating a theme that extends Dialog's theme but set windowIsFloating flag to false and windowIsTranslucent to true.

That way you need manually to create a layout that behave like a Dialog, but you don't have any glitch and your background can be fully transparent :)


Symple example of a theme that will do the trick:

<style name="MyTheme" parent="@android:style/Theme">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

Then you need to apply margins to your layout, otherwise it will be fullscreen as a normal Activity.

다른 팁

I share this because it may be useful to others. I've run into a transparency issue with a SurfaceView inside a dialog and the following answer helped me resolve it (and I did not have to use windowIsFloating=false as suggested here although it also does the trick):

https://stackoverflow.com/a/7061396/875442

As it turned out my problem was both a Z-ordering and a transparency issue.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top