Improve SurfaceView Custom View Performance?

I wrote a simple Android application that draws a maze using a custom view derived from SurfaceView . It follows the sample LunarLander sample program and performs all calculations and drawings using the background thread directly in the SurfaceHolder object.

Everything is fine and good, and it works well for small / medium-sized mazes, however, if I set the maze cell size to 8 pixels, there will be many cells in the maze executed by the application.

The code does what I donโ€™t like, it draws every cell, even if it hasnโ€™t been changed, and this is necessary to prevent the screen from flickering from the SurfaceView double buffer (in previous iterations of the application, I was only drawing what has changed, and this led to clumsy mess).

Then I want to use SurfaceView , but be more selective about what happens. Is it possible? If not, what are the alternatives? How about saving a bitmap off screen and pulling it selectively into it?

EDIT: I implemented an off-screen combination of Bitmap and Canvas , written by my state machine with a timer, drawing only those areas that were affected during the thread / solution. Then I just draw the entire screen bitmap on the SurfaceView as part of the run() method, and this solved my problem; I can drop the cell size up to 5 pixels and the performance is fine.

+7
source share
1 answer

About double buffering problems:

Take a look at this series of tutorials. Do you see any difference in the way the picture is processed? https://web.archive.org/web/20121109162356/http://www.droidnova.com/playing-with-graphics-in-android-part-iii,176.html

About optimizing your drawing:

The following link explains some approaches:

http://developer.android.com/guide/topics/graphics/opengl.html

http://developer.android.com/reference/android/opengl/GLSurfaceView.html

Continuous rendering vs render-when-dirty

Most 3D applications, such as games or simulations, are continuously animated. But some 3D applications are more reactive: they wait passively until the user does nothing, and then respond to it. For these types of applications, by default. Continuous behavior GLSurfaceView screen redrawing is time. If you are developing a reactive application, you can call GLSurfaceView.setRenderMode (RENDERMODE_WHEN_DIRTY), which disables continuous animation. Then you call GLSurfaceView.requestRender () whenever you want to re-render.

In addition, in 2D games, he easily calculates what the user sees, so refrain from drawing objects from the visible scene.

One last note:

you said

Everything is fine and good, and it works well for small / medium-sized mazes, however, if I set the maze cell size to 8 pixels, there will be many cells in the maze executed by the application.

Does this mean that you are calculating the solution of the maze? If so, you should not do it in the same thread where you draw it. You have to solve it in another thread. (Perhaps I realized that you are mistaken)

+5
source

All Articles