surfaceview disappears when i call setZOderOnTop(true)

I have custom surfaceview and I want to overlay on other views.

So I tried below answers. how to make surfaceview transparent

When I call setZOderOnTop(true) on my custom surfaceview, it shortly overlays other views. But surfaceview disappears after short time.

Also surfaceview appears shortly when I on/off my android device.

I have no idea why it diappears. please help me!

there is my main activity's onCreate

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);


    int sample_id=R.drawable.d7d;
    piece_size=105;
    sample= BitmapFactory.decodeResource(this.getResources(),
            sample_id);
    int returnv[] = centercrop(sample.getWidth(),sample.getHeight());
    int ncols=returnv[0]/piece_size;
    int nrows=returnv[1]/piece_size;

    returnv[0]=ncols*piece_size;
    returnv[1]=nrows*piece_size;
    sample=getResizedBitmap(sample,returnv[0],returnv[1]);

    Bundle config = ExampleJigsawConfigurations.customsettings(returnv[0],returnv[1],ncols,nrows,returnv[0],returnv[1],sample,sample_id,piece_size);

    puzzleSurface = new PuzzleCompactSurface(this,config);



    JigsawPuzzle jigsawPuzzle = new JigsawPuzzle(this, config);
    puzzleSurface.setPuzzle(jigsawPuzzle);

    setContentView(R.layout.puzzle_layout);
    RelativeLayout surfaceView=(RelativeLayout)findViewById(R.id.surfaceframe);
    surfaceView.addView(puzzleSurface);
}

layout Xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:clipToPadding="false">

    <include
        android:id="@+id/toolbar2"
        layout="@layout/toolbar2" />
    <LinearLayout 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:orientation="horizontal"
        android:layout_weight="1"
        android:background="#F7F1EB"
        android:clipToPadding="false">
        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:id="@+id/surfaceframe"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </RelativeLayout>
        </FrameLayout>

    </LinearLayout>
</LinearLayout>

custom surfaceview

public PuzzleCompactSurface(Context context,Bundle config) {
    super(context);

    getHolder().setFormat(PixelFormat.TRANSLUCENT);
    getHolder().addCallback(this);

    gameThread = new PuzzleThread(getHolder(), context, this);
    this.config=config;


    MAX_PUZZLE_PIECE_SIZE= config.getBundle("board").getInt("pieceSize");
    CLIP=Math.round((float) 10 / 64 * MAX_PUZZLE_PIECE_SIZE);
    setFocusable(true);
    setZOrderOnTop(true);
}

I solved it by my self. disappearing surfaceview issue was because of navigation bar.

I think navigation bar was on the top of my custom surfaceview, and it caused surfaceview hide.

    View decorView = getWindow().getDecorView();
    // Hide both the navigation bar and the status bar.
    // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
    // a general rule, you should design your app to hide the status bar whenever you
    // hide the navigation bar.
    uiOptions =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    decorView.setSystemUiVisibility(uiOptions);

By putting this code on main activity, surfaceview overlays well. But I can't use navigation bar.

链接地址: http://www.djcxy.com/p/66774.html

上一篇: 清除功能在自定义SurfaceView Android中不起作用

下一篇: 当我调用setZOderOnTop(true)时,surfaceview消失