Chapter 6 painting with White Paper-Canvas canvas (1)

Source: Internet
Author: User
Tags drawtext

Chapter 6 painting with White Paper-Canvas canvas (1)
Chapter 6 painting with White Paper-Canvas

The previous sections detail the use of Android UI components. Through the previous chapter, developers can develop satisfactory UI effects. However, sometimes we need to implement a more beautiful UI effect. At this time, we may not be able to directly use the UI components, but we need to draw various UI effects on our own.

In Android, Canvas is a Canvas where developers can draw anything they want. In this chapter, we will introduce Canvas and related technologies.

6.1 Canvas introduction 6.1.1View Canvas-Canvas drawing with normal View

Java provides the Canvas class from the j2-midlet. Currently, on the Android platform, the main task is to manage the painting process, just like a Canvas, any painting will be completed on this canvas.

The Canvas class has three constructor Methods: Construct an empty Canvas, construct (2D) from the Bitmap, and create (3D) from the GL object, as follows:

Canvas (); // generally used in the View Canvas

Canvas (Bitmap bitmap); // create a map from a 2D Object

Canvas (GL gl); // create a 3D object

 

Due to some inherent problems in Java, considering the processing speed of hardware, more 3D applications need to be implemented using the underlying library of C ++ to provide a better user experience. Here we will focus on the functions of Canvas in 2D.

First, let's take a look at how to use the Canvas painting of a common View.

Generally, the procedure is as follows:

1) define your own View: class your_view extends View {}.

2) Reload the onDraw method of View: protectedvoid onDraw (Canvas canvas ){}.

3) define your own painting operations in the onDraw method.

4) use it in code or layout files.

For example, we can implement a textview with border text. The code snippet is as follows.

// Import omitted

Public class ShadeTextView extends TextView {

Public ShadeTextView (Context context, AttributeSet attrs ){

Super (context, attrs );

}

@ Override

Public void onDraw (Canvas canvas ){

Super. onDraw (canvas );

DrawText (canvas, 0xffff0000 );

Super. onDraw (canvas );

}

// Note: only single-line word processing is performed here.

Private void drawText (Canvas canvas, int bg ){

Paint paint = getPaint ();

// Obtain textview text

String text = String. valueOf (getText ());

// Obtain the left margin of the first line of text

Float startX = getLayout (). getLineLeft (0 );

// Obtain the bottom distance of the first line of text

Float startY = getBaseline ();

Paint. setColor (bg );

Canvas. drawText (text, startX + 1, startY, paint );

Canvas. drawText (text, startX, startY-1, paint );

Canvas. drawText (text, startX, startY + 1, paint );

Canvas. drawText (text, startX-1, startY, paint );

}

}

 

Use the following in the layout file:

<Com. yourpackage. ShadeTextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "test shadow text"

Android: textSize = "20sp"/>

 

Figure 6-1 implementation result of TextView with border

 

Here, we create a class ShadeTextView that inherits from the TextView class, overwrites the onDraw (Canvas canvas) method, and draws a border in this method.

6.1.2 Bitmap Canvas-use a Canvas for normal Bitmap

We can also define our own Bitmap, then generate a Canvas that belongs to this Bitmap, and draw on it as needed. You can use this image freely.

This method is mainly used for custom drawing, and for scenarios where refreshing is fast and dual buffering is required to prevent the screen from flashing.

The sample code is as follows.

Bitmap B = Bitmap. createBitmap (100,100, Bitmap. Config. ARGB_8888 );

// The Bitmap must be placed in the View Canvas to display the picture.

Canvas c = new Canvas (B );

6.1.3SurfaceView Canvas-use SurfaceView Canvas to draw images

We can see how to use the Canvas of a common View and the Canvas of a common image. Android also encapsulates a SurfaceView class to help us use the Canvas of the View for drawing. The main difference between SurfaceView mode and View mode is that SurfaceView defines a special thread to complete the drawing work. The application does not need to wait for the View to be refreshed, thus improving the performance. The View method is suitable for animations with relatively small processing capacity and low frame rate, such as chess games. The SurfaceView method is mainly used in games and high-quality animations.

To use SurfaceView, follow these steps:

1) define a SurfaceView: class your_SurfaceView extends extendsSurfaceView implements SurfaceHolder. Callback (){}

2) three methods for implementing SurfaceHolder. Callback: surfaceCreated (), surfaceChanged (), surfaceDestroyed ()

3) define your own focus on drawing Thread: class your_thread extends Thread

4) run () function of the overload thread (start this thread in surfaceCreated () of SurfaceView)

The following sample code details the thread processing process.

// Import omitted

Public class YourViewThread extends Thread {

 

// Number of milliseconds of sleep

Private int sleepSpan = 100;

// Cyclic mark bit

Private boolean flag = false;

// Reference the game interface

Private YourSurfaceView mYourSurfaceView;

Private SurfaceHolder mSurfaceHolder = null;

Public YourViewThread (YourSurfaceView mYourSurfaceView, SurfaceHolder mSurfaceHolder ){

This. mYourSurfaceView = mYourSurfaceView;

This. mSurfaceHolder = mSurfaceHolder;

}

Public void run (){

// Canvas

Canvas c;

While (flag ){

C = null;

Try {

// Lock the entire canvas. We recommend that you do not set the parameter to null when the memory requirement is high.

C = mSurfaceHolder. lockCanvas (null );

Synchronized (this. mSurfaceHolder ){

Try {

MYourSurfaceView. onDraw (c );

} Catch (Exception e ){}

}

} Finally {

If (c! = Null ){

// Update the Screen Content

MSurfaceHolder. unlockCanvasAndPost (c );

}

}

Try {

// SleepSpan millisecond

Thread. sleep (sleepSpan );

} Catch (Exception e ){}

}

}

 

Public void setFlag (boolean flag ){

// Set the cyclic mark

This. flag = flag;

}

}

---------------------------------------- It is difficult for programmers to make money. Therefore, they must learn financial management. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. We recommend investing in don't invest in security, which is almost impossible to transfer. It is very difficult to withdraw the website link in advance. Don't make it in white --------------------------------------------

The following sample code details the drawing process.

// Import omitted

Public class YourSurfaceView extends SurfaceView

Implements SurfaceHolder. Callback {

 

Private YourViewThread mYourViewThread;

 

Public YourSurfaceView (Activity activity ){

Super (activity );

// Pass the SurfaceView canvas handle to the refresh thread

MYourViewThread = new YourViewThread (this, getHolder ());

GetHolder (). addCallback (this );

}

 

Protected void onDraw (Canvas canvas ){

If (canvas = null ){

Return;

}

}

 

@ Override

Public boolean onTouchEvent (MotionEvent event ){

Return true;

}

 

@ Override

Public boolean onKeyDown (int keyCode, KeyEvent event ){

Return false;

}

 

@ Override

Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){

// Automatically called when the canvas changes

}

 

@ Override

Public void surfaceCreated (SurfaceHolder holder ){

// Automatically called when the canvas is created

Try {

// Start the refresh thread

MYourViewThread. setFlag (true );

MYourViewThread. start ();

} Catch (Exception ex ){

MYourViewThread = new YourViewThread (this, getHolder ());

MYourViewThread. setFlag (true );

MYourViewThread. start ();

}

}

 

@ Override

Public void surfaceDestroyed (SurfaceHolder holder ){

// Automatically called when the canvas is destroyed

Boolean retry = true;

MYourViewThread. setFlag (false );

While (retry ){

Try {

MYourViewThread. join ();

Retry = false;

} Catch (InterruptedException e ){

}

}

}

}

Related Article

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.