2015-12-22 47 views
-1

我有自定義的surfaceview,我想覆蓋其他視圖。當我調用setZOderOnTop時,surfaceview消失(true)

所以我試着下面的答案。 how to make surfaceview transparent

當我在我的自定義表面視圖上調用setZOderOnTop(true)時,它很快覆蓋了其他視圖。但是短時間後Surfaceview會消失。

當我開啓/關閉我的android設備時,Surfaceview也會很快出現。

我不知道爲什麼會出現這種情況。請幫幫我!

有我的主要活動的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); 
} 

佈局的xml文件

<?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> 

定製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); 
} 

回答

0

我解決它在我的自我。消失的surfaceview問題是因爲導航欄。

我覺得導航欄在我的自定義表面視圖的頂部,它導致surfaceview隱藏。

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); 

通過將此代碼放在主要活動上,surfaceview覆蓋效果良好。但我無法使用導航欄。

相關問題