本文介绍了View.SurfaceView,为什么它的成员,mSurfaceHolder,从getSurface返回null()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习的Andr​​oid游戏开发的这些日子。我碰到过一个关于SurfaceView \\ SurfaceHolder问题。当我读到查看/ SurfaceView.java在Android SDK中22源$ C ​​$ C,我很困惑。以下是code:

I am studying Android Game Development these days. I come across a problem about SurfaceView\SurfaceHolder. When I read the source code of View/SurfaceView.java in android sdk 22, I am confused. following is the code:


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一个例子,但是我没有figout任何特殊的步骤来处理mSurfaceHolder,示例的code是如下:

I know, mSurfaceHolder.getSurface()\lockCanvas are very important, but it returns null! So, I think this mSurfaceHolder may be dealt with some other steps. But I have learned an example about SurfaceView, but I didn't figout out any special steps to deal with mSurfaceHolder, the example's code is as following:



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

在此先感谢!

推荐答案

我怕你指的是错误的来源$ C ​​$ C。 SurfaceView的code您发表与不是真正的框架源$ C ​​$ C,SurfaceView源$ C ​​$ c是的

I'm afraid you refer to the wrong source code. the code of SurfaceView you post is from layoutlib not the real framework source code, the source code of SurfaceView is here

这篇关于View.SurfaceView,为什么它的成员,mSurfaceHolder,从getSurface返回null()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:10