24.Android Paint and canvas simple application learning

Source: Internet
Author: User

In Android, you need to display 2D graphics through the graphics class, which includes common classes such as canvas, paint (brush), color (colors), Bitmap (image), and so on. Graphics have features such as drawing points, lines, colors, 2D geometries, and image processing.

1.Paint (Brush) class

To draw a graphic, you first have to adjust the brush to set the relevant properties of the brush to your own development needs. The common properties of the pain class are set up as follows:

Setantialias (); Set the brush's jagged effect

SetColor (); Set the color of a brush

Setargb (); Set A, R, G, b values for a brush

Setalpha (); Set the alpha value of a brush

Settextsize (); Set the size of the font

SetStyle (); Set the style of the brush (hollow or solid)

Setstrokewidth (); Set the width of a hollow border

GetColor (); Get the color of a brush

2.Canvas (Canvas) class

DrawRect (RECTF rect, paint paint)//plot area, parameter one for RECTF one area

DrawPath (path path, paint paint)//Draw a path, parameter

Drawbitmap (Bitmap Bitmap, rect src, rect dst, paint paint)//map, parameter one is our regular Bitmap object, parameter two is the source area (here is Bitmap), the parameter three is the target area ( Should be in the canvas location and size), parameter four is the paint brush object, because it is possible to use scaling and stretching, and when the original rect is not equal to the target rect, there will be a significant loss of performance.

DrawLine (float StartX, float starty, float stopx, float stopy, paintpaint)//Draw line, the x-axis position of a starting point of the parameter, the y-axis position of the parameter two starting point, the x-axis horizontal position of the three end of the parameter, Parameter four the vertical position of the y-axis, and the last parameter is the paint brush object.

Drawpoint (float x, float y, paint paint)//Draw point, parameter one horizontal x axis, parameter two vertical y axis, third parameter is paint object.

DrawText (string text, float x, floaty, paint paint)//rendered text, canvas class In addition to the above can also depict text, parameter one is a String type of text, parameter two x axis, parameter three Y axis, Parameter four is the Paint object.

DrawOval (RECTF oval, paint paint)//Draw ellipse, parameter one is scan area, parameter two is Paint object;

Drawcircle (float CX, float CY, float radius,paint paint)//Draw circle, parameter one is the X axis of center point, parameter two is the Y axis of center point, parameter is radius, parameter four is Paint object;

DrawArc (RECTF oval, float startangle, float sweepAngle, Boolean usecenter, Paint paint)//arc, parameter one is the RECTF object, The bounds of a rectangular area ellipse are used to define the shape, size, arc, parameter two is the starting angle (degree) at the beginning of the arc, the parameter three sweep angle (degree) starts to measure clockwise, the parameter four is if this is true, including the Oval center arc, and close it.

Here's a simple example of what you can write:

First, we need to customize a class, such as MyView, to inherit from the view class. Then, the OnDraw () function of the replication view class. Finally, use the paint and canvas objects in the OnDraw () function to draw the graphics we need.

The code is as follows:

1  PackageCom.example.paint_canvasdemo;2 3 ImportAndroid.content.Context;4 ImportAndroid.graphics.Canvas;5 ImportAndroid.graphics.Color;6 ImportAndroid.graphics.Paint;7 ImportAndroid.graphics.Paint.Style;8 ImportAndroid.view.View;9 ImportJava.lang.Math;Ten  One  Public classMyViewextendsView { A      -     Private intScreenw, Screenh;//for the width and height of the screen -  the      PublicMyView (Context context) { -         Super(context); -         //TODO auto-generated Constructor stub -     } +  -     protected voidOnDraw (canvas canvas) { +         Super. OnDraw (canvas); A         //get the width and height of the screen atScreenw = This. getwidth (); -Screenh = This. GetHeight (); -          -Paint paint_circle =NewPaint (); -         //Set Color - Paint_circle.setcolor (color.blue); in         //set Hollow Style.fill to solid - Paint_circle.setstyle (style.stroke); to         //Anti- aliasing +Paint_circle.setantialias (true); -         //set the Hollow line width thePaint_circle.setstrokewidth (10); *         //Draw a circle $Canvas.drawcircle (SCREENW/2, SCREENH/2, 200, paint_circle);Panax Notoginseng          -         //Draw a square the Paint_circle.setcolor (color.red); +         intX1 = (int) Math.sqrt (2); A         intRect_xy = $ * X1/2; theCanvas.drawrect (Screenw/2-rect_xy, Screenh/2-rect_xy, Screenw/2+rect_xy, screenh/2+Rect_xy, paint_circle);  +     } -  $}

Then modify the following Mainactivity.java code:

1  PackageCom.example.paint_canvasdemo;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 6  Public classMainactivityextendsActivity {7 8 @Override9     protected voidonCreate (Bundle savedinstancestate) {Ten         Super. OnCreate (savedinstancestate); One         //Setcontentview (r.layout.activity_main); ASetcontentview (NewMyView ( This)); -     } -  the   -}

Operating effect:

24.Android Paint and canvas simple application learning

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.