Android development: Create a thread on surfaceview to draw a rotating image and refresh a specific area (dirty rectangle)

Source: Internet
Author: User

What is a dirty rectangle? For example, when you draw a rotating image on surfaceview, you only need to refresh a specific rectangular area. This rectangle is a dirty rectangle. On surfaceview, canvas = holder. lockcanvas (New rect (0, 0,130,130); // obtain the dirty rectangle refresh of the canvas, such as the rect parameter.

Please refer to the source code:

Package Yan. guoqi. rectphoto; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android. graphics. pixelformat; import android. graphics. rect; import android. util. attributeset; import android. util. log; import android. view. surfaceholder; import android. view. surfaceview; public class drawsurfaceview extends surfaceview implements surfaceholder. callback {Private Static final string tag2 = "drawsv"; private Boolean runflag = true; protected surfaceholder holder; private bitmap rotateimg; private thread mythread; Public drawsurfaceview (context, attributeset attrs) // constructor {super (context, attrs); // todo auto-generated constructor stubrotateimg = bitmapfactory. decoderesource (getresources (), R. drawable. rotate_circle); rotateimg = bitmap. createscaledbitmap (rotateimg, 100,100, true); holder = This. getholder (); holder. addcallback (this); holder. setformat (pixelformat. transparent); // top-layer surfaceview rendering to transparent this. setzorderontop (true); mythread = new thread (New mythread ();} public void surfacechanged (surfaceholder arg0, int arg1, int arg2, int arg3) {// todo auto-generated method stublog. V (tag2, "drawsv: surfacechanged... ");} public void surfacecreated (surfaceholder arg0) {// todo auto-generated method stublog. V (tag2, "drawsv: surfacecreated... "); // start the custom thread mythread. start ();} public void surfacedestroyed (surfaceholder arg0) {// todo auto-generated method stublog. V (tag2, "drawsv: surfacedestroyed... "); // terminate the custom thread runflag = false; mythread. interrupt ();}/* Custom thread */class mythread implements runnable {public void run () {// todo auto-generated method stubcanvas canvas = NULL; int rotate = 0; while (runflag) {try {canvas = holder. lockcanvas (New rect (0, 0,130,130); // obtain the canvas paint = new paint (); // canvas. drawbitmap (rotateimg, 0, 0, paint); // draws the rotating background // creates a matrix to control image rotation and shifts the matrix = new matrix (); // sets the Rotation Angle matrix. postrotate (rotate + = 48) % 360, rotateimg. getwidth ()/2, rotateimg. getheight ()/2); // set the left margin and top margin of matrix. posttranslate (0, 0); // draw a rotated image canvas. drawbitmap (rotateimg, matrix, paint); // sleep controls the maximum frame rate to draw 30 threads three times per second. sleep (30); holder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image} catch (exception e) {// todo: handle exceptionlog. V (tag2, "drawsurfaceview: painting failed... ");}}}}}

Notes:

The first is how to safely stop a thread. Set the flag here. Because the built-in stop () method is not secure, Android itself is not recommended. For details about how to safely abort a thread, refer to here: http://www.iteye.com/problems/67052 http://www.dewen.org/q/1957

Second, Holder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image. lockcanvas (New rect (0, 0,130,130); // obtain the canvas in. For example. The reason is that the canvas cannot be unlocked when the thread is suspended.

Third, use the Matrix to rotate the image's core code:

// Create a matrix to control image rotation and smooth matrix = new matrix (); // set the Rotation Angle matrix. postrotate (rotate + = 48) % 360, rotateimg. getwidth ()/2, rotateimg. getheight ()/2); // set the left margin and top margin of matrix. posttranslate (0, 0); // draw a rotated image canvas. drawbitmap (rotateimg, matrix, paint); // sleep controls the maximum frame rate to draw 30 threads three times per second. sleep (30 );


Fourth, the reason why surfaceview is set to the top-level transparency is that when the miscellaneous want to explore the bottom-layer surfaceview to preview the camera video, draw a rotating image animation on the top-level surfaceview. Unfortunately, the image is indeed rotated, and the underlying camera preview is normal. However, a rotating image blocks the preview image.Alas, it seems that
The image I rotate is not transparent ???


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.