Most Android controls inherit from the view type. Therefore, they generally inherit the View class when using custom controls. However, for highly efficient, game-level graphics, or players, the common view class isThe surfaceview class is required at this time. Because it is relatively advanced, it is recommended to install B. Simply inheriting from the surfaceview class is not acceptable, and a surfaceholder must be implemented. callback interface is used to specify the callback method when surfaceview is created, changed, or deleted, and surfaceview is controlled through a surfaceholder object in surfaceview.
If you use this surfaceview as the full screen view of an activity, call setcontentview (New myview, you should modify the corresponding layout XML file and add a similar layout.Code:
<Com. Android. sv. myview
Android: Id = "@ + ID/SV"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
Com. example. FQ. myview is the full name of the corresponding custom class.
Because the default XML file parsing method is to call the view (context, attributeset) constructor of the view to construct the view, your custom surfaceview should also have a parameter (context, attributeset) and execute the super (context, attributeset) function of the parent class in the constructor ). When drawing, you must first use canvas c = holder. lockcanvas (); lock and obtain the canvas, then draw, then call holder. unlockcanvasandpost (c); render the painting content
The following is an example of a runtime diagram:
Main Interface:
-
- Public ClassMainactivityExtendsActivity {
-
- PrivateMyview mSv =Null;
-
- PrivateButton mbtnok;
-
- PrivateButton mbtncancle;
-
- @ Override
- Public VoidOncreate (bundle savedinstancestate ){
-
- Super. Oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- MSv = (myview) findviewbyid (R. Id. sv );
-
- MSv. settag (True);
- Mbtnok = (button) findviewbyid (R. Id. btnok );
-
- Mbtnok. setonclicklistener (NewOnclicklistener (){
-
-
- Public VoidOnclick (view arg0 ){
- // Todo auto-generated method stub
-
- MSv. settag (True);
-
- }
-
- });
-
- Mbtncancle = (button) findviewbyid (R. Id. btncancel );
-
- Mbtncancle. setonclicklistener (NewOnclicklistener (){
-
-
- Public VoidOnclick (view arg0 ){
- // Todo auto-generated method stub
-
- MSv. settag (False);
-
- }
-
- });
-
- }
- }
Mytest interface:
[Java] View plaincopy
- Public ClassMyviewExtendsSurfaceviewImplementsSurfaceholder. Callback {
-
- PrivateSurfaceholder holder =Null;// Control object
-
- PrivateVector <float> xs =NewVector <float> ();
- PrivateVector <float> ys =NewVector <float> ();
-
- PublicMyview (context, attributeset attrs ){
-
- Super(Context, attrs );
-
- Holder = getholder ();
- Holder. addcallback (This);
-
- }
-
-
- Public VoidSurfacechanged (surfaceholder arg0,IntArg1,IntArg2,IntArg3 ){
- // Todo auto-generated method stub
-
-
- }
-
-
- Public VoidSurfacecreated (surfaceholder arg0 ){
- // Todo auto-generated method stub
-
- NewThread (NewMyloop (). Start ();// In view, the system does not allow the thread outside the main thread to control the UI, but surfaceview does.
-
- }
-
- Public VoidSurfacedestroyed (surfaceholder arg0 ){
-
- // Todo auto-generated method stub
-
-
- }
-
- Public VoidDodraw (canvas ){
- // Todo auto-generated method stub
-
- Super. Ondraw (canvas );
-
- Canvas. drawcolor (color. White );// The background is drawn.
-
- Paint P =NewPaint ();// Stroke
- P. setantialias (True);// Anti-sawtooth
-
- P. setcolor (color. Black );
-
- P. setstyle (style. Stroke );
-
- IntA = Xs. Size ();
- For(IntI =0; I <Xs. Size (); I ++)
-
- Canvas. drawcircle (XS. elementat (I), Ys. elementat (I ),10, P );
-
- }
-
-
- @ Override
- Public BooleanOntouchevent (motionevent event ){
-
- // Todo auto-generated method stub
-
- If(Event. getaction () = motionevent. action_down ){
-
- String A =This. Gettag (). tostring ();
- If(A. Equals ("True")){
-
- Xs. Add (event. getx ());
-
- Ys. Add (event. Gety ());
-
- }
-
- }
- Return True;
-
- }
-
-
- ClassMyloopImplementsRunnable {
-
- // Familiar with game programming, Master Loop
- Public VoidRun (){
-
- // Todo auto-generated method stub
-
- While(True){
-
- Try{
- Canvas c = holder. lockcanvas ();
-
- Dodraw (C );
-
- Holder. unlockcanvasandpost (C );
-
- Thread. Sleep (20);
- }Catch(Exception e ){
-
-
- }
-
- }
-
- }
-
- }
-
- }
Main. xml file:
[Java] View plaincopy
-
- <? XML version ="1.0"Encoding ="UTF-8"?>
- <Relativelayout xmlns: Android =Http://schemas.android.com/apk/res/android"
-
- Android: layout_width ="Fill_parent"
-
- Android: layout_height ="Fill_parent"
-
- >
-
- <Com. Android. sv. myview
-
- Android: Id ="@ + ID/SV"
- Android: layout_width ="Fill_parent"
-
- Android: layout_height ="Fill_parent"
-
- />
-
- <Button Android: Id ="@ + ID/btnok"
-
- Android: layout_width ="Wrap_content"
- Android: layout_height ="Wrap_content"
-
- Android: text ="Enable plotting"/>
-
- <Button Android: Id ="@ + ID/btncancel"
-
- Android: layout_width ="Wrap_content"
- Android: layout_height ="Wrap_content"
-
- Android: layout_torightof ="@ ID/btnok"
-
- Android: text ="Cancel drawing"/>
-
- </Relativelayout>