Surfaceview troubles (1)-dual cache and screen clearing

Source: Internet
Author: User
Tags drawtext

When studying Android development, we often recommend using surfaceview instead of view when you see a picture. There are two reasons:

  • Surfaceview implements a dual cache mechanism to avoid flickering interfaces;
  • Surfaceview allows you to draw images in non-UI threads, so that you can start a thread to draw images. This reduces the problem that the main UI thread is "stuck", that is, improving the efficiency.

However, it is not easy to use it during the learning process. After a long time, I am still confused. Here I will record it and share it with you. In applications, there are often some "weird" problems that add a lot of "troubles ". The first headache is dual cache and clear screen.

  1. How to double cache? According to the introduction, dual cache generally means that there is a cache area in the memory area that corresponds to the interface image. When the interface changes, first, draw the interface content in the cache area, and then display it at one time, so that there is no flickering effect and the display efficiency is higher. But does surfaceview's dual cache differ from this statement?
  2. After the image is painted in the memory and then displayed, will it be basically painted in the original image or re-painted?
  3. Why is it stacked in the same place every time (such as writing?

Let's take a look at a test example and look at the code first:

Res/layout/Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <COM. tutor. surface. mysurfaceview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </linearlayout>

 

COM/tutor/surface/drawrectsurfaceview. Java

Package COM. tutor. surface; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> public class drawrectsurfaceview extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}< br/>}

 

COM/tutor/surface/mysurfaceview. Java

Package COM. tutor. surface; <br/> Import Java. util. timer; <br/> Import android. content. context; <br/> Import android. util. attributeset; <br/> Import android. util. log; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> public class mysurfaceview extends surfaceview implements surfaceholder. callback {<br/> private timer = NULL; <br/> private mytimertask task = NULL; <br /> Public mysurfaceview (context, attributeset attrs) {<br/> super (context, attrs); <br/> surfaceholder holder = getholder (); <br/> holder. addcallback (this); <br/> // todo auto-generated constructor stub <br/>}< br/> @ override <br/> Public void surfacechanged (surfaceholder holder, int format, int width, <br/> int height) {<br/> // todo auto-generated method stub </P> <p >}< br/> @ override <br/> pub LIC void surfacecreated (surfaceholder holder) {<br/> // todo auto-generated method stub <br/> timer = new timer (); <br/> task = new mytimertask (holder); <br/> timer. schedule (task, 500,100 0); <br/>}< br/> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> // todo auto-generated method stub <br/> If (timer! = NULL) {<br/> timer. cancel (); <br/> timer = NULL; <br/>}< br/> task = NULL; <br/>}</P> <p >}< br/>

 

COM/tutor/surface/mytimertask. Java

Package COM. tutor. surface; <br/> Import Java. util. timertask; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. view. surfaceholder; <br/> public class mytimertask extends timertask {<br/> private surfaceholder holder = NULL; <br/> private paint = NULL; <br/> private int COUNT = 0; </P> <p> Public mytimertask (surfaceholder _ Holder) {<br/> holder = _ holder; <br/> paint = new paint (); <br/> paint. setcolor (color. white); <br/>}< br/> Public void run () {<br/> // todo auto-generated method stub <br/> If (count> 40) // test the program. If the value is greater than 20, <br/> return; </P> <p> canvas = NULL; <br/> try {<br/> canvas = holder. lockcanvas (null); // lock the entire canvas <br/> If (count % 2 = 0) {<br/> canvas. drawtext (count + "", 50, count * 8, paint); <br/>}else {<br/> canva S. drawtext (count + "", 150, count * 8, paint); <br/>}< br/>} catch (exception e) {<br/> // todo: handle exception <br/>}finally {<br/> If (canvas! = NULL) {<br/> holder. unlockcanvasandpost (canvas); <br/>}< br/> count ++; <br/>}</P> <p> Public void cleardraw () {<br/> canvas = NULL; <br/> try {<br/> canvas = holder. lockcanvas (null); <br/> canvas. drawcolor (color. black); <br/>} catch (exception e) {<br/> // todo: handle exception <br/>} finally {<br/> If (canvas! = NULL) {<br/> holder. unlockcanvasandpost (canvas); <br/>}< br/>

 

Running result:

 

We can see from the above:

  • The numbers in the two columns alternate (first, the column of the first image is an even number, then the column of the second image is an odd number, and then the column is returned to the even number, so it is alternating );
  • When you draw the following number, the number you have drawn before will be retained;
  • The screen was not explicitly cleared, but it was always black.
  • If you change the coordinates of writing to a fixed one (that is, all words are written in only one place), the words will be stacked and you cannot see clearly what you have written.

Come back and think about the three questions at the beginning:

  • Surfaceview's dual cache is somewhat different. surfaceview has two caches, one is the front buffer and the other is the back buffer. The two buffers are displayed alternately on the interface (FLIP, that is, the content of the front buffer is currently seen. If the interface changes at this time, the back buffer will draw the content based on the original, and then the front buffer and the back buffer will exchange the location; it should be noted that because there are two buffers, If you redraw all the content each time, there will be no problem, but if each painting is part of the content, then there is a problem: Some and part of the display are displayed alternately, which is of course not what we want. The solution is to clear the screen each time and then draw everything again.
  • When the screen is unclear, it is drawn on the basis of the original;
  • Because the content is drawn on the basis of the original, if you draw in a place each time, it will be stacked up. The solution is to clear the content before painting each time. The simple method for clearing the content is canvas. drawcolor (color. black), the scope of content clearing is determined by holder. lockcanvas (New rect (left, top, right, bottom) decides to use holder. lockcanvas (null) indicates that the range is full screen. Note: If the screen is cleared, unlocking the canvas post and then painting the original content will cause a flash. Therefore, after the screen is cleared, the canvas post (Display) will be solved ).

 

Supplement:

The solution to the first problem is "Clear the screen every time and draw everything again". In some cases, there can be another solution, however, this will cause another "weird" problem. I will explain it again next time.

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.