关于canvas的使用

前言

1
2
3
4
5
6
7
8
WHO AM I:

The Canvas class holds the "draw" calls. To draw something, you need
4 basic components:
1.a Bitmap to hold the pixels
2.a Canvas to host the draw calls (writing into the bitmap)
3.a drawing primitive (e.g. Rect,Path, text, Bitmap)
4.a paint (to describe the colors and styles for the *drawing)

宏观图

Canvas_Main

易混淆用法

canvas

drawArc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
### arc center is the left,top,right,bottom center to darw it
### param:startAngle and SWEEPANGLE
* <p>Draw the specified arc, which will be scaled to fit inside the
* specified oval.</p>
*
* <p>If the start angle is negative or >= 360, the start angle is treated
* as start angle modulo 360.</p>
*
* <p>If the sweep angle is >= 360, then the oval is drawn
* completely. Note that this differs slightly from SkPath::arcTo, which
* treats the sweep angle modulo 360. If the sweep angle is negative,
* the sweep angle is treated as sweep angle modulo 360</p>
*
* <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the
* geometric angle of 0 degrees (3 o'clock on a watch.)</p>
*
* @param startAngle Starting angle (in degrees) where the arc begins
* @param sweepAngle Sweep angle (in degrees) measured clockwise
* @param useCenter If true, include the center of the oval in the arc, and
close it if it is being stroked. This will draw a wedge
* @param paint The paint used to draw the arc
*/
public void drawArc(float left, float top, float right, float bottom, float startAngle,
float sweepAngle, boolean useCenter, @NonNull Paint paint) {
native_drawArc(mNativeCanvasWrapper, left, top, right, bottom, startAngle, sweepAngle,
useCenter, paint.getNativeInstance());
}

path

Path中的addArc vs arcTo

Canvas_Diff_Arc

setFillType && op

有点类似于paint的setXferMode(),是path在图形叠加方面的处理功能接口

#####

paint中的工具方法

measuretext vs gettextbounds
measuretext
  1. 它返回的是一个浮点数
  2. 它给出的是文字的实际占用面积,是在最小占用面积的前后增加了advanceX,所以会比后者要宽
gettextbounds
  1. 它返回的是一个整数
  2. 它给出的是文字的最小占用面积
breakText找出换行处
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

/**
* Measure the text, stopping early if the measured width exceeds maxWidth.
* Return the number of chars that were measured, and if measuredWidth is
* not null, return in it the actual width measured.
*
* @param text The text to measure. Cannot be null.
* @param index The offset into text to begin measuring at
* @param count The number of maximum number of entries to measure. If count
* is negative, then the characters are measured in reverse order.
* @param maxWidth The maximum width to accumulate.
* @param measuredWidth Optional. If not null, returns the actual width
* measured.
* @return The number of chars that were measured. Will always be <=
* abs(count).
*/
public int breakText(char[] text, int index, int count,
float maxWidth, float[] measuredWidth) {
if (text == null) {
throw new IllegalArgumentException("text cannot be null");
}
if (index < 0 || text.length - index < Math.abs(count)) {
throw new ArrayIndexOutOfBoundsException();
}

if (text.length == 0 || count == 0) {
return 0;
}
if (!mHasCompatScaling) {
return native_breakText(mNativePaint, mNativeTypeface, text, index, count, maxWidth,
mBidiFlags, measuredWidth);
}

final float oldSize = getTextSize();
setTextSize(oldSize * mCompatScaling);
int res = native_breakText(mNativePaint, mNativeTypeface, text, index, count,
maxWidth * mCompatScaling, mBidiFlags, measuredWidth);
setTextSize(oldSize);
if (measuredWidth != null) measuredWidth[0] *= mInvCompatScaling;
return res;
}
打开另一扇门,文字转化成path

通过paint中提供的方法getTextPath(char[] text, int index, int count, float x, float y, Path path)