Examples of surfaceview in Android

Source: Internet
Author: User

Surfaceview plays an important role in game development. It has a greater degree of freedom for Screen Control (unlike view update with handler, about view ), however, there are not many references in this regard. The examples that can be found are a bit overwhelming and cannot be clearly displayed. The following is a simple example, strive to clearly display the process, while others can be simplified.

 

Program effect: Draw a blue rectangle with a thread.

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://rayleung.javaeye.com/blog/420410 ">
  1. Package com. Ray. test;
  2. /*
  3. * Surfaceview sample program
  4. * Demonstrate the process
  5. */
  6. Import Android. App. activity;
  7. Import Android. content. context;
  8. Import Android. Graphics. Canvas;
  9. Import Android. Graphics. color;
  10. Import Android. Graphics. paint;
  11. Import Android. Graphics. rectf;
  12. Import Android. OS. Bundle;
  13. Import Android. View. surfaceholder;
  14. Import Android. View. surfaceview;
  15. Public class test extends activity {
  16. Public void oncreate (bundle savedinstancestate ){
  17. Super. oncreate (savedinstancestate );
  18. Setcontentview (New myview (this ));
  19. }
  20. // Internal class
  21. Class myview extends surfaceview implements surfaceholder. Callback {
  22. Surfaceholder holder;
  23. Public myview (context ){
  24. Super (context );
  25. Holder = This. getholder (); // Get holder
  26. Holder. addcallback (this );
  27. // Setfocusable (true );
  28. }
  29. @ Override
  30. Public void surfacechanged (surfaceholder holder, int format, int width,
  31. Int height ){
  32. }
  33. @ Override
  34. Public void surfacecreated (surfaceholder holder ){
  35. New thread (New mythread (). Start ();
  36. }
  37. @ Override
  38. Public void surfacedestroyed (surfaceholder holder ){
  39. }
  40. // Internal class of the internal class
  41. Class mythread implements runnable {
  42. @ Override
  43. Public void run (){
  44. Canvas canvas = holder. lockcanvas (null); // get the canvas
  45. Paint mpaint = new paint ();
  46. Mpaint. setcolor (color. Blue );
  47. Canvas. drawrect (New rectf (40, 60, 80), mpaint );
  48. Holder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image
  49. }
  50. }
  51. }
  52. }
Package COM. ray. test;/** the surfaceview sample program * demonstrates the process */import android. app. activity; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectf; import android. OS. bundle; import android. view. surfaceholder; import android. view. surfaceview; public class test extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (New myview (this);} // internal class myview extends surfaceview implements surfaceholder. callback {surfaceholder holder; Public myview (context) {super (context); holder = This. getholder (); // get holderholder. addcallback (this); // setfocusable (true) ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ overridepublic void surfacecreated (surfaceholder holder) {New thread (New mythread ()). start () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {}// internal class mythread implements runnable {@ overridepublic void run () {canvas = holder. lockcanvas (null); // obtain the canvas paint mpaint = new paint (); mpaint. setcolor (color. blue); canvas. drawrect (New rectf (40, 60, 80), mpaint); holder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image }}}}

 

  • Testsurfaceview0.1.rar (25.6 KB)
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.