|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectSurfaceView
org.hermit.android.core.SurfaceRunner
public abstract class SurfaceRunner
Common base for applications with an animated view. This class can be used in games etc. It handles all the setup states of a SurfaceView, and provides a Thread which the app can use to manage the animation.
When using this class in an app, the app context must call these methods (usually from its corresponding Activity methods):
The surface is enabled once it is created and sized, and
onStart()
and onResume()
have been called. You then
start and stop it by calling surfaceStart()
and
surfaceStop()
.
Field Summary | |
---|---|
static int |
LOOPED_TICKER
Surface runner option: use a Looper to drive animations. |
static int |
SURFACE_DYNAMIC
Surface runner option: handle configuration changes dynamically. |
Constructor Summary | |
---|---|
SurfaceRunner(Context app)
Create a SurfaceRunner instance. |
|
SurfaceRunner(Context app,
AttributeSet attrs)
Create a SurfaceRunner instance. |
|
SurfaceRunner(Context app,
int options)
Create a SurfaceRunner instance. |
Method Summary | |
---|---|
protected abstract void |
animStart()
We are starting the animation loop. |
protected abstract void |
animStop()
We are stopping the animation loop, for example to pause the app. |
protected abstract void |
appSize(int width,
int height,
Bitmap.Config config)
Set the screen size. |
protected abstract void |
appStart()
The application is starting. |
protected abstract void |
appStop()
The application is closing down. |
protected abstract void |
doDraw(Canvas canvas,
long now)
Draw the current frame of the application. |
protected abstract void |
doUpdate(long now)
Update the state of the application for the current frame. |
Bitmap |
getBitmap()
Get a Bitmap which is the same size and format as the surface. |
Bitmap |
getBitmap(int w,
int h)
Get a Bitmap of a given size, in the same format as the surface. |
java.lang.String |
getRes(int resid)
Get the String value of a resource. |
void |
onPause()
Pause the app. |
void |
onResume()
We're resuming the app. |
void |
onStart()
The application is starting. |
void |
onStop()
The application is closing down. |
boolean |
onSurfaceThread()
Determine whether the caller is on the surface's animation thread. |
void |
onWindowFocusChanged(boolean hasWindowFocus)
Handle changes in focus. |
boolean |
optionSet(int option)
Check whether the given option flag is set on this surface. |
void |
postUpdate()
Asynchronously schedule an update; i.e. |
void |
setDebugPerf(boolean enable)
Turn display of performance info on or off. |
void |
setDebugPos(int x,
int y)
Set the screen position at which we display performance info. |
void |
setDelay(long delay)
Set the delay in ms in each iteration of the main loop. |
void |
statsCount(int index,
int val)
Increment a performance counter. |
protected void |
statsCreate(java.lang.String[] labels)
Reserve space in the stats display for some application performance stats. |
void |
statsTime(int index,
long val)
Record a performance timer. |
void |
surfaceChanged(SurfaceHolder holder,
int format,
int width,
int height)
This is called immediately after any structural changes (format or size) have been made to the surface. |
void |
surfaceCreated(SurfaceHolder holder)
This is called immediately after the surface is first created. |
void |
surfaceDestroyed(SurfaceHolder holder)
This is called immediately before a surface is destroyed. |
void |
surfaceStart()
Start the surface running. |
void |
surfaceStop()
Stop the surface running. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int SURFACE_DYNAMIC
public static final int LOOPED_TICKER
Constructor Detail |
---|
public SurfaceRunner(Context app)
app
- The application context we're running in.public SurfaceRunner(Context app, int options)
app
- The application context we're running in.options
- Options for this SurfaceRunner. A bitwise OR of
SURFACE_XXX constants.public SurfaceRunner(Context app, AttributeSet attrs)
app
- The application context we're running in.attrs
- Layout attributes for this SurfaceRunner.Method Detail |
---|
public boolean optionSet(int option)
option
- The option flag to test; one of SURFACE_XXX.
public void setDelay(long delay)
delay
- The time in ms to sleep each time round the main
animation loop. If zero, we will not sleep,
but will run continuously.
If you want to do all your animation under
direct app control using postUpdate()
,
just set a large delay. You may want to consider
using 1000 -- i.e. one second -- to make sure
you get a refresh at a decent interval.
public void surfaceCreated(SurfaceHolder holder)
holder
- The SurfaceHolder whose surface is being created.public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
holder
- The SurfaceHolder whose surface has changed.format
- The new PixelFormat of the surface.width
- The new width of the surface.height
- The new height of the surface.public void surfaceDestroyed(SurfaceHolder holder)
holder
- The SurfaceHolder whose surface is being destroyed.public void onStart()
public void onResume()
public void surfaceStart()
public void surfaceStop()
public void onPause()
public void onStop()
public void onWindowFocusChanged(boolean hasWindowFocus)
hasWindowFocus
- True iff we have focus.public void postUpdate()
protected abstract void appStart()
protected abstract void appSize(int width, int height, Bitmap.Config config)
width
- The new width of the surface.height
- The new height of the surface.config
- The pixel format of the surface.protected abstract void animStart()
doUpdate() and doDraw() may be called from this point on.
protected abstract void animStop()
doUpdate() and doDraw() will not be called from this point on.
protected abstract void appStop()
protected abstract void doUpdate(long now)
Applications must override this, and can use it to update for example the physics of a game. This may be a no-op in some cases.
doDraw() will always be called after this method is called; however, the converse is not true, as we sometimes need to draw just to update the screen. Hence this method is useful for updates which are dependent on time rather than frames.
now
- Current time in ms.protected abstract void doDraw(Canvas canvas, long now)
Applications must override this, and are expected to draw the entire screen into the provided canvas.
This method will always be called after a call to doUpdate(), and also when the screen needs to be re-drawn.
canvas
- The Canvas to draw into.now
- Current time in ms. Will be the same as that
passed to doUpdate(), if there was a preceeding
call to doUpdate().public java.lang.String getRes(int resid)
resid
- The ID of the resource we want.
public Bitmap getBitmap()
public Bitmap getBitmap(int w, int h)
w
- Desired width in pixels.h
- Desired height in pixels.
public boolean onSurfaceThread()
public void setDebugPerf(boolean enable)
enable
- True to enable performance display.public void setDebugPos(int x, int y)
x
- Screen X position.y
- Screen Y position.protected void statsCreate(java.lang.String[] labels)
setDebugPerf(boolean enable)
is passed true. Each stat is
subsequently referred to by its index in this labels array.
This method must be called before appStart() in order for the app's stats to be displayed. After appStart() is called, the stats content is frozen until the next appStop() / appStart(). Typically the app should invoke this method from its constructor. However this method is, of course, optional.
labels
- Labels for the app's stats, one label
per stat. Labels need to be 7 chars or less.public void statsCount(int index, int val)
index
- Index of the stat to bump (its index in the
"labels" argument to
statsCreate(String[] labels)
).val
- Amount to add to the counter.public void statsTime(int index, long val)
index
- Index of the stat to record (its index in the
"labels" argument to
statsCreate(String[] labels)
).val
- The time value for this iteration.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |