1.畫(huà)筆 Paint
-
paint.setAntiAlias(true);//抗鋸齒 -
paint.setColor(Color.RED);//畫(huà)筆顏色 -
paint.setStyle(Style.FILL);//填充樣式-
Paint.Style.FILL//填充內(nèi)部 -
Paint.Style.FILL_AND_STROKE//填充內(nèi)部和描邊 -
Paint.Style.STROKE//僅描邊
-
-
paint.setStrokeWidth(3);//畫(huà)筆寬度 -
paint.setShadowLayer(8, 16, 16, Color.GRAY);//陰影-
setShadowLayer (float radius, float dx, float dy, int color)添加陰影
radius陰影的傾斜度dx水平位移dy垂直位移
-
2.畫(huà)布 Canvas
-
canvas.drawColor(Color.BLUE);//畫(huà)布背景顏色 -
canvas.drawRGB(255, 255, 0);//畫(huà)布背景顏色
用畫(huà)布畫(huà)各種幾何圖形https://blog.csdn.net/harvic880925/article/details/38875149
void drawCircle(float cx, float cy, float radius, @NonNull Paint paint)【畫(huà)圓】
cx圓心點(diǎn)X軸坐標(biāo)cy圓心點(diǎn)Y軸坐標(biāo)radius圓的半徑void drawLine (float startX, float startY, float stopX, float stopY, Paint paint)【畫(huà)直線】
設(shè)置paint.setStrokeCap(Paint.Cap.ROUND);:畫(huà)筆為圓頭,【畫(huà)圓角直線】void drawLines (float[] pts, Paint paint)
void drawLines (float[] pts, int offset, int count, Paint paint)【畫(huà)多條直線】
pts:是點(diǎn)的集合,四個(gè)點(diǎn)形成一條線,前兩個(gè)點(diǎn)為畫(huà)直線起始位置的x,y,后兩個(gè)點(diǎn)為結(jié)束位置的x,y,所以pts應(yīng)該是個(gè)數(shù)應(yīng)該是四的倍增數(shù)量。void drawPoint (float x, float y, Paint paint)【畫(huà)點(diǎn)】void drawPoints (float[] pts, Paint paint)
void drawPoints (float[] pts, int offset, int count, Paint paint)【畫(huà)多點(diǎn)】
pts:是點(diǎn)的集合,二的倍增數(shù)量,offset:集合中跳過(guò)的數(shù)值個(gè)數(shù),注意不是點(diǎn)的個(gè)數(shù)!一個(gè)點(diǎn)是兩個(gè)數(shù)值;count:參與繪制的數(shù)值的個(gè)數(shù),指pts[]里人數(shù)值個(gè)數(shù),而不是點(diǎn)的個(gè)數(shù),因?yàn)橐粋€(gè)點(diǎn)是兩個(gè)數(shù)值void drawRect (float left, float top, float right, float bottom, Paint paint)
void drawRect (RectF rect, Paint paint)
void drawRect (Rect r, Paint paint)【畫(huà)矩形】
//RectF
RectF()
RectF(float left, float top, float right, float bottom)
RectF(RectF r)
RectF(Rect r)
//Rect
Rect()
Rect(int left, int top, int right, int bottom)
Rect(Rect r)
-
void drawRoundRect (RectF rect, float rx, float ry, Paint paint)【畫(huà)圓角矩形】
rx:生成圓角的橢圓的X軸半徑ry:生成圓角的橢圓的Y軸半徑 -
void drawOval (RectF oval, Paint paint)【畫(huà)橢圓】 -
void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)【畫(huà)弧】
oval:生成橢圓的矩形
startAngle:弧開(kāi)始的角度,以X軸正方向?yàn)?度
sweepAngle:弧持續(xù)的角度
useCenter是否有弧的兩邊,true,有兩邊,false,只有一條弧
設(shè)置paint.setStrokeCap(Paint.Cap.ROUND);:畫(huà)筆為圓頭,【畫(huà)圓角弧度】
3.路徑Path
用畫(huà)布畫(huà)路徑及文字https://blog.csdn.net/harvic880925/article/details/38926877
-
void drawPath (Path path, Paint paint)【畫(huà)直線路徑】
Path 方法:
1.直線路徑
-
void moveTo (float x1, float y1):直線的開(kāi)始點(diǎn);即將直線路徑的繪制點(diǎn)定在(x1,y1)的位置; -
void lineTo (float x2, float y2):直線的結(jié)束點(diǎn),又是下一次繪制直線路徑的開(kāi)始點(diǎn);lineTo可以一直用; -
void close ():如果連續(xù)畫(huà)了幾條直線,但沒(méi)有形成閉環(huán),調(diào)用Close()會(huì)將路徑首尾點(diǎn)連接起來(lái),形成閉環(huán),實(shí)驗(yàn)后不調(diào)用也可以閉環(huán)
2.矩形路徑
-
void addRect (float left, float top, float right, float bottom, Path.Direction dir)
void addRect (RectF rect, Path.Direction dir)
Path.Direction.CCW是counter-clockwise縮寫(xiě),指創(chuàng)建逆時(shí)針方向的矩形路徑;
Path.Direction.CW是clockwise的縮寫(xiě),指創(chuàng)建順時(shí)針方向的矩形路徑;
3.圓角矩形路徑
-
void addRoundRect (RectF rect, float[] radii, Path.Direction dir)
可以定制每個(gè)角的圓角大小,radii必須傳入8個(gè)數(shù)值,分四組 -
void addRoundRect (RectF rect, float rx, float ry, Path.Direction dir)
只能構(gòu)建統(tǒng)一圓角大小
4.圓形路徑
void addCircle (float x, float y, float radius, Path.Direction dir)
x:圓心X軸坐標(biāo)
y:圓心Y軸坐標(biāo)
radius:圓半徑
5.橢圓路徑
void addOval (RectF oval, Path.Direction dir)
6.弧形路徑
-
void addArc (RectF oval, float startAngle, float sweepAngle)
oval:弧是橢圓的一部分,這個(gè)參數(shù)就是生成橢圓所對(duì)應(yīng)的矩形;
startAngle:開(kāi)始的角度,X軸正方向?yàn)?度
sweepAngel:持續(xù)的度數(shù);
7.線段軌跡
-
void quadTo (float x1, float y1, float x2, float y2)
x1二次曲線上控制點(diǎn)的X坐標(biāo)
y1二次曲線上控制點(diǎn)的Y坐標(biāo)
x2二次曲線上終點(diǎn)的X坐標(biāo)
y2二次曲線上終點(diǎn)的Y坐標(biāo)
和lineTo很像,但是它對(duì)不是折線的過(guò)渡更加平滑,類似于心電圖和波紋圖的區(qū)別,可以理解為賽貝爾曲線。x1y1相當(dāng)于一個(gè)牽引力的著力點(diǎn)
紅點(diǎn)為控制點(diǎn)
4.文字 Paint+Path+Canvas
畫(huà)筆
Paint的設(shè)置
1.普通設(shè)置
paint.setStrokeWidth (5);//設(shè)置畫(huà)筆寬度
paint.setAntiAlias(true); //指定是否使用抗鋸齒功能,如果使用,會(huì)使繪圖速度變慢
paint.setStyle(Paint.Style.FILL);//繪圖樣式,對(duì)于設(shè)文字和幾何圖形都有效
paint.setTextAlign(Align.CENTER);//設(shè)置文字對(duì)齊方式,取值:align.CENTER、align.LEFT或align.RIGHT
paint.setTextSize(12);//設(shè)置文字大小
2.樣式設(shè)置
paint.setFakeBoldText(true);//粗體
paint.setUnderlineText(true);//下劃線
paint.setTextSkewX((float) -0.25);//字體水平傾斜度,普通斜體字是-0.25
paint.setStrikeThruText(true);//刪除線
paint.setTextScaleX(2);//只會(huì)將水平方向拉伸,高度不會(huì)變
paint.setStrokeCap(Paint.Cap.BUTT)//劃過(guò)的線條和路徑
畫(huà)布
Canvas的繪圖方式
1.普通水平繪制
void drawText (String text, float x, float y, Paint paint)//常規(guī)繪制
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)//截取數(shù)組文字繪制
void drawText (CharSequence text, int start, int end, float x, float y, Paint paint)//多媒體文字繪制,但這里的text并不支持復(fù)雜的多媒體文字繪制,只是支持這種類型而已,所以Span創(chuàng)建的特效是沒(méi)有用的。
2.指定單個(gè)文字位置繪制,由于不支持glyph的合成【已過(guò)時(shí)】
-
void drawPosText (char[] text, int index, int count, float[] pos, Paint paint)
text要繪制的文字?jǐn)?shù)組
index第一個(gè)要繪制的文字的索引
count要繪制的文字的個(gè)數(shù),用來(lái)算最后一個(gè)文字的位置,從第一個(gè)繪制的文字開(kāi)始算起
pos每個(gè)字體的位置,同樣兩兩一組,如{x1,y1,x2,y2,x3,y3……} void drawPosText (String text, float[] pos, Paint paint)
3.沿路徑繪制
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 與路徑起始點(diǎn)的高度偏移距離,為負(fù)會(huì)導(dǎo)致起始文字重疊
vOffset 與路徑中心的垂直偏移量

字體樣式
Typeface在Paint中設(shè)置字體樣式:paint.setTypeface(typeface);
1.創(chuàng)建Typeface
- 使用系統(tǒng)中的字體
Typeface create(String familyName, int style)//直接通過(guò)指定字體名來(lái)加載系統(tǒng)中自帶的文字樣式
Typeface create(Typeface family, int style)//通過(guò)其它Typeface變量來(lái)構(gòu)建文字樣式 - 自字義字體
Typeface createFromAsset(AssetManager mgr, String path)//通過(guò)從Asset中獲取外部字體來(lái)顯示字體樣式
Typeface createFromFile(String path)//直接從路徑創(chuàng)建
Typeface createFromFile(File path)//從外部路徑來(lái)創(chuàng)建字體樣式
Typeface defaultFromStyle(int style)//創(chuàng)建默認(rèn)字體 -
style的枚舉值如下:
Typeface.NORMAL//正常體
Typeface.BOLD//粗體
Typeface.ITALIC//斜體
Typeface.BOLD_ITALIC//粗斜體
5.區(qū)域 Range
參考:https://blog.csdn.net/harvic880925/article/details/39056701
1、基本構(gòu)造函數(shù)
public Region() //創(chuàng)建一個(gè)空的區(qū)域 ,需要配合其它函數(shù)使用
public Region(Region region) //復(fù)制一個(gè)region的區(qū)域
public Region(Rect r) //創(chuàng)建一個(gè)矩形的區(qū)域 ,通過(guò)矩形
public Region(int left, int top, int right, int bottom) //創(chuàng)建一個(gè)矩形的區(qū)域,通過(guò)左上右下
-
public Region()創(chuàng)建一個(gè)空的區(qū)域后,通過(guò)下面的操作方法構(gòu)建一個(gè)區(qū)域
public void setEmpty()//置空:從某種意義上講置空也是一個(gè)構(gòu)造函數(shù),即將原來(lái)的一個(gè)區(qū)域變量變成了一個(gè)空變量,要再利用其它的Set方法重新構(gòu)造區(qū)域
public boolean set(Region region)//利用新的區(qū)域值來(lái)替換原來(lái)的區(qū)域
public boolean set(Rect r)//利用矩形所代表的區(qū)域替換原來(lái)的區(qū)域
public boolean set(int left, int top, int right, int bottom)//根據(jù)左上右下四點(diǎn)構(gòu)造出矩形區(qū)域來(lái)替換原來(lái)的區(qū)域值
public boolean setPath(Path path, Region clip)//根據(jù)路徑的區(qū)域與某區(qū)域的交集,構(gòu)造出新區(qū)域
2、矩形集枚舉區(qū)域——RegionIterator類
由于在Canvas中沒(méi)有直接繪制Region的函數(shù),我們想要繪制一個(gè)區(qū)域,就只能通過(guò)利用RegionIterator構(gòu)造矩形集來(lái)逼近的顯示區(qū)域。用法如下
private void drawRegion(Canvas canvas, Region region, Paint paint) {
RegionIterator iter = new RegionIterator(region);
Rect rect = new Rect();
while (iter.next(rect)) canvas.drawRect(rect, paint);
//或者其他可以用Rect來(lái)繪制的方法:
//while (iter.next(rect)) canvas.drawOval(new RectF(rect), paint);
}

3、區(qū)域的合并、交叉等操作
public final boolean union(Rect r)
public boolean op(Rect r, Op op)
public boolean op(int left, int top, int right, int bottom, Op op)
public boolean op(Region region, Op op)
public boolean op(Rect rect, Region region, Op op)
-
Op的參數(shù)
DIFFERENCE(0)//最終區(qū)域?yàn)閞egion1 與 region2不同的區(qū)域
INTERSECT(1)// 最終區(qū)域?yàn)閞egion1 與 region2相交的區(qū)域
UNION(2)//最終區(qū)域?yàn)閞egion1 與 region2組合一起的區(qū)域
XOR(3)//最終區(qū)域?yàn)閞egion1 與 region2相交之外的區(qū)域
REVERSE_DIFFERENCE(4)//最終區(qū)域?yàn)閞egion2 與 region1不同的區(qū)域
REPLACE(5)//最終區(qū)域?yàn)闉閞egion2的區(qū)域
例如兩個(gè)矩形取交集:
//初始化畫(huà)筆
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
//構(gòu)造兩個(gè)矩形
Rect rect1 = new Rect(100,100,400,200);
Rect rect2 = new Rect(200,0,300,300);
//構(gòu)造兩個(gè)Region
Region region = new Region(rect1);
Region region2= new Region(rect2);
//取兩個(gè)區(qū)域的交集
region.op(region2, Region.Op.INTERSECT);
//繪制兩個(gè)空的矩形
canvas.drawRect(rect1, paint);
canvas.drawRect(rect2, paint);
//交集用綠色繪制
Paint paint_fill = new Paint();
paint_fill.setColor(Color.MAGENTA);
paint_fill.setStyle(Paint.Style.FILL);
drawRegion(canvas, region, paint_fill);



4、其它一些方法
/**幾個(gè)判斷方法*/
public native boolean isEmpty();//判斷該區(qū)域是否為空
public native boolean isRect(); //是否是一個(gè)矩陣
public native boolean isComplex();//是否是多個(gè)矩陣組合
/**一系列的getBound方法,返回一個(gè)Region的邊界*/
public Rect getBounds()
public boolean getBounds(Rect r)
public Path getBoundaryPath()
public boolean getBoundaryPath(Path path)
/**一系列的判斷是否包含某點(diǎn) 和是否相交*/
public native boolean contains(int x, int y);//是否包含某點(diǎn)
public boolean quickContains(Rect r) //是否包含某矩形
public native boolean quickContains(int left, int top, int right,
int bottom) //是否沒(méi)有包含某矩陣形
public boolean quickReject(Rect r) //是否沒(méi)和該矩形相交
public native boolean quickReject(int left, int top, int right, int bottom); //是否沒(méi)和該矩形相交
public native boolean quickReject(Region rgn); //是否沒(méi)和該矩形相交
/**幾個(gè)平移變換的方法*/
public void translate(int dx, int dy)
public native void translate(int dx, int dy, Region dst);
public void scale(float scale) //hide
public native void scale(float scale, Region dst);//hide /**幾個(gè)判斷方法*/
public native boolean isEmpty();//判斷該區(qū)域是否為空
public native boolean isRect(); //是否是一個(gè)矩陣
public native boolean isComplex();//是否是多個(gè)矩陣組合
/**一系列的getBound方法,返回一個(gè)Region的邊界*/
public Rect getBounds()
public boolean getBounds(Rect r)
public Path getBoundaryPath()
public boolean getBoundaryPath(Path path)
/**一系列的判斷是否包含某點(diǎn) 和是否相交*/
public native boolean contains(int x, int y);//是否包含某點(diǎn)
public boolean quickContains(Rect r) //是否包含某矩形
public native boolean quickContains(int left, int top, int right,
int bottom) //是否沒(méi)有包含某矩陣形
public boolean quickReject(Rect r) //是否沒(méi)和該矩形相交
public native boolean quickReject(int left, int top, int right, int bottom); //是否沒(méi)和該矩形相交
public native boolean quickReject(Region rgn); //是否沒(méi)和該矩形相交
/**幾個(gè)平移變換的方法*/
public void translate(int dx, int dy)
public native void translate(int dx, int dy, Region dst);
public void scale(float scale) //hide
public native void scale(float scale, Region dst);//hide

