Android SurfaceView中hasSurface的含义是什么

Android SurfaceView中hasSurface的含义是什么,android,surfaceview,Android,Surfaceview,我发现许多SurfaceViewdemo使用hasSurface。但我不明白。hasSurface是什么意思?有人帮我吗 import android.content.Context; import android.graphics.Canvas; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MySurfaceView extends SurfaceView imple

我发现许多
SurfaceView
demo使用hasSurface。但我不明白。hasSurface是什么意思?有人帮我吗

    import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements
  SurfaceHolder.Callback {

  private SurfaceHolder holder;
  private MySurfaceViewThread mySurfaceViewThread;
  private boolean hasSurface;

  MySurfaceView(Context context) {
    super(context);
    init();
  }

  private void init() {
    // Create a new SurfaceHolder and assign this
    // class as its callback.
    holder = getHolder();
    holder.addCallback(this);
    hasSurface = false;
  }

  public void resume() {
    // Create and start the graphics update thread.
    if (mySurfaceViewThread == null) {
       mySurfaceViewThread = new MySurfaceViewThread();

       if (hasSurface == true)
         mySurfaceViewThread.start();
    }
  }

  public void pause() {
     // Kill the graphics update thread
     if (mySurfaceViewThread != null) {
       mySurfaceViewThread.requestExitAndWait();
       mySurfaceViewThread = null;
      }
  }

  public void surfaceCreated(SurfaceHolder holder) {
    hasSurface = true;
    if (mySurfaceViewThread != null)
       mySurfaceViewThread.start();
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    hasSurface = false;
    pause();
  }

  public void surfaceChanged(SurfaceHolder holder, int format,
                           int w, int h) {
    if (mySurfaceViewThread != null)
      mySurfaceViewThread.onWindowResize(w, h);
  }

  class MySurfaceViewThread extends Thread {
      private boolean done;

      MySurfaceViewThread() {
         super();
         done = false;
      }

     @Override
     public void run() {
       SurfaceHolder surfaceHolder = holder;

       // Repeat the drawing loop until the thread is stopped.
       while (!done) {
         // Lock the surface and return the canvas to draw onto.
         Canvas canvas = surfaceHolder.lockCanvas();
         // TODO: Draw on the canvas!
         // Unlock the canvas and render the current image.
         surfaceHolder.unlockCanvasAndPost(canvas);
        }
      }

     public void requestExitAndWait() {
       // Mark this thread as complete and combine into
       // the main application thread.
       done = true;
        try {
           join();
        } catch (InterruptedException ex) { }

      }

     public void onWindowResize(int w, int h) {
       // Deal with a change in the available surface size.
     }
  }
}

曲面是在SurfaceView窗口可见时创建的,因此您需要知道代码是否可以访问它。您应该实现
surfaceCreated()
surfaceDestroyed()
,以便在显示和隐藏窗口时在创建和销毁曲面时得到通知,因此基本上
hasSurface
(或您使用的任何名称)为简单起见,保留您的surfrace的最后已知状态。

我们无法从SurfaceHolder获取画布,只要该曲面尚未生效。但是,我们可以通过以下语句检查曲面是否已创建:

boolean isCreated = surfaceHolder.getSurface().isValid();
我假设HassSurface与getSurface()类似。isValid()


演示代码在这里可能很有用,这里使用的是hasSurface。我已经添加了演示代码,您使用的是哪个版本?我在文档中没有看到hasSurface。“私有布尔hasSurface;”有必要吗?让我们看一下演示,“hasSurface”用于恢复函数,但我认为resume中的hasSurface总是错误的,这是真的吗?SurfaceView是异步处理的,不必总是false,因为在没有任何东西正在运行时不调用onResume。不必总是false,因为在没有任何东西正在运行时不调用onResume???抱歉,我不确定您的意思,因此没有必要使用surfaceHolder.getSurface().isValid();在从SurfaceHolder获取画布之前。如果不确保Surface存在,则在尝试调用与画布相关的方法时会出现异常。而不是在检查surfaceHolder后在run方法中使用HassSurface。getSurface().isValid()返回true,然后继续执行其余代码。否则调用continue直到surface有效。你读过我的代码了吗?你同意我的说法吗,简历中的hasSurface总是假的?这取决于是否在简历之前调用onSurfaceCreated,因为onSurfaceCreated是唯一将hasSurface设置为true的地方。我同意你的看法,简历中的纷争永远不会是真的。要运行onSurfaceCreated,线程需要启动,但要启动,线程取决于HassSurface是否为true。僵局
 @Override
 public void run() {


   SurfaceHolder surfaceHolder = holder;

   // Repeat the drawing loop until the thread is stopped.
   while (!done) {
     if(!holder.getSurface().isValid())
        continue;
     // Lock the surface and return the canvas to draw onto.
     Canvas canvas = surfaceHolder.lockCanvas();
     // TODO: Draw on the canvas!
     // Unlock the canvas and render the current image.
     surfaceHolder.unlockCanvasAndPost(canvas);
    }
  }