Android and androidsdk

Source: Internet
Author: User

Android and androidsdk
SurfaceView

SurfaceView is a special subclass of View. It aims to provide another thread for painting.

  • Procedure

1. To draw with SurfaceView, first create a class that inherits SurfaceView. At the same time, this class should implement the SurfaceHolder. Callback interface.

Three callback functions in this interface (surfaceChanged (SurfaceHolder holder, int format, int width, int height), surfaceCreated (SurfaceHolder holder), surfaceDestroyed (SurfaceHolder holder )) the time when the Surface is changed, created, and destroyed.

2. operations on the Surface object are performed through SurfaceHolder. Therefore, when initializing your SurfaceView class, you need to call getHolder () to obtain the SurfaceHolder object, and then use addCallback () add the callback interface (because your class implements the corresponding interface, you can pass this here ).

3. Create a Thread class in your SurfaceView class to process the painting operation. Therefore, we need to pass the SurfaceHolder object obtained above to this thread class.

4. draw: Draw the Canvas object in the run () method of the thread class, and use the lockCanvas () method to draw the Canvas object, after painting, call unlockCanvasAndPost () to pass in the Canvas object. Then, the Surface will be drawn according to the Canvas.

Note: The previous content is retained each time SurfaceHolder is used to obtain the canvas.

Code
Public class MySurfaceView extends SurfaceView implements SurfaceHolder. callback {private DrawThread mThread = null; public MySurfaceView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init ();} public MySurfaceView (Context context, AttributeSet attrs) {super (context, attrs); init () ;}public MySurfaceView (Context context) {super (context); init () ;}private Void init () {Log. d (AppConstants. LOG_TAG, "init"); SurfaceHolder holder = getHolder (); holder. addCallback (this); mThread = new DrawThread (holder) ;}@ Override protected void onSizeChanged (int w, int h, int oldw, int oldh) {Log. d (AppConstants. LOG_TAG, "onSizeChanged"); super. onSizeChanged (w, h, oldw, oldh) ;}@ Override public void surfaceCreated (SurfaceHolder holder) {Log. d (AppConstants. LOG_TAG, "SurfaceCreated"); mThread. setRun (true); mThread. start () ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {Log. d (AppConstants. LOG_TAG, "surfaceChanged") ;}@ Override public void surfaceDestroyed (SurfaceHolder holder) {Log. d (AppConstants. LOG_TAG, "surfaceDestroyed"); mThread. setRun (false);}/*** draw Thread class **/public class DrawThread extends Thread {Private SurfaceHolder mHolder = null; private boolean isRun = false; public DrawThread (SurfaceHolder holder) {Log. d (AppConstants. LOG_TAG, "DrawThread Constructor"); mHolder = holder;} public void setRun (boolean isRun) {Log. d (AppConstants. LOG_TAG, "DrawThread setRun:" + isRun); this. isRun = isRun;} @ Override public void run () {Log. d (AppConstants. LOG_TAG, "DrawThread run"); int count = 0; whil E (isRun) {Canvas canvas = null; synchronized (mHolder) {try {Log. d (AppConstants. LOG_TAG, "Drawing -------------"); canvas = mHolder. lockCanvas (); canvas. drawColor (Color. WHITE); Paint p = new Paint (); p. setColor (Color. BLACK); Rect r = new Rect (100, 50,300,250); canvas. drawRect (r, p); canvas. drawText ("this is the" + (count ++) + "seconds", 100,310, p); Thread. sleep (1000); // The sleep duration is 1 second} catch (Exception e) {Lo G. d (AppConstants. LOG_TAG, "throw Exception in run"); e. printStackTrace () ;}finally {if (null! = Canvas) {mHolder. unlockCanvasAndPost (canvas );}}}}}}}
I am the dividing line of tiantiao

 

 

 

Reference: http://www.cnblogs.com/mengdd/archive/2013/04/09/3009897.html


What is android

Android means a robot. android phones use android phones. Google developed android phones and android phones do not know what the android phones mean.

What is android

Android means a robot. android phones use android phones. Google developed android phones and android phones do not know what the android phones mean.

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.