Efficient plotting on surfaceview in Android

Source: Internet
Author: User

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 is

The surfaceview class is required at this time.
Because it is relatively advanced, it is recommended to install B. It is not feasible to simply inherit from the surfaceview class. A surfaceholder. Callback interface must be implemented to specify the callback method when surfaceview is created, changed, or deleted, and

In surfaceview, A surfaceholder object is used to control surfaceview.
If you use this surfaceview as the full screen view of an activity, call setcontentview (New myview, modify the corresponding layout XML file.

Add similar layout code:
<Com. example. FQ. 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:
Myview. Java:
Public class myview extends surfaceview implements surfaceholder. Callback {
Private surfaceholder holder = NULL; // control object
Private vector <float> xs = new vector <float> ();
Private vector <float> ys = new vector <float> ();

Public myview (context, attributeset ATTR ){
Super (context, ATTR );
// Todo auto-generated constructor stub
Holder = getholder ();
Holder. addcallback (this );
}

@ Override
Public void surfacechanged (surfaceholder holder, int format, int width,
Int height ){
// Todo auto-generated method stub

}

@ Override
Public void surfacecreated (surfaceholder holder ){
// Todo auto-generated method stub
New thread (New myloop (). Start ();
}

@ Override
Public void surfacedestroyed (surfaceholder holder ){
// Todo auto-generated method stub

}

Public void dodraw (canvas ){
// Todo auto-generated method stub
Super. ondraw (canvas );
Canvas. drawcolor (color. White); // The background is drawn here.
Paint P = new paint (); // stroke
P. setantialias (true); // anti-sawtooth
P. setcolor (color. Black );
P. setstyle (style. Stroke );
For (INT I = 0; I <Xs. Size (); I ++)
Canvas. drawcircle (XS. elementat (I), Ys. elementat (I), 10, P );
}

@ Override
Public Boolean ontouchevent (motionevent event ){
// Todo auto-generated method stub
If (event. getaction () = motionevent. action_down ){
Xs. Add (event. getx ());
Ys. Add (event. Gety ());
}
Return true;
}

Class myloop implements runnable {
// Familiar with game programming, Master Loop
@ Override
Public void run (){
// Todo auto-generated method stub
While (true ){
Try {
Canvas c = holder. lockcanvas ();
Dodraw (C );
Holder. unlockcanvasandpost (C );
Thread. Sleep (20 );
} Catch (exception e ){

}
}
}

}

}

Main. Java
Public class main extends activity {
Private myview SV = NULL;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
SV = (calendarview) findviewbyid (R. Id. sv );
}
}

Main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Com. example. FQ. myview
Android: Id = "@ + ID/SV"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
</Linearlayout>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.