1.1 基本图形绘制
1.1.1 概述
- Paint、Canvas
1.1.2 画笔的基本设置
1.setAntiAlias() 抗锯齿
2.setColor() 颜色
3.setStyle() 填充样式
Paint.Style.FILL
Paint.Style.FILL_AND_STROKE
Paint.Style.STROKE
4.setStrokeWidth() 描边宽度值
1.1.3 Canvas 使用基础
- 设置画布背景
void drawColor()
void drawARGB()
void drawRGB()
- 画直线
1.void drawLine()
与画笔的 Style 没有关系,与 StrokeWidth 有关
2.void drawLines() 多条直线
- 点
1.void drawPoint()
1.void drawPoints() 多个点
- 矩形工具类 RectF、Rect
1.RectF 保存 float,Rect 保存 int 类型
Rect rect = new Rect(10,10,100,100);
Rect rect = new Rect();
rect.set(10,10,100,100);
- 矩形
1.普通矩形
void drawRect(float left, float top, float right, float bottom, Paint paint)
void drawRect(RectF rect, Paint paint)
void drawRect(Rect r, Paint paint)
2.圆角矩形
void drawRoundRect(RectF rect, float rx, float ry, Paint paint)
- 圆形
void drawCircle(float cx, float cy, float radius, Paint paint)
- 椭圆
void drawOval(RectF oval, Paint paint)
- 弧
void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
startAngle:弧开始的角度,以 X 轴正方向为 0°。
sweepAngle:弧持续的角度。
useCenter:是否有弧的两边
1.1.4 Rect 与 RectF
boolean contains(int x, int y)
boolean contains(int left, int top, int right, int bottom)
boolean contains(Rect r)
static boolean intersects(Rect a, Rect b)
boolean intersects(int left, int top, int right, int bottom)
boolean intersect(int left, int top, int right, int bottom)
boolean intersect(Rect r)
public void union(int left, int top, int right, int bottom)
public void union(Rect r)
public void union(int x, int y)
1.1.5 Color
static int argb(int alpha, int red, int green, int blue)
static int rgb(int red, int green, int blue)
static int alpha(int color)
static int red(int color)
static int green(int color)
static int blue(int color)
1.2 路径
1.2.1 概述
void drawPath(Path path, Paint paint)
1.2.2 直线路径
void moveTo(float x1, float y1)
void lineTo(float x2, float y2)
void close()
1.2.3 弧线路径
void arcTo(RectF oval, float startAngle, float sweepAngle)
void arcTo(float left, float top, float right, float bottom, float startAngle, float sweepAngle,boolean forceMoveTo)
void arcTo(RectF oval, float startAngle, float sweepAngle, boolean
forceMoveTo)
forceMoveTo:是否强制将弧的起始点作为绘制起始位置
1.2.4 addXXX系列函数
- 作用:让我们直接往 Path 中添加一些曲线,而不必考虑连贯性
void addRect(float left, float top, float right, float bottom, Path.Direction
dir)
void addRect(RectF rect, Path.Direction dir)
Path.Direction:用于依据生成方向排版的文字
Path.Direction.CCW:是 counter-clockwise 的缩写,指创建逆时针方向的矩形路径。
Path.Direction.CW:是 clockwise 的缩写,指创建顺时针方向的矩形路径。
void addRoundRect(RectF rect, float[] radii, Path.Direction dir)
void addRoundRect(RectF rect, float rx, float ry, Path.Direction dir)
void addCircle(float x, float y, float radius, Path.Direction dir)
void addOval(RectF oval, Path.Direction dir)
void addArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle)
void addArc(RectF oval, float startAngle, float sweepAngle)
1.2.5 填充模式
path.setFillType()
FillType.WINDING:默认值,当两个图形相交时,取相交部分显示。
FillType.EVEN_ODD:取 path 所在并不相交的区域。
FillType.INVERSE_WINDING:取 path 的外部区域。
FillType.INVERSE_EVEN_ODD:取 path 的外部和相交区域
1.2.6 重置路径
- 路径对象一旦被重置,其中保存的所有路径都将被清空
void reset() 会清除内存,但不会清除 FillType。
void rewind() 不会清除内存,但会清除 FillType;
1.3 文字
paint.setStyle(Paint.Style.FILL);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(12);
paint.setFakeBoldText(true);
paint.setUnderlineText(true);
paint.setTextSkewX((float) -0.25);
paint.setStrikeThruText(true);
paint.setTextScaleX(2);
1.3.2 Canvas 绘制文本
void drawText(String text, float x, float y, Paint paint)
void drawText(CharSequence text, int start, int end, float x, float y, Paint paint)
void drawText(String text, int start, int end, float x, float y, Paint paint)
void drawText(char[] text, int index, int count, float x, float y, Paint paint)
void drawPosText(String text, float[] pos, Paint paint)
void drawPosText(char[] text, int index, int count, float[] pos, Paint paint)
void drawTextOnPath (String text, Path path, float hOffset, float vOffset, Paint paint)
void drawTextOnPath (char[] text, int index, int count, Path path, float hOffset, float vOffset,Paint paint)
hOffset: X 轴方向的偏移量
vOffset: Y 轴方向的偏移量
Typeface setTypeface(Typeface typeface)
Typeface.SANS_SERIF、Typeface.MONOSPACE、Typeface.SERIF
Typeface.NORMAL:正常字体。
Typeface.BOLD:粗体。
Typeface.ITALIC:斜体。
Typeface.BOLD_ITALIC:粗斜体。
Typeface createFromAsset(AssetManager mgr, String path)
Typeface createFromFile(String path)
Typeface createFromFile(File path)
1.4 Region
- 区域,任意形状的封闭图形。
1.4.1 Region
public Region(Region region)
public Region(Rect r)
public Region(int left, int top, int right, int bottom)
private void drawRegion(Canvas canvas,Region rgn,Paint paint) {
RegionIterator iter = new RegionIterator(rgn);
Rect r = new Rect();
while (iter.next(r)) {
canvas.drawRect(r, paint);
}
}
set()函数
1.4.2 枚举区域-RegionIterator类
RegionIterator(Region region)
boolean next(Rect r)
boolean union(Rect r)
boolean op()
1.5 Canvas
- 每次调用 drawXXX 系列函数来绘图时,都会产生一个全新的 Canvas 透明图层。
- 如果在调用 drawXXX 系列函数前,调用平移、旋转等函数对 Canvas 进行了操作,那么这个操作是不可逆的。每次产生的画布的最新位置都是执行这些操作后的位置。
- 在 Canvas 图层与屏幕合成时,超出屏幕范围的图像是不会显示出来的。
void translate(float dx, float dy)
void rotate(float degrees)
void rotate(float degrees, float px, float py)
public void scale(float sx, float sy)
void skew(float sx, float sy)
clipRect()
clipRegion()
1.5.2 画布的保存与恢复
- save():每次调用 save() 函数,都会先保存当前画布的状态,然后将其放入特定的栈中。
- restore():每次调用 restore()函数,都会把栈中顶层的画布状态取出来,并按照这个状态恢复当前的画布,然后在这个画布上作画。
- restoreToCount(int saveCount):将指定索引的画布作为当前画布。
1.6.1 控件概述
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}