我有自定义的surfaceview,我想覆盖其他视图。
所以我尝试了下面的答案。 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 :(得分:0)
我自己解决了。消失的surfaceview问题是因为导航栏。
我认为导航栏位于我的自定义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可以很好地覆盖。但是我无法使用导航栏。