SurfaceView refresh in android
for SurfaceView you should create a second class which will create a background Thread for managing gameplay and rendering, so that when it's rendering it will lock your screen hence nothing can interrupt it, and you will put your graphic logic on the first class which extends SurfaceView. The link below seems explaning it in detail with example code in it. http://www.mathcs.org/java/android/game_surfaceview.html
hguser
Updated on June 04, 2022Comments
-
hguser about 2 years
I am working on an application which need to extend the SurfaceView. Now I meet some problems with the redraw of the view.
For a normal android.view.View, I will override the
onDraw()
method, put all my graphic login inside this method . And call theinvalidate()
method when I want it redraw.But how about the SurfaceView ? It seems that
postInvalidate()
can be used to call its redraw. But where can I put the graphic logic?Should I put the graphci login to
Surface.Callback.surfaceCreated()
? If so,when I call methodpostInvalidate()
,does it will call thesurfaceCreated()
internally? If not, how to fix it? -
hguser about 11 yearsThen does it mean that the you do not need to call the
postInvalidate()
method to the surfaceveiw,since you control the drawing logic in the new thread which will draw all the time? -
Onur A. about 11 yearsyes you dont need. because on the other thread simply you lock canvas, draw on it and then unlock and post it, it's more like two buffer rendering, you calculate and draw on one buffer while the other one is currently rendered on screen and then they swap.
-
TomeeNS almost 8 years@Onur A. But, if a game is some chess game or a card game you do not need to render the screen non stop and waste processing power. So is there a way to invalidate SurfaceView's area that needs to be redrawn, on demand?
-
TomeeNS almost 8 years@Onur A. But: I've found an easy way: 1. Dont use thread mechanism; 2. Make your own Draw() method and call it manually whenever you need it ;D. The Draw() must lock the surface first [Canvas canvas = holder.lockCanvas();], then draw on it [drawItAll(canvas);], and finally unlock it [holder.unlockCanvasAndPost(canvas);].
-
Onur A. over 7 yearsfor unreal-time games your solution might save the day but this is actually the pattern of game loops