Android Custom Component Family "12"--Non-UI threading drawing Surfaceview

Source: Internet
Author: User
Tags event listener

First, the introduction of Surfaceview

We've already customized the view before, using canvas drawing, but there are some flaws in the drawing mechanism of view.

1, view lacks double buffering mechanism.

2. The program must redraw the picture displayed on the entire view and compare the resource consumption.

3. The non-UI thread cannot update the view component, so it consumes the main thread resource and is slow when it needs to process logic in the main thread.

In Android, we are provided with a surfaceview to replace the view implementation of drawing graphics, generally in the game drawing aspect of the wider application, so if it is more complicated to use the drawing recommended Surfaceview.

Second, the drawing mechanism of Surfaceview Surfaceview is generally used in conjunction with Surfaceholder, Surfaceholder is used for drawing on surfaceview associated with it, calling Surfaceview Getholder ( ) method to get the Surfaceview associated Surfaceholder.surfaceholder provides Lockcanvas and Lockcanvas (Rect dirty) to lock the drawing area. and get to the canvas of the area (canvas) We can draw the drawing by this canvas. Iii. Surfaceview Using instance 1, a simple use (drawn on the UI thread)
Package Com.test.surfaceview;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rect;import Android.os.bundle;import android.view.MotionEvent; Import Android.view.surfaceholder;import Android.view.surfaceview;import Android.view.view;import Android.view.view.ontouchlistener;import Com.example.testsurfaceview.r;public class MainActivity extends Activity{ Private Surfaceholder holder;private Paint paint;private rect rect, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);p aint = new paint () ; rect = new Rect (); Surfaceview surface = (Surfaceview) Findviewbyid (r.id.show); holder = Surface.getholder ();// Three functions destroyed by the system Holder.addcallback (new Surfaceholder.callback () {@Overridepublic void surfacedestroyed (Surfaceholder Holder) {} @Overridepublic void SurfacecreateD (surfaceholder holder) {Canvas canvas = Holder.lockcanvas (); Bitmap Bitmap = Bitmapfactory.decoderesource (MainActivity.this.getResources (), r.drawable.ic_launcher); Canvas.drawbitmap (bitmap, 0, 0, null); Holder.unlockcanvasandpost (canvas);} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {}});// Bind Event Listener Surface.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) {if (event.getaction () = = Motionevent.action_down) {int CX = (int) event.getx (), int cy = (int) event.gety (); Rect.set (cx-50, CY-50, CX +, CY + 50);//Lock a fixed area and lock the canvas CA Nvas = Holder.lockcanvas (rect);//Save the current state of the canvas Canvas.save ();//Rotate Canvas canvas.rotate (CX, CY);p Aint.setcolor (color.red); /Draw Block Canvas.drawrect (cx-40, cy-40, CX, CY, paint);//Restore Canvas Canvas.restore ();p aint.setcolor (Color.green);// Draw Block Canvas.drawrect (CX, CY, CX + MAX, CY + +, paint);//Draw complete, release canvas, commit modify holder.unlockcanvasandpost (canvas);} return false;}});}}
2. Drawing in a non-UI thread
Package Com.test.surfaceview;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rect;import Android.os.asynctask;import Android.util.AttributeSet; Import Android.view.surfaceholder;import android.view.surfaceview;import com.example.testsurfaceview.r;/** * Sun Xiao Qiang Http://blog.csdn.net/dawanganban * * @author Administrator * */public class Mysurfaceview extends Surfaceview {private C Ontext context;private Rect rect;private Paint paint;private appstartdrawview appstartdrawview;public MySurfaceView ( Context context, AttributeSet Attrs) {Super (context, attrs); this.context = Context;rect = new rect ();p aint = new paint (); a Ppstartdrawview = new Appstartdrawview (); This.getholder (). Addcallback (Appstartdrawview);} /** * Stop animation */public void Stopanim () {Appstartdrawview.cancel (true);} Private class Appstartdrawview extends Asynctask<void, INteger, Void> implements Surfaceholder.callback {private Boolean isstarted = False;private surfaceholder holder;@ overridepublic void surfacecreated (Surfaceholder holder) {This.holder = holder;//draw a picture canvas canvas = Holder.lockcanvas (); Bitmap Bitmap = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.ic_launcher); Canvas.drawbitmap ( Bitmap, 0, 0, NULL), Holder.unlockcanvasandpost (canvas), if (!isstarted) {this.execute (); isstarted = True;}} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width, int height) {this.holder = holder;} @Overridepublic void surfacedestroyed (Surfaceholder holder) {isstarted = False;holder = null;} @Overrideprotected void Doinbackground (void ... params) {int cx = 0;int cy = 0;while (!iscancelled ()) {//Draw animation//todo ... rec T.set (cx-50, CY-50, CX +, CY + 50);//Lock a fixed area and lock the canvas canvas = Holder.lockcanvas (rect);//Save the current state of the canvas Canvas.save ();//Spin Turn the canvas canvas.rotate (CX, CY);p Aint.setcolor (color.red);//Draw Square canvas.drawrect (cx-40, Cy-, CX, CY, paint),//Restore Canvas Canvas.restore ();p aint.setcolor (Color.green);//Draw Block Canvas.drawrect (CX, CY, CX + max, CY + Max, Pai NT);//Draw complete, release canvas, submit modify Holder.unlockcanvasandpost (canvas),//todo .... try {thread.sleep (n);} catch ( Interruptedexception e) {e.printstacktrace ();} CX + = 60;cy + = 60;if (cx >=) {Stopanim ();}} return null;}}}

Android Custom Component Family "12"--Non-UI thread drawing Surfaceview

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.