Reputation: 111
I want to write a program that a ball move on screen, because of this, i read many tutorial and i cant understand this line:
mHolder = getHolder();
mHolder.addCallback(this);
what is "this
" parameter in addCallback
method? why i use this method?
i read about SurfaceView
and SurfaceHolder
and Canvas
but i cant good understand SurfaceHolder
.what does it do?
I know maybe my question was stupid but this is a question ! Please explain about this concept.
Upvotes: 4
Views: 5539
Reputation: 38707
"this" is a basic Java/OO concept ... here it refers to the object calling mHolder.addCallback(). In your class declaration you will see at the top that it "implements SurfaceHolder.Callback".
Since your class implements SurfaceHolder.Callback, it IS an instance of SurfaceHolder.Callback, and it can therefore pass a reference to itself ('this') to functions that take a SurfaceHolder.Callback parameter.
As for the "concept" of SurfaceHolder, it's merely the interface for app code to interact with a Surface. It's used in SurfaceView.java ... look for the private SurfaceHolder mSurfaceHolder declaration.
This is 100% speculation but I imagine SurfaceHolder was separated out this way because the designers envisaged having other kinds of SurfaceViews and wanted a standard way for app code to interact with them.
Upvotes: 5