The difference between Surfaceview and view
View is in the main thread of the UI that responds directly to the user's actions, as well as the distribution of tasks, but the task is more complex and can become blocked.
Surfaceview does not have this problem, assuming that it obtains images directly from memory, and more importantly, Surfaceview can change the UI through threads outside the main thread.
Use
Updates to the UI are divided into active and passive updates, and for passive updates, they are updated with time-based controls, often with low frequencies, so they tend to choose view to complete.
For the active update, update frequency faster, such as Timer update screen, a version will take Surfaceview.
Instance code:
Public class MyView extends surfaceview implements Surfaceholder . Callback { Public MyView(Context context) {Super(context); Getholder (). Addcallback ( This); } Public void Draw(){//Lock CanvasCanvas canvas = Getholder (). Lockcanvas ();//After closing, remember to unlock the canvasGetholder (). Unlockcanvasandpost (canvas); }@Override Public void surfacecreated(Surfaceholder Holder) {//TODO auto-generated method stub}@Override Public void surfacechanged(Surfaceholder holder,intFormatintWidthintHeight) {//TODO auto-generated method stub}@Override Public void surfacedestroyed(Surfaceholder Holder) {//TODO auto-generated method stub}}
Draw simple graphics with Suifaceview
A red Square is drawn:
Public class MyView extends surfaceview implements Surfaceholder . Callback { PrivatePaint paint =NULL; Public MyView(Context context) {Super(context); Paint =NewPaint (); Paint.setcolor (color.red); Getholder (). Addcallback ( This); } Public void Draw() {Canvas canvas = Getholder (). Lockcanvas (); Canvas.drawcolor (Color.White); Canvas.drawrect (0,0, -, -, paint); Getholder (). Unlockcanvasandpost (canvas); }@Override Public void surfacecreated(Surfaceholder Holder) {//TODO auto-generated method stubDraw (); }@Override Public void surfacechanged(Surfaceholder holder,intFormatintWidthintHeight) {//TODO auto-generated method stub}@Override Public void surfacedestroyed(Surfaceholder Holder) {//TODO auto-generated method stub}}
Note Be sure to start drawing after the Surfacecreated method, and be sure to finish drawing before the Surfacedestroyed method.
Android game Development Surfaceview use-android learning Journey (Fri)