SurfaceView中的hasSurface是什么意思



我发现很多SurfaceView演示都使用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.
     }
  }
}

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

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

boolean isCreated = surfaceHolder.getSurface().isValid();

我假设hasSurface类似于getSurface().isValid()。

 @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);
    }
  }

最新更新