SurfaceView中hasSurface的含义是什么?

时间:2014-01-08 12:46:13

标签: android surfaceview

我发现许多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.
     }
  }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

只要Surface尚未生效,我们就无法从SurfaceHolder获取Canvas。但是,我们可以通过以下语句检查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);
    }
  }