首页 文章

View.SurfaceView,为什么它的成员mSurfaceHolder从getSurface()返回null?

提问于
浏览
0

这些天我正在研究Android游戏开发 . 我遇到了一个关于SurfaceView \ SurfaceHolder的问题 . 当我在android sdk 22中阅读View / SurfaceView.java的源代码时,我很困惑 . 以下是代码:

public class SurfaceView extends MockView {
    ...    
    public SurfaceHolder getHolder() {
        return mSurfaceHolder;
    }

    private SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
        ...

        @Override
        public Surface getSurface() { return null; }
        @Override
        public Canvas lockCanvas() { return null; }
        ...

    }
}

我知道,mSurfaceHolder.getSurface()\ lockCanvas非常重要,但它返回null!所以,我认为这个mSurfaceHolder可能会处理其他一些步骤 . 但我已经学习了一个关于SurfaceView的例子,但我没有想出任何处理mSurfaceHolder的特殊步骤,该示例的代码如下:

public class SurfaceViewTest extends Activity {

    String TAG = "SurfaceViewTest";
    FastRenderView surfaceView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_surface_view_test);
        surfaceView = new FastRenderView(this);
        setContentView(surfaceView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        surfaceView.doResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is  
        // present.
        getMenuInflater().inflate(R.menu.menu_surface_view_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * customized surfaceview class
     */
    public class FastRenderView extends SurfaceView implements Runnable {

        boolean running = false;
        SurfaceHolder holder = null;
        Thread thread = null;

        public FastRenderView(Context context) {
            super(context);
            // holder
            // getHolder() returns a SurfaceHolder implementation, it
            // isn't null, but it contains nothing.
            holder = getHolder();
            if (holder == null)
                Log.e(TAG, "failed to get valid holder");

        }

        public void doResume() {
            thread = new Thread(this);
            thread.start();
            running = true;
        }

        public Random rand = new Random();
        @Override
        public void run() {
            while (running) {
                // from android sdk 22, SurfaceView.java, we can see, this
                // holder's getSurface() returns null.
                // but why? it returns null, so here it an exception 
                // should be thrown out!
                if (!holder.getSurface().isValid()) {
                    continue;
                }

                Canvas canvas = holder.lockCanvas();
                if (canvas == null) {
                    Log.e(TAG, "get an invalid canvas to draw");
                    continue;
                }
                canvas.drawRGB(
                    rand.nextInt(255), 
                    rand.nextInt(255), 
                    rand.nextInt(255));
                holder.unlockCanvasAndPost(canvas);

                // sleep
                try {
                    Thread.sleep(1000);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
            }
            Log.i(TAG, "running ...");
        }

        public void doPause() {

            running = false;
            while (true) {
                try {
                    thread.join();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

提前致谢!

1 回答

  • 0

    我怕你提到错误的源代码 . 您发布的SurfaceView代码来自layoutlib而不是真正的框架源代码,SurfaceView的源代码是here

相关问题