一切正常,当我开始我的应用程序时。但是当我进入下一个 Activity 并返回 SurfaceView 时,它看起来像 black
。当我调试时,我可以看到需要在 Canvas 上绘制的线条正在运行,但什么也没有显示。
这是我的代码:
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 调用的。 为什么会发生这种情况,我该如何解决?
谢谢!
最佳答案
答案取决于您如何更改为其他 Activity 。
基本上,您必须考虑如何在 Activity 暂停、停止或销毁时存储 Surface 的当前状态。
例如,您可以在 Activity 中重载 onSaveInstanceState 以保存 degreUP 和 degreDOWN(顺便说一下,它们未在当前代码 fragment 中定义)。并在 onRestoreInstanceState 中恢复它们。见Saving Android Activity state using Save Instance State
关于android - 恢复到 SurfaceView 显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277694/