One of the Android drawings: Introduction to Canvas and Paint

Source: Internet
Author: User

Objective

In addition to using existing images, Android apps often need to dynamically generate 2D images from the scene at run time, such as mobile games, which requires support from android2d drawings. This blog is about how to use canvas for drawing under Android. Finally, a simple demo will show you how to use canvas to draw and save on ImageView.

Canvases Canvas

2D drawing under Android requires the support of the canvas class, which is located under the "Android.graphics.Canvas" package, which translates to the meaning of the canvas and is used to complete the drawing on the view.

The canvas provides two constructors:

    • Canvas (): Creates an empty canvas object.
    • Canvas (Bitmap Bitmap): Creates a canvas with a Bitmap bitmap background.

Since canvas is mainly used for 2D drawing, it also provides a number of corresponding drawxxx () methods, so that we can draw on the canvas object, Drawxxx () has many types, it is possible to draw: points, lines, rectangles, circles, ellipses, text, bitmaps and other graphics, This is no longer an introduction, but only a few of the methods commonly used in canvas:

    • void Drawbitmap (Bitmap bitmap,float left,float top,paint paint): Draws a bitmap at the specified coordinates.
    • void DrawLine (float startx,float starty,float stopx,float stopy,paint paint): Draws a line between the given starting and ending points.
    • void DrawPath (Path path,paint paint): Draws lines based on the given Path.
    • void Drawpoint (float x,float y,paint paint): Draws a point based on the given coordinates.
    • void DrawText (String text,int start,int end,paint paint): Draws the text based on the given coordinates.
    • int getheight (): Gets the height of the canvas.
    • int getwidth (): Gets the width of the canvas.

  

Brush Paint

From the several canvas.drawxxx () methods listed above, there is a parameter of type paint, which can be interpreted as a "brush", through which the canvas is painted on canvas. It is located under the "Android.graphics.Paint" package and is used primarily to set drawing styles, including brush colors, brush weights, fill styles, and more.

There are a number of ways to set the drawing style in paint, and here are just a few of the most common and advanced content that can be explained in more detail:

    • Setargb (int a,int r,int g,int b): Sets the ARGB color.
    • SetColor (int color): Sets the color.
    • Setalpha (int a): Sets the transparency.
    • Setpatheffect (Patheffect effect): Sets the path effect when the path is drawn.
    • Setshader (Shader Shader): Sets the fill effect of the paint.
    • Setantialias (Boolean AA): Sets whether anti-aliasing.
    • Setstrokewidth (float width): Sets the stroke width of the paint.
    • SetStyle (Paint.style Style): Sets the fill style of the Paint.
    • Settextsize (float textSize): Sets the text size when the text is drawn.

Setxfermode

Set the mode at which two pictures intersect

We know that under normal circumstances, drawing on an existing image will add a new layer of shape on top of it. If the new paint is completely opaque, it will completely obscure the paint below;

And setxfermode can solve this problem.


In general, the usage is like this.

Java code
    1. Canvas canvas = new Canvas (BITMAP1);
    2. Paint.setxfermode (new Porterduffxfermode (mode.src_in));
    3. Canvas.drawbitmap (Mask, 0f, 0f, paint);


It is the problem that is displayed when the image mask is drawn on the image bitmap1.

Canvas's original picture can be understood as the background is DST
The picture on the new picture can be understood as the foreground is SRC


The values of mode are as follows

One of the Android drawings: Introduction to Canvas and Paint

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.