1. Surfaceview:
complete the quick switching of the interface within the unit time (the game interface is fluent).
We know the general view before, only in the main thread inside the display, the main thread to update the UI. However , Surfaceview can update the UI inside a child thread.
The Surfaceview has built-in dual-buffering technology that will correspond to two threads (thread A and thread B) Once the Surfaceview is started;
(1) Thread A is the foreground , thread b is a background thread ;
(2) Thread A begins the UI display, and ( at the same time ) thread B calculates the display of the next UI, a executes, and B is finished;
(3) At this time, thread A and thread B quickly switch roles , when thread B is the foreground process , thread A is a background process ;
(4) At this time, the thread B front-end display UI, thread a background calculation processing ; b is finished, a is executed;
(5) at this time, thread B and thread a quickly switch roles, when thread A is the foreground process , thread B is a background process ;
(6) so repeated;
2. The case demo uses Surfaceview, as follows:
(1)Activity_main.xml, as follows:
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context=". Mainactivity " >6 7 8 <Surfaceview9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:layout_centerhorizontal= "true" A android:layout_centervertical= "true" - Android:id= "@+id/sv"/> - the </Relativelayout>
(2)mainactivity, as follows:
1 PackageCom.itheima.surfaceview;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.graphics.Canvas;6 ImportAndroid.graphics.Color;7 ImportAndroid.graphics.Paint;8 ImportAndroid.view.Menu;9 ImportAndroid.view.SurfaceHolder;Ten ImportAndroid.view.SurfaceHolder.Callback; One ImportAndroid.view.SurfaceView; A ImportAndroid.view.View; - - Public classMainactivityextendsActivity { the PrivateSurfaceview SV; - Privatepaint paint; - - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); ASV = (Surfaceview) Findviewbyid (R.ID.SV);//Stage at -Paint =NewPaint (); - Paint.setcolor (color.red); - FinalSurfaceholder holder = Sv.getholder ();//Director, director controls how to show. - -Holder.addcallback (NewCallback () { in - @Override to Public voidsurfacedestroyed (Surfaceholder holder) { +SYSTEM.OUT.PRINTLN ("The interface has been destroyed"); - } the * @Override $ Public voidSurfacecreated (FinalSurfaceholder Holder) {Panax NotoginsengSYSTEM.OUT.PRINTLN ("interface is created")); - NewThread () { the Public voidrun () { + for(inti = 0; I < 500; i++) { ACanvas Canvas =Holder.lockcanvas (); theCanvas.drawcircle (200, 200, I, paint); + holder.unlockcanvasandpost (canvas); - Try { $Thread.Sleep (2); $}Catch(interruptedexception e) { - e.printstacktrace (); - } the } - };Wuyi }.start (); the } - Wu @Override - Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth, About intheight) { $ - } - }); - } A + the}
Surfaceview This control, the memory overhead is relatively large, if put Surfaceview into the oncreate inside will be easy to produce a null pointer, because OnCreate call Surfaceview, maybe Surfaceview is not created well, calling it is, of course, a null pointer exception .
Here is the use of a Surfaceholder API, is Addcallback (... ), where the interface implementation class has a method:
public void surfacecreated (surfaceholder holder), which is the method that is called after the Surfaceview is created .
Android (Java) Learning Note 244: Multimedia Surfaceview