My surfraceview is where the majority of my application is and I would like to reduce its lag, thus I have been told to take it off the UI Thread. Is it possible to run a SurfaceView in an AsyncTask?
Asked
Active
Viewed 1,209 times
1
-
1Uh, you can't run the UI off the UI thread. You'll get a CalledFromWrongThread Exception. You could however to all the processing stuff which tells you *where* to draw in a background thread, and then only do the *actual drawing* on the UI thread. – Raghav Sood Mar 19 '13 at 01:04
-
2SurfaceView is meant to be drawn into from a background thread: you can use the lockCanvas() and unlockCanvasAndPost() methods on SurfaceHolder to achieve this. – Romain Guy Mar 19 '13 at 01:38
1 Answers
1
While it is possible to draw into a SurfaceView from an AsyncTask it is not recommended. AsyncTasks can run in a serial queue so you would likely block other tasks. Instead just use a Thread. The SDK contains a sample app called LunarLander which shows how to do this.

Romain Guy
- 97,993
- 18
- 219
- 200