2

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; } ... } } 

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

Thanks in advance!

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, you are right, can you explain the differences between them? I downloaded the source code from Android SDK Manager.
I see the instruction from: androidxref.com/5.1.0_r1/xref/frameworks/base/tools/layoutlib, it says "Layoutlib is a custom version of the android View framework designed to run inside Eclipse.The goal of the library is to provide layout rendering in Eclipse that are very very close to their rendering on devices." then, the source code I downloaded from SDK manager is just the 'source for Android SDK' rather than the Android framework source code? Am I right? Thanks.
Yes, but only a few of them differs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.