《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); 
 } 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章