《Android自定义控件入门与实战》读书笔记--第1章 绘图基础

1.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 使用基础

  1. 设置画布背景
void drawColor()
void drawARGB()
void drawRGB()
  1. 画直线
1.void drawLine()
与画笔的 Style 没有关系,与 StrokeWidth 有关
2.void drawLines() 多条直线 
1.void drawPoint()
1.void drawPoints() 多个点
  1. 矩形工具类 RectF、Rect
1.RectF 保存 float,Rect 保存 int 类型

//1.直接构造
Rect rect = new Rect(10,10,100,100); 
//2.间接构造
Rect rect = new Rect(); 
rect.set(10,10,100,100);
  1. 矩形
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)
  1. 圆形
void drawCircle(float cx, float cy, float radius, Paint paint)
  1. 椭圆
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

//1.判断是否包含某个点
boolean contains(int x, int y)
//注:postInvalidate()函数中就是利用 handler 给主线程发送刷新界面的消息来实现的,刷新速度比 invalidate() 慢

//2.判断是否包含某个矩形
boolean contains(int left, int top, int right, int bottom) 
boolean contains(Rect r)

//3.静态方法判断是否相交
static boolean intersects(Rect a, Rect b)

//4.成员方法判断是否相交
boolean intersects(int left, int top, int right, int bottom)

//5.判断相交并返回结果
boolean intersect(int left, int top, int right, int bottom) //会改变 rect1 的值
boolean intersect(Rect r)

//6.合并两个矩形
public void union(int left, int top, int right, int bottom) 
public void union(Rect r)

//7.合并矩形某个点
public void union(int x, int y)

1.1.5 Color

//1.带透明度
static int argb(int alpha, int red, int green, int blue)

//2.不带透明度的颜色
static int rgb(int red, int green, int blue)

//3.提取颜色
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系列函数

  1. 作用:让我们直接往 Path 中添加一些曲线,而不必考虑连贯性
//1.添加矩形路径
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 的缩写,指创建顺时针方向的矩形路径。

//2.添加圆角矩形路径
void addRoundRect(RectF rect, float[] radii, Path.Direction dir) 
void addRoundRect(RectF rect, float rx, float ry, Path.Direction dir)

//3.添加圆形路径
void addCircle(float x, float y, float radius, Path.Direction dir)

//4.添加椭圆路径
void addOval(RectF oval, Path.Direction dir)

//5.添加弧形路径
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 重置路径

  1. 路径对象一旦被重置,其中保存的所有路径都将被清空
void reset()  会清除内存,但不会清除 FillType。
void rewind() 不会清除内存,但会清除 FillType;

1.3 文字

paint.setStyle(Paint.Style.FILL); //绘图样式,对于文字和几何图形都有效 
paint.setTextAlign(Align.CENTER); //设置文字对齐方式,Align.CENTER、Align.LEFT、Align.RIGHT 
paint.setTextSize(12); //设置文字大小 
 
//样式设置 
paint.setFakeBoldText(true); //粗体文字 
paint.setUnderlineText(true); //下划线 
paint.setTextSkewX((float) -0.25); //设置字体水平倾斜度,普通斜体字设为-0.25 
paint.setStrikeThruText(true); //设置带有删除线效果 
paint.setTextScaleX(2); //只会将水平方向拉伸,高度不会变

1.3.2 Canvas 绘制文本

//1.普通绘制
void drawText(String text, float x, float y, Paint paint)

//2.截取绘制
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)

//3.逐个指定文字位置
void drawPosText(String text, float[] pos, Paint paint) 
void drawPosText(char[] text, int index, int count, float[] pos, Paint paint)

//4.路径绘制
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 轴方向的偏移量

//5.设置文字样式
Typeface setTypeface(Typeface typeface)
//5.1.自带
Typeface.SANS_SERIF、Typeface.MONOSPACE、Typeface.SERIF
//5.2.defaultFromStyle() 根据字体样式获取对应的默认字体
Typeface.NORMAL:正常字体。
Typeface.BOLD:粗体。
Typeface.ITALIC:斜体。
Typeface.BOLD_ITALIC:粗斜体。
//5.3.create(String familyName, int style) 指定字体名来加载系统中自带的字体样式
//5.3.自定义字体样式
Typeface createFromAsset(AssetManager mgr, String path) 
Typeface createFromFile(String path) 
Typeface createFromFile(File path)

1.4 Region

  1. 区域,任意形状的封闭图形。

1.4.1 Region

//1.直接构造
public Region(Region region) //复制一个 Region 的范围 
public Region(Rect r) //创建一个矩形区域 
public Region(int left, int top, int right, int bottom) //创建一个矩形区域
//1.1.自己写一个方法
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); 
 } 
}
//本意并不是用来绘图的
//2.间接构造
set()函数

1.4.2 枚举区域-RegionIterator类

//构造函数:根据区域构建对应的矩形集
RegionIterator(Region region)
//获取下一个矩形,将结果保存在参数 Rect r 中。
boolean next(Rect r)

//1.区域相交
boolean union(Rect r)
//2.区域操作
boolean op() 

1.5 Canvas

  1. 每次调用 drawXXX 系列函数来绘图时,都会产生一个全新的 Canvas 透明图层。
  2. 如果在调用 drawXXX 系列函数前,调用平移、旋转等函数对 Canvas 进行了操作,那么这个操作是不可逆的。每次产生的画布的最新位置都是执行这些操作后的位置。
  3. 在 Canvas 图层与屏幕合成时,超出屏幕范围的图像是不会显示出来的。
//1.平移
void translate(float dx, float dy)

//2.旋转(Rotate)
void rotate(float degrees) 
void rotate(float degrees, float px, float py)

//3.缩放(Scale)
public void scale(float sx, float sy)

//4.扭曲(Skew)
void skew(float sx, float sy)//正切值

//5.裁剪画布(clip 系列函数)
//除调用 save()、restore()函数以外,这个操作是不可逆的,一旦 Canvas 被裁剪,就不能恢复。
//要禁用硬件加速功能 setLayerType(LAYER_TYPE_SOFTWARE,null);
clipRect()
clipRegion()

1.5.2 画布的保存与恢复

  1. save():每次调用 save() 函数,都会先保存当前画布的状态,然后将其放入特定的栈中。
  2. restore():每次调用 restore()函数,都会把栈中顶层的画布状态取出来,并按照这个状态恢复当前的画布,然后在这个画布上作画。
  3. restoreToCount(int saveCount):将指定索引的画布作为当前画布。

1.6.1 控件概述

public class CustomView extends View { 
 public CustomView(Context context) { 
 super(context); 
 } 
 public CustomView(Context context, AttributeSet attrs) { //xml
 super(context, attrs); 
 } 
 public CustomView(Context context, AttributeSet attrs, int defStyle) { 
 super(context, attrs, defStyle); 
 } 
}
发布了44 篇原创文章 · 获赞 15 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_44947117/article/details/104258644