Differences between view and surfaceview

Source: Internet
Author: User
Tags gety

The difference between the two is described here:

1view in the drawing, override the ondraw (canvas) method, and re-draw the canvas through the invaldate () and pastinvalidate () methods;

Invalidate () cannot be called cyclically in self-created threads;

Postinvalidate () can be called and executed cyclically in the self-created thread. If you do not create a thread loop re-painting cloth in the current view, the two re-painting cloth functions are no different and can be used.

Public class myview extends view {private float X; private float y; Public myview (context) {super (context );} // call @ overrideprotected void ondraw (canvas) {paint mpaint = new paint (); mpaint to draw an image. setcolor (color. blue); canvas. drawline (0, 0, x, y, mpaint); super. ondraw (canvas) ;}@ overridepublic Boolean ontouchevent (motionevent event) {x = event. getx (); y = event. gety (); // re-paint the cloth invalidate (); // postinvalidate (); Return true ;}}

2. surfaceview inherits from view

When using surfaceview, you will find that the ondraw (canvas) method of the parent class is not executed. The reason is that in surfaceview, we do not directly deal with it, but control it through surfaceholder. Use the lockcanvas () function of surfaceholder to obtain the canvas object of surfaceview, and modify the data in surfaceview by drawing the content on the canvas.

Public class mysurfaceview extends surfaceview implements callback, runnable {private surfaceholder SFH; private paint; private canvas; private thread th; private Boolean flag; private int textx; private int texty; public mysurfaceview (context) {super (context); SFH = This. getholder (); SFH. addcallback (this); paint = new paint (); paint. setcolor (color. white) ;}@ overridepublic void surfacecrea Ted (surfaceholder holder) {flag = true; Th = new thread (this); th. start () ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ overridepublic void surfacedestroyed (surfaceholder holder) {flag = false ;} @ overridepublic Boolean ontouchevent (motionevent event) {textx = (INT) event. getx (); texty = (INT) event. gety (); Return true;} public void mydraw () {// get a locked canvas, In order to prevent surfaceview from being modified during painting and destroy other conditions such as paint blackpaint = new paint (); blackpaint. setcolor (color. black); try {canvas = SFH. lockcanvas (); If (canvas! = NULL) {canvas. drawrect (0, 0, getwidth (), getheight (), blackpaint); // Screen 1 // canvas. drawcolor (color. black); // screen flushing 2 // canvas. drawrgb (0, 0, 0); // fl screen 3canvas. drawtext ("game", textx, texty, paint) ;}} catch (exception e) {e. printstacktrace ();} finally {If (canvas! = NULL) {// unlock the canvas and submit SFH. unlockcanvasandpost (canvas) ;}}/ *** game logic */private void logic () {}@ overridepublic void run () {While (FLAG) {long start = system. currenttimemillis (); mydraw (); logic (); long end = system. currenttimemillis (); try {If (end-start <50) {thread. sleep (50-(end-Start);} catch (interruptedexception e) {e. printstacktrace ();}}}}

The differences between the two canvas updates:

Re-drawing the canvas in view is performed by calling the postinvalidate () and invalidate () functions provided by view, that is, the canvas is updated by the system main UI. When the main UI thread of the system is blocked by the rendering function, the system will fail to respond to messages such as buttons and touch screens;

In surfaceview, the re-painting of the canvas is performed by a new separate thread, so there will be no problems such as failure to respond to keys and touch screen information due to blocking of the main UI thread.


Surfaceview has a dual buffer mechanism, while view does not




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.