我用以下结构编写了一个简单的游戏。主xml布局具有用户视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menuRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.sokolovlev.UFOtest03.MenuView
android:id="@+id/menuView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
此视图有两个文件。首先是用户MenuView:
package com.sokolovlev.UFOtest03;
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MenuView extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder mSurfaceHolder; //Draw surface
public static MenuManager mMenuManager; //Draw manager
public MenuView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Surface events registration
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mMenuManager = new MenuManager(mSurfaceHolder, context);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
mMenuManager.initPositions(height, width);
}
@SuppressWarnings("static-access")
@Override
public void surfaceCreated(SurfaceHolder holder)
{
mMenuManager.setRunning(true);
try
{
mMenuManager.start();
}
catch (Exception e) { }
}
@SuppressWarnings("static-access")
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
mMenuManager.setRunning(false);
while (retry)
{
try
{
mMenuManager.join();
retry = false;
}
catch (InterruptedException e) { }
}
}
}
第二个文件是这个抽奖经理:
package com.sokolovlev.UFOtest03;
import ...
public class MenuManager extends Thread
{
private SurfaceHolder mSurfaceHolder;
private static boolean mRunning;
private int _screenHeight;
private int _screenWidth;
private ...
Context c;
private ...
public MenuManager(SurfaceHolder surfaceHolder, Context context)
{
mSurfaceHolder = surfaceHolder;
mRunning = false;
c = context;
...
}
public static void setRunning(boolean running)
{
mRunning = running;
}
@Override
public void run()
{
while (mRunning)
{
Canvas canvas = null;
try
{
// подготовка Canvas-а
canvas = mSurfaceHolder.lockCanvas();
canvas.drawRGB(0, 0, 0);
synchronized (mSurfaceHolder)
{
//All drawing
}
}
catch (Exception e) { }
finally
{
if (canvas != null)
{
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
一切正常! (步骤№1)但是如果我按下中央按钮,或者我有来电,或者我打电话给任务管理员(步骤№2)并回到我的应用程序 - 我没有我的绘图,只有黑屏(步骤№3) 。但如果我走到第2步,然后在我的应用程序中,我看到一切正常。
我不明白,哪里有重装会影响我的图纸!求救!
答案 0 :(得分:0)
要解决此问题,我需要保存设置并重新启动表面。然后加载设置。 (参见LunarLander示例)