Basic knowledge of andriod plotting

Source: Internet
Author: User

1.Bitmap Configuration

Alpha_9: a bitmap used as an Alpha mask. It only assigns 8 bits to the Alph channel. No other colors.

Argb_4444: 4 bits are allocated for each color channel, including the alpha channel. It can represent 4096 different colors with 16 alpha values.

Argb_8888: assigns 8 bits to each color channel, including the alpha channel. It indicates 256 different colors with 16.7 million Alpha values.

Rgb_565: 5 bits are used for the red channel, 6 bits are used for the green channel, and 5 bits are used for the blue channel (without the alpha channel), which can have 65535 different colors. This setting has almost the same high quality as argb_8888, but consumes less memory.

2.Create a canvas object

If you want to create a bitmap object and draw it, you can create a canvas object for it, for example

Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager().getDefaultDisplay().getWidth(), (int) getWindowManager().getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);

 

 

3.Use of paint objects

(1)Color: You can use the setcolor method on the paint object and pass in a color object. The color class defines a series of colors, expressed as a 32-bit integer constant:

Color. Black \ color. Blue \ color. Red

You can also use the color. argb static method to construct a specific color, such:

int mycolor=Color.argb(255,128,64,32)

In fact, you can also use the paint object method setargb to specify the color:

paint.setARGB(255,128,64,32);

 

(2)Style: When using the setstyle method to define the painting object style, you must specify whether to fill the drawn shape or just draw the outline. Possible styles are defined as constants of the paint. Style Class.

Paint. style. Stroke: draws only the contour of the shape.

Paint. style. Fill: Fill shape only

Paint. style. fill_and_stroke: Fill and draw the shape outline

(3)Stroke width: You can use the setstrokewidth method on the paint object to specify.

4.Draw shape

(1)Point: You can use the drawpoint method of the canvas object.

(2)Straight Line: You can use the drawline method of the canvas object.

(3)Rectangle: The drawrect method exclusive to canvas can be used. However, this method has two reloads. The first method is the simplest method to directly input leftx, topy, rightx, bottomy, the other is to specify a rectangle by passing in rectf (the rectf class uses floating point values to represent leftx, topy, rightx, and bottomy respectively ).

 

RectF rectangle=new RectF(leftx,topy,rightx,bottomy);canvas.drawRect(rectangle,paint);

(4)
Elliptic: Similar to drawing a rectangle using a rectf object, the rectf object defines the boundary of an ellipse, that is, drawing an ellipse inside the rectangle.

 

 

RectF ovalBounds=new RectF(leftx,topy,rightx,bottomy);canvas.drawOval(ovalBounds,paint);

(5)
Circle: You can use the drawcircle method of the canvas object.

 

(6)Path: A path is a series of straight lines that can be used to create any shape. To draw a path, you must first construct a path object. The path object can be called for any number of times. By using moveTo, it is told to directly move to a certain point without drawing, or to draw a straight line to a certain point using lineto.

 

Paint paint = new paint (); Path P = New Path (); p. moveTo (20, 20); // if not moveTo, P is drawn at (0, 0) by default. lineto (1, 100,200); p. lineto (200,100); canvas. drawpath (p, paint );

5.Draw text
We can use the drawtext method of the canvas object to draw text, and set the text size with the settextsize of the paint class.

 

(1) built-in Font: The paint class provides the settypeface method and passes in a typeface object to specify which font should be used.

The typeface class defines many constants to indicate fonts attached to the Android system, for example, typeface. monospace \ typeface. sans_serif \ typeface. serif \ typeface. Default \ typeface. default_bold.

 

paint.setTypeface(Typeface.DEFAULT_BOLD);

(2) Font Style: together with the built-in font, the typeface class also defines a series of style constants. The create method in typeface can be used to modify a built-in font. The return value of this method is a new typeface object. Style list in typeface:

 

Typeface. Bold \ typeface. italic \ typeface. Normal \ typeface. bold_italic

Typeface serif_italic = typeface. Create (typeface. Serif, typeface. italic );

Paint. settypeface (serif_italic );

(3) External Font: Android apps are not limited to built-in fonts. It supports creating typeface objects from any TrueType font file. TrueType is a standard font that can be used on various platforms.

A font that is completely different from the android built-in font is the Chopin Script font created by clude Pelletier. It is applicable to the public domain and can be downloaded free of charge from various sources, such as fontspace.com.

To use this font, you can download it and put the. TTF file into the asset folder of the project. The typeface. createfromasset method accepts an assetmanager object and the file name as the parameter. The assetmanager object can be obtained by calling getassets from the context. This method returns a typeface object that can be passed to the paint. settypeface method.

 

Typeface chops=Typeface.createFromAsset(getAssets(),"ChopinScript.ttf");paint.setTypeface(chops);

(4) text in the path: the drawtextonpath method of the canvas object used

 

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.