Surfaceview simple framework

Source: Internet
Author: User
  • Introduction

Image data can be obtained directly from hardware interfaces such as memory or DMA, which is a very important drawing container.

Its feature is that it can be drawn to the screen in a subthread. In this way, the main thread blocking can be avoided when the drawing task is heavy, thus improving the response speed of the program.

  • Implementation Process

Surfaceview is inherited to implement the surfaceview. Callback interface, and the method in the interface is used to manage the surface lifecycle.

Surfaceview. getholder () obtains the surfaceholder object, which is the controller of the surface.

Surfaceholder. addcallback (callback) adds a callback interface.

Surfaceholder. lockcanvas () Get the canvas object and lock the canvas

Canvas plotting, where a series of drawing operations are buffered to the surface.

Surfaceholder. unlockcanvasandpost (canvas) is locked and submitted to display the image.

  • Code
1 import android. content. context; 2 Import android. graphics. canvas; 3 Import android. graphics. paint; 4 Import android. util. attributeset; 5 import android. view. surfaceholder; 6 Import android. view. surfaceview; 7 8 public abstract class simplesurfaceview extends surfaceview implements 9 surfaceholder. callback, runnable {10 11 private canvas; 12 private surfaceholder holder; // controller 13 priv of the surface Ate Boolean isrun; 14 protected long sleeptime = 500; // The frame flushing frequency. The number of milliseconds of sleep is 15 private paint; 16 17/** 18 * constructor 19 */20 public simplesurfaceview (context) {21 super (context); 22 This. holder = getholder (); // get the surfaceholder object 23 holder. addcallback (this); // Add the surface life cycle callback 24 25 paint = new paint () to this surfaceholder; // create the paint object 26 paint. setantialias (true); // you can set the paint brush anti-tooth 27} 28 29/** 30 * constructor. It is required to build an interface in XML format. 31 */32 public simplesurfaceview (context, attributeset attrs) {33 super (context, attrs); 34 this. holder = getholder (); // get the surfaceholder object 35 holder. addcallback (this); // Add the surface life cycle callback 36 37 paint = new paint () to this surfaceholder; // create the paint object 38 paint. setantialias (true); // set the paint brush anti-tooth 39} 40 41/** 42 * from the callback interface, surface lifecycle method, call 43 */44 @ override45 public void surfacecreated (surfaceholder surfacehold Er) {46 isrun = true; 47 new thread (this ). start (); 48} 49 50/** 51 * From callback interface, surface lifecycle method, call 52 */53 @ override54 public void surfacechanged (surfaceholder, int I, int I2, 55 int I3) {56 57} 58 59/*** 60 * From callback interface, surface lifecycle method, 61 */62 @ override63 public void surfacedestroyed (surfaceholder) {64 isrun = false; 65} 66 67/** 68 * From runnable interface 69 */70 @ override7 1 Public void run () {72 while (isrun) {73 try {74 canvas = holder. lockcanvas (); // lock the canvas 75 simpledraw (canvas, paint); // subclass rewrite this method to draw 76} catch (exception e) {77 E. printstacktrace (); 78} finally {79 if (canvas! = NULL) 80 holder. unlockcanvasandpost (canvas); // unlock canvas 81} 82 83 // sleep for a moment, reducing the frame flushing frequency by 84 try {85 thread. sleep (sleeptime); 86} catch (interruptedexception e) {87 E. printstacktrace (); 88} 89} 90} 91 92 protected abstract void simpledraw (canvas, paint); // drawing, implemented by subclass 93}

 

Surfaceview simple framework

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.