Android paint source code analysis

Source: Internet
Author: User
Tags getcolor

 

Today, I learned the paint class and decided to start learning from the source code.

First, let's take a look at the definition of the class, which has many features: public class paint extends _ original_paint. What is the next _ original_paint? I hope you can explain it and I don't understand it.

Style is used, which is an internal enumeration class. There are three types: Fill (0), stroke (1), fill_and_stroke (2 );

/**
* The style specifies if the primitive being drawn is filled,
* Stroked, or both (in the same color). The default is fill.
*/
Public Enum Style

There is also align, which is still an internal enumeration class. There are three values: Left (0), center (1), right (2 );

/**
* Align specifies how drawtext aligns its text relative to
* [X, y] coordinates. The default is left.
*/
Public Enum align

There is a join to be solved...

/**
* The join specifies the treatment where lines and curve segments
* Join on a stroked path. The default is miter.
*/
Public Enum join

There is also a cap. I don't know how to use it. I want to check it...

/**
* The CAP specifies the treatment for the beginning and ending
* Stroked lines and paths. The default is butt.
*/
Public Enum cap

 

Internal static class: we can see from the code that painting has a lot to do with this fontinfo.

Public static final class fontinfo {
Font mfont;
Java. AWT. fontmetrics mmetrics;
}

 

There are three constructor types:

Public paint (){
This (0 );
}

Public paint (INT flags ){
Setflags (flags | default_paint_flags );
Initfont ();
}

Public paint (paint ){
Set (paint );
Initfont ();
}

 

 

Typeface is used later. I personally think it is a font style class. The content in the source code is relatively simple.

 

The following methods are basically some basic methods, such as font color, size, and margin settings.

 

The paint brush in Android is a paint class, which contains many methods to set its attributes. The main method is as follows:

Setantialias: Set the watermark effect of the paint brush.
Setcolor: Set the paint brush color.
Setargb: Set the, R, P, and g Values of the paint brush.
Setalpha: Set the Alpha value.
Settextsize: Set the font size.
Setstyle: Set the paint brush style, hollow or solid.
Setstrokewidth: Set the width of the hollow border.
Getcolor: get the paint brush color.
Getalpha: Get the Alpha value of the paint brush.

Paint class Introduction

*

* Paint is the paint brush, which plays an extremely important role in the drawing process. The paint brush mainly stores the color,

* Style and other painting information, specifying how to draw text and graphics, there are many ways to set the paint brush object,

* Generally, there are two types: one is related to drawing, and the other is related to text drawing.

*

* 1. Drawing

* Setargb (int A, int R, int g, int B );

* Set the color of the painting. A indicates transparency, R, G, and B indicates the color value.

*

* Setalpha (int );

* Set the transparency of the drawing.

*

* Setcolor (INT color );

* Set the color to be drawn, which is represented by a color value. The color value includes transparency and RGB color.

*

* Setantialias (Boolean aa );

* Setting whether to use the anti-aliasing function consumes a large amount of resources and slows down the drawing speed.

*

* Setdither (Boolean dither );

* Setting whether to use image jitter processing will make the color of the drawn image smoother and fuller, and the image clearer

*

* Setfilterbitmap (Boolean filter );

* If this parameter is set to true, the bitmap image is filtered out during the animation to accelerate the display.

* Speed. This setting item depends on dither and xfermode settings.

*

* Setmaskfilter (maskfilter );

* Set maskfilter. Different maskfilters can be used to implement filter effects, such as filtering and stereo *

* Setcolorfilter (colorfilter );

* Set the color filter to achieve the effect of changing colors that are not needed when drawing colors.

*

* Setpatheffect (patheffect effect );

* Set the effect of the draw path, such as drawing a line.

*

* Setshader (shader );

* Set the image effect. You can use shader to draw different gradient effects.

*

* Setshadowlayer (float radius, float dx, float dy, int color );

* Set the shadow layer under the image to produce the shadow effect. radius indicates the shadow angle. dx and Dy indicate the distance between the shadow on the X and Y axes, and color indicates the shadow color.

*

* Setstyle (paint. Style );

* Set the paint brush style to fill, fill_or_stroke, or stroke.

*

* Setstrokecap (paint. Cap CAP );

* When the paint brush style is stroke or fill_or_stroke, set the image style of the brush, such as the circle style.

* Cap. round, or square style cap. Square

*

* Setsrokejoin (paint. Join join );

* Set the combination of various images during painting, such as smooth effects.

*

* Setstrokewidth (float width );

* When the paint brush style is stroke or fill_or_stroke, set the width of the paint brush.

*

* Setxfermode (xfermode );

* Sets the processing method for overlapping images, such as merge, intersection, or union. It is often used to make the eraser erasure effect.

*

* 2. Text Rendering

* Setfakeboldtext (Boolean fakeboldtext );

* Simulate bold text and set it to a small font.

*

* Setsubpixeltext (Boolean subpixeltext );

* Setting this parameter to true will help display the text on the LCD screen.

*

* Settextalign (paint. Align align );

* Set the alignment direction of the drawn text

*

* Settextscalex (float scalex );

* You can set the zoom ratio on the X axis of the drawn text to stretch the text.

*

* Settextsize (float textsize );

* Set the font size of the drawn text.

*

* Settextskewx (float skewx );

* Set italic text. skewx is a skewed radian.

*

* Settypeface (typeface );

* Set the typeface object, that is, the font style, including bold, italic, and linefeed, non-linefeed, etc.

*

* Setunderlinetext (Boolean underlinetext );

* Set the underlined text effect.

*

* Setstrikethrutext (Boolean strikethrutext );

* Set the effect of strikethrough.

 

 

The test code is as follows:

Package com. example. fsdfdfs;

Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. util. log;
Import Android. View. keyevent;
Import Android. View. motionevent;
Import Android. View. view;

Public class gameview extends view {

Public final static string tag = "tag ";
// Declare the paint object
Private paint mpaint = NULL;

Public gameview (context ){
Super (context );
// Build object
Mpaint = new paint ();
}

@ Override
Protected void ondraw (canvas ){
Super. ondraw (canvas );

// Set the paint to non-sawtooth
Mpaint. setantialias (true );

// Set the paint color
Mpaint. setcolor (color. Red );
Mpaint. setcolor (color. Blue );
Mpaint. setcolor (color. Yellow );
Mpaint. setcolor (color. Green );
// Set the color.
Mpaint. setcolor (color. RGB (255, 0, 0 ));
// This is also set color
Mpaint. setargb (255,255, 0, 0 );

// Extract color
Color. Red (0 xcccccc );
Color. Green (0 xcccccc );

// Set the paint color and Alpha value (A, R, G, B)
Mpaint. setalpha (220 );

// This can be set as another paint object.
// Mpaint. Set (new paint ());
// Set the font size
Mpaint. settextsize (14 );

// Set the paint style to "Hollow"
// Of course, you can also set it to "solid" (paint. style. Fill)
Mpaint. setstyle (paint. style. Stroke );

// Set the width of the "Hollow" outer box
Mpaint. setstrokewidth (5 );

// Obtain the paint color, Alpha value, outer frame width, and font size.
Log. I ("tag", "paint color ------>" + mpaint. getcolor ());
Log. I (TAG, "paint Alpha ------->" + mpaint. getalpha ());
Log. I ("tag", "paint strokewidth --------->" + mpaint. getstrokewidth ());
Log. I ("tag", "paint textsize ----------->" + mpaint. gettextsize ());

// Draw a hollow rectangle
Canvas. drawrect (320-80), 20, (320-80)/2 + 80, 20 + 40, mpaint );

// Set the style to solid
Mpaint. setstyle (paint. style. Fill );

Mpaint. setcolor (color. Green );

// Draw a green solid rectangle
Canvas. drawrect (0, 20, 40, 20 + 40, mpaint );
}

// Trigger event
Public Boolean ontouchevent (motionevent event ){
Return true;
}

// Press the button
Public Boolean onkeydown (INT keycode, keyevent event ){
Return true;
}

// Trigger an event by pressing a button
Public Boolean onkeyup (INT keycode, keyevent event ){
Return true;
}

Public Boolean onkeymultiple (INT keycode, int repeatcount, keyevent event ){
Return true;
}

}

 

Activity:

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

Setcontentview (New gameview (this ));
}

 

 

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.