Sunday 7 January 2018

android - View.SurfaceView, why its member, mSurfaceHolder, returns null from getSurface()?

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!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...