Android uses canvas to draw various images

Source: Internet
Author: User
Tags drawtext

Canvas is a canvas. You can use a paint brush to draw any image on it.


Principle:

Canvas can be regarded as a proxy or interface of the Surface. The image is drawn on the Surface. Canvas encapsulates all painting calls. Using Canvas,

The content drawn to the Surface is first stored in a memory area (that is, the corresponding Bitmapz), and the Bitmap is finally displayed in the window.


Usage:

1. Canvas can be directly new Canvas ();

2. Override the OnDraw () method in the View, which contains a Canvas, which is discussed today.


Method:

// Draw a region. The parameter is set to RectF.

DrawRect (RectF rect, Paint paint)

// Draw a Path with parameter 1 as the Path object

DrawPath (Path path, Paint paint)

// Texture. Parameter 1 is our regular Bitmap object. Parameter 2 is the source region (here it is bitmap). Parameter 3 is the target region.

(It should be in the position and size of the canvas). Parameter 4 is the Paint brush object, because the possibility of scaling and stretching is used, when the original

When the Rect is not equal to the target Rect, the performance will be greatly reduced.

DrawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)

// Draw a line. The X-axis position of the start point of the parameter, the y-axis position of the second start point of the parameter, and the X-axis horizontal position of the Third end point of the parameter,

The four Y axis vertical position of the parameter. The last parameter is the Paint brush object.

DrawLine (float startX, float startY, float stopX, float stopY, Paintpaint)

// Painting point, parameter 1 Horizontal X axis, parameter 2 vertical Y axis, and third parameter is the Paint object.

DrawPoint (float x, float y, Paint paint)

// Render text. In addition to the above, the Canvas class can also depict text. Parameter 1 is String-type text, parameter 2 x axis,

The three y axes and four parameters are the Paint objects.

DrawText (String text, float x, floaty, Paint paint)

// Draw an ellipse. The first parameter is the scan area and the second parameter is the paint object;

DrawOval (RectF oval, Paint paint)

// Draw a circle. The first parameter is the X axis of the center, the second parameter is the Y axis of the center, the third parameter is the radius, and the fourth parameter is the paint object;

DrawCircle (float cx, float cy, float radius, Paint paint)

// Draw an arc, Parameter 1 is a RectF object. The boundary of an elliptical area in a rectangle is defined in shape, size, and arc. Parameter 2 is

The starting angle (degree) starts at the beginning of the arc, and the three scanning angles (degrees) start to be measured clockwise. The fourth parameter is if this is true, including

The arc at the center of the elliptic, and close it. If it is false, it will be an arc. Parameter 5 is the painting object;

DrawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint)


The next step is to start painting. You also need the tool Paint, path

Paint is the paint brush. We use new Paint (); wait for a paint brush and set its attributes.

Common Methods of Painting:

SetARGB (int a, int r, int g, int B) // set the color of the Paint object. Parameter 1 is the alpha transparent value.

SetAlpha (int a) // set the alpha opacity in the range of 0 ~ 255

SetAntiAlias (boolean aa) // indicates whether the image is anti-sawtooth.

SetColor (int color) // set the Color. Here, the color class defined in Android contains some common Color definitions.

SetTextScaleX (float scaleX) // sets the text zoom factor. 1.0f is the original

SetTextSize (float textSize) // set the font size

SetUnderlineText (booleanunderlineText) // set the underline

SetStyle (Style. STROKE) // you can specify the empty brush.


Example:

Public class DrawviewActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // set DrawView's width and heightDisplay d = getWindowManager (). getdefadisplay display (); DrawView dv = new DrawView (this); dv. width = d. getWidth (); dv. height = d. getHeight (); setContentView (dv);} class DrawView extends View {public float width; public float height; private Paint mpaint; public DrawView (Context context) {super (context ); mpaint = new Paint (); mpaint. setColor (Color. RED); mpaint. setAntiAlias (true) ;}@ Overrideprotected void onDraw (Canvas canvas) {// TODO Auto-generated method stubsuper. onDraw (canvas); // drawtextcanvas. save (); mpaint. setTextSize (30); canvas. drawText ("hello honjane", 20, 20, mpaint); canvas. restore (); // drawRectcanvas. save (); RectF r = new RectF (40, 40, 60, 60); mpaint. setColor (Color. BLUE); canvas. drawRect (r, mpaint); canvas. restore (); canvas. save (); mpaint. setColor (Color. BLUE); canvas. drawRect (65, 40, 85, 60, mpaint); canvas. restore (); // drawCirclecanvas. save (); mpaint. setStyle (Style. STROKE); canvas. drawCircle (width/2, height/2,100, mpaint); canvas. restore (); // drawArccanvas. save (); RectF oval1 = new RectF (150,300,180,400); canvas. drawArc (oval1, 180,250, false, mpaint); // small arc oval1.set (300,300,600,780); canvas. drawArc (oval1, 230,170, false, mpaint); oval1.set (200,300,500,780); canvas. drawArc (oval1, 230,170, true, mpaint); canvas. restore (); // triangle canvas. save (); mpaint. setStyle (Style. FILL); Path p = new Path (); p. moveTo (80,100); p. lineTo (1, 140,300); p. lineTo (1, 20,300); p. close (); canvas. drawPath (p, mpaint); canvas. restore (); canvas. save (); mpaint. setStyle (Paint. style. FILL); // filled with mpaint. setColor (Color. LTGRAY); mpaint. setAntiAlias (true); // you can specify the canvas for the paint brush. drawText ("Draw rounded rectangle:", 10,260, mpaint); RectF oval3 = new RectF (80,260,200,300); // set a new rectangular canvas. drawRoundRect (oval3, 20, 15, mpaint); // The second parameter is the x radius, and the third parameter is the y radius canvas. restore (); // color-changing canvas. save (); Shader shader = new LinearGradient (0, 0,100,100, new int [] {Color. BLACK, Color. CYAN, Color. DKGRAY, Color. GRAY}, null, Shader. tileMode. MIRROR); mpaint. setShader (shader); RectF oval2 = new RectF (250,100,450,300); canvas. drawArc (oval2, 200,130, true, mpaint); canvas. restore (); // draw an image canvas. save (); Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher); canvas. drawBitmap (bitmap, 250,360, mpaint); canvas. restore ();}}}





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.