About 2D Drawing tools classes (Canvas,paint and color) in Android

Source: Internet
Author: User
Tags drawtext

The usual Android application development in the picture, look for art design some pictures, put in the project on it, but in addition to using existing images, we can also use 2D drawing, is to draw some patterns or text in view. The classes that are commonly used are canvas (canvas), Paint (brush) and color (used to set brush colors) classes, and of course, many other classes, depending on the needs of your project.

Canvas: Canvases are used to draw graphs such as rectangles, circles, text, bitmaps, etc. directly on the view. Official API URL: http://developer.android.com/reference/android/graphics/Canvas.html. the objects that canvas can draw are: arcs (arcs), fill colors (ARGB and color), Bitmap, circles (circle and Oval), points (point), lines (line), rectangles (Rect), pictures (picture), rounded rectangles (roundrect), text ( Text), vertex (Vertices), Path. Another two common methods are:

Save: Used to saveCanvasthe state. Saveafter that, you can callCanvastranslation, scaling, rotation, error cutting, cropping, and so on. Restore: Used to restoreCanvasthe previously saved state. PreventSaveafter theCanvasthe actions performed have an impact on subsequent drawing. Saveand theRestoreto pair with (Restorecan be comparedSaveless, but not much), ifRestorenumber of calls thanSavemore, will causeError.

Paint: A brush that is used on the canvas to set some parameters of our drawing pattern, such as line width (thickness), color, etc. The common settings are:

Setetantialias: Sets the brush's jagged effect.

SetColor: Setting Colors

Setargb: Sets the ARGB color value.

Setalpha: Setting alpha value

Settextsize: Sets the font size.

SetStyle: Sets the brush style, hollow or solid.

Setstrokewidth: Sets the hollow border width.

Setshader: Setting the Shadow Effect

Settextskewx: Set Text tilt

Color: Colors class, mainly provides the color of the brush and so on. You can define the color in Res/values/colors.xml, or you can use the system-provided color values, and also notify Color.parsecolor () to set the colors. Common system color values such as:

Color.Black;
Color.White;
Color.Blue;
color.red;
Color.yellow ...

Here's a small example:

public class Mydrawview extends View {
PublicMydrawview(Context context) {
Super (context);
}
@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Paint p = new paint ();
P.setcolor (Color.Blue);
Canvas.drawtext ("Draw circle:", ten, p);
P.setcolor (Color.yellow);
Canvas.drawline (Max, Max, Max, p);//Draw Line
P.setstyle (Paint.Style.FILL);
Canvas.drawtext ("Draw a point:", ten, A, p);
Canvas.drawpoint (390, p);//Draw a dot
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);//Draw a map
Canvas.drawbitmap (Bitmap, 250,360, p);
}
}


About 2D Drawing tools classes (Canvas,paint and color) in Android

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.