Canvas painting in Android

Source: Internet
Author: User

There is a Canvas class in Android. The Canvas class represents a Canvas. You can draw what you want on it. Of course, you can also set canvas attributes, the color/size of the canvas, and so on. Canvas provides the following methods:

   Canvas (): Create an empty Canvas. You can use setBitmap () to set the Canvas to be drawn;

   Canvas (Bitmapbitmap): Creates a Canvas from a bitmap object, and draws the content on bitmap. bitmap cannot be null;

   Canvas (GLgl): used to draw 3D effects. It is related to OpenGL;

   DrawColor: Specifies the background color of the canvas;

   SetBitmap: Specifies the canvas;

   ClipRect: Set the display area, that is, set the cropping area;

 IsOpaque: checks whether Transparency is supported;

   Rotate: rotate the canvas;
 
   Canvas. the drawRect (RectF, Paint) method is used to draw a rectangle. The first parameter is the image display area, and the second parameter is the Paint brush. After setting the Rect and Paint area, you can draw a picture;

   Canvas. the drawRoundRect (RectF, float, float, Paint) method is used to draw a rounded rectangle. The first parameter is the graphic display area, the second and third parameters are the radius of the horizontal and vertical rounded corners.

   Canvas. drawLine (startX, startY, stopX, stopY, and paint): the types of the first four parameters are float, and the last parameter type is Paint. Indicates to draw a straight line from the point (startX, startY) to the point (stopX, stopY) with the paint brush;

 Canvas. drawArc (oval, startAngle, sweepAngle, useCenter, paint): the first parameter oval is of the RectF type, that is, the Arc display area. startAngle and sweepAngle are of the float type, indicates the starting angle of the arc and the arc degree respectively. The orientation of three o'clock is 0 degrees. useCenter sets whether to display the center of the circle, boolean type, and paint as the paint brush;

 The canvas. drawCircle (float, Paint) method is used to draw a circle. The first two parameters represent the coordinates of the center, the third parameter is the circle radius, and the fourth parameter is the Paint brush;

  After the usage of these functions is clear, are we just hitting the code? Don't worry. Let's make a design. Since these functions are used for drawing, that is to say, they have commonalities-painting. All of us should design an interface. For this task, only one member method is enough. For each graph, is it a painting by only one method or a painting encapsulated into a class? I suggest encapsulating it into a class. Maybe you will dislike it tomorrow and think about it, or you want to add something to each part of the image after two days. Therefore, I encapsulate every graph into a class and implement an interface called drawGraphics. Finally, remember to create a thread for the UI.

However, it should be noted that the Rect here is different from the Rectangle in Java. It turns out that the RECT structure in this article and MFC is a family.
   Rect (intleft, int top, int right, int bottom)
   Left
             X coordinate value in the upper left corner of the rectangle
 Top
   Y coordinate value in the upper left corner of the rectangle
 Right
   X coordinate value in the lower right corner of the rectangle
 Bottom
   Y coordinate value in the lower right corner of the rectangle

 I will use a picture below to explain which blog I forgot to find, such as rect (260,120.

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.