Android surfaceview usage

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 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:

 


  1. Public ClassMainactivityExtendsActivity {
  2. PrivateMyview mSv =Null;
  3. PrivateButton mbtnok;
  4. PrivateButton mbtncancle;
  5. @ Override
  6. Public VoidOncreate (bundle savedinstancestate ){
  7. Super. Oncreate (savedinstancestate );
  8. Setcontentview (R. layout. Main );
  9. MSv = (myview) findviewbyid (R. Id. sv );
  10. MSv. settag (True);
  11. Mbtnok = (button) findviewbyid (R. Id. btnok );
  12. Mbtnok. setonclicklistener (NewOnclicklistener (){
  13. Public VoidOnclick (view arg0 ){
  14. // Todo auto-generated method stub
  15. MSv. settag (True);
  16. }
  17. });
  18. Mbtncancle = (button) findviewbyid (R. Id. btncancel );
  19. Mbtncancle. setonclicklistener (NewOnclicklistener (){
  20. Public VoidOnclick (view arg0 ){
  21. // Todo auto-generated method stub
  22. MSv. settag (False);
  23. }
  24. });
  25. }
  26. }


Mytest interface:

 

[Java]   View plaincopy

  1. Public ClassMyviewExtendsSurfaceviewImplementsSurfaceholder. Callback {
  2. PrivateSurfaceholder holder =Null;// Control object
  3. PrivateVector <float> xs =NewVector <float> ();
  4. PrivateVector <float> ys =NewVector <float> ();
  5. PublicMyview (context, attributeset attrs ){
  6. Super(Context, attrs );
  7. Holder = getholder ();
  8. Holder. addcallback (This);
  9. }
  10. Public VoidSurfacechanged (surfaceholder arg0,IntArg1,IntArg2,IntArg3 ){
  11. // Todo auto-generated method stub
  12. }
  13. Public VoidSurfacecreated (surfaceholder arg0 ){
  14. // Todo auto-generated method stub
  15. NewThread (NewMyloop (). Start ();// In view, the system does not allow the thread outside the main thread to control the UI, but surfaceview does.
  16. }
  17. Public VoidSurfacedestroyed (surfaceholder arg0 ){
  18. // Todo auto-generated method stub
  19. }
  20. Public VoidDodraw (canvas ){
  21. // Todo auto-generated method stub
  22. Super. Ondraw (canvas );
  23. Canvas. drawcolor (color. White );// The background is drawn.
  24. Paint P =NewPaint ();// Stroke
  25. P. setantialias (True);// Anti-sawtooth
  26. P. setcolor (color. Black );
  27. P. setstyle (style. Stroke );
  28. IntA = Xs. Size ();
  29. For(IntI =0; I <Xs. Size (); I ++)
  30. Canvas. drawcircle (XS. elementat (I), Ys. elementat (I ),10, P );
  31. }
  32. @ Override
  33. Public BooleanOntouchevent (motionevent event ){
  34. // Todo auto-generated method stub
  35. If(Event. getaction () = motionevent. action_down ){
  36. String A =This. Gettag (). tostring ();
  37. If(A. Equals ("True")){
  38. Xs. Add (event. getx ());
  39. Ys. Add (event. Gety ());
  40. }
  41. }
  42. Return True;
  43. }
  44. ClassMyloopImplementsRunnable {
  45. // Familiar with game programming, Master Loop
  46. Public VoidRun (){
  47. // Todo auto-generated method stub
  48. While(True){
  49. Try{
  50. Canvas c = holder. lockcanvas ();
  51. Dodraw (C );
  52. Holder. unlockcanvasandpost (C );
  53. Thread. Sleep (20);
  54. }Catch(Exception e ){
  55. }
  56. }
  57. }
  58. }
  59. }


Main. xml file:

 

[Java]   View plaincopy

    1. <? XML version ="1.0"Encoding ="UTF-8"?>
    2. <Relativelayout xmlns: Android =Http://schemas.android.com/apk/res/android"
    3. Android: layout_width ="Fill_parent"
    4. Android: layout_height ="Fill_parent"
    5. >
    6. <Com. Android. sv. myview
    7. Android: Id ="@ + ID/SV"
    8. Android: layout_width ="Fill_parent"
    9. Android: layout_height ="Fill_parent"
    10. />
    11. <Button Android: Id ="@ + ID/btnok"
    12. Android: layout_width ="Wrap_content"
    13. Android: layout_height ="Wrap_content"
    14. Android: text ="Enable plotting"/>
    15. <Button Android: Id ="@ + ID/btncancel"
    16. Android: layout_width ="Wrap_content"
    17. Android: layout_height ="Wrap_content"
    18. Android: layout_torightof ="@ ID/btnok"
    19. Android: text ="Cancel drawing"/>
    20. </Relativelayout>
Related Article

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.