恢复到SurfaceView显示黑屏

时间:2012-04-23 09:11:56

标签: android surfaceview

一切正常,当我开始申请时。但是当我进入下一个活动并返回SurfaceView时,它看起来像black。当我调试时,我可以看到需要在画布上绘制的线条,但没有任何显示 这是我的代码:

public class DrawView extends SurfaceView implements Runnable
{

    Thread thread = null;
    SurfaceHolder holder;
    Bitmap background;
    Canvas canvas;
    Matrix matrixUP = new Matrix();
    Matrix matrixDOWN = new Matrix();
    boolean ok = false;

    public DrawView(Context context) 
    {
        super(context);
        holder = getHolder();
    }

    public void run() 
    {
        while(ok)
        {
            if(!holder.getSurface().isValid())
            {
                continue;
            }
            watch_hand = Bitmap.createScaledBitmap(watch_hand, width/4, height/6, false);
            watch_hand_down = Bitmap.createScaledBitmap(watch_hand_down, width/4, height/6, false);
            background = BitmapFactory.decodeResource(getResources(), R.drawable.needles);
            background = Bitmap.createScaledBitmap(background, width, height/2+20, false);
            canvas = holder.lockCanvas();
            matrixUP.setTranslate(CENTER_X - watch_hand.getWidth()/2, height/4 - watch_hand.getHeight() - 40);
            matrixUP.preRotate((float) degreUP , watch_hand.getWidth()/2 , watch_hand.getHeight()-10);

            matrixDOWN.setTranslate(CENTER_X - watch_hand_down.getWidth()/2, height/2 - watch_hand_down.getHeight() - 20);
            matrixDOWN.preRotate((float) degreDOWN , watch_hand_down.getWidth()/2 , 10);

            canvas.drawARGB(255, 255, 255, 255);
            canvas.drawBitmap(background, 0, 0, null);
            canvas.drawBitmap(watch_hand, matrixUP, null);
            canvas.drawBitmap(watch_hand_down, matrixDOWN, null);
            holder.unlockCanvasAndPost(canvas);
        }           
    }

    public void pause()
    {
        ok = false;
        while(true)
        {
            try
            {
                thread.join();
            }catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            break;
        }
        thread = null;
    }

    public void resume()
    {
        ok = true;
        thread = new Thread(this);
        thread.start();
    }           
}

如你所见,我正在使用简历和暂停功能。它们是从onCreate调用的。 为什么会发生这种情况?我该如何解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:1)

答案取决于您如何更改为其他活动。

基本上,当活动暂停,停止或摧毁时,你必须考虑如何存储表面的当前状态。

例如,您可以在活动中重载onSaveInstanceState以保存degreUP和degreDOWN(当前代码片段中未定义它们的方式)。并在onRestoreInstanceState中恢复它们。见Saving Android Activity state using Save Instance State

答案 1 :(得分:0)

从表面视图所在活动的onResume()和onPause()方法调用表面视图的resume()和pause()方法(在您的案例中为DrawView)。你提到你是从onCreate()调用的。

这应该可以解决问题。