2015-12-22 28 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覆盖效果良好。但我无法使用导航栏。

相关问题