1

我编写了一个具有以下结构的简单游戏。主要 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 步,然后在我的应用程序中,我会发现一切正常。 在此处输入图像描述

我不明白,哪里有重新加载会影响我的图纸!请帮忙!

4

1 回答 1

0

要解决此问题,我需要保存设置并重新启动 Surface。然后加载设置。(参见 LunarLander 示例)

于 2012-07-28T08:06:15.580 回答