Android原生繪圖工具Canvas詳細
如果對上一篇感興趣的話可以看看Android原生繪圖Paint
下面步入正題:先看看效果圖
1.Canvas提供的繪制函數(shù)
canvas.drawColor(); canvas.drawRGB(); canvas.drawRect(); canvas.drawRoundRect(); canvas.drawCircle(); canvas.drawPath(); canvas.drawLine(); canvas.drawArc(); canvas.drawOval(); canvas.drawPoint(); canvas.drawPoints(); canvas.drawText(); canvas.drawTextOnPath(); canvas.drawBitmap();
2.繪制背景
用于初始化和清空畫布
//繪制顏色,默認模式 public void drawColor(@ColorInt int color) { super.drawColor(color); } //顏色繪制,設置mode public void drawColor(@ColorInt int color, @NonNull PorterDuff.Mode mode) { super.drawColor(color, mode); } //參數(shù)0-255 public void drawARGB(int a, int r, int g, int b) { super.drawARGB(a, r, g, b); } //參數(shù)0-255 public void drawRGB(int r, int g, int b) { super.drawRGB(r, g, b); }
第二個函數(shù)中用到PorterDuff.Mode
,PorterDuff.Mode
主要用于圖像混合模式,后面細說
3.繪制矩形drawRect
//傳入RectF public void drawRect(@NonNull RectF rect, @NonNull Paint paint) { super.drawRect(rect, paint); } //傳入Rect public void drawRect(@NonNull Rect r, @NonNull Paint paint) { super.drawRect(r, paint); } //把Rect的四個點坐標傳入 public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) { super.drawRect(left, top, right, bottom, paint); }
Rect
和RectF
都是提供一個矩形局域。
(1)精度不一樣,Rect是使用int類型作為數(shù)值,RectF
是使用float
類型作為數(shù)值。
(2)兩個類型提供的方法也不是完全一致。
Rect rect = new Rect(100,100,300,300); RectF rectf = new RectF(100,400,300,600); canvas.drawRect(rect, mPaint); canvas.drawRect(rectf, mPaint); canvas.drawRect(100, 700, 300, 900, mPaint);
4.繪制圓角矩形drawRoundRect
public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) { super.drawRoundRect(rect, rx, ry, paint); } //不利用RectF,直接設置四個點 public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, @NonNull Paint paint) { super.drawRoundRect(left, top, right, bottom, rx, ry, paint); }
rect
:RectF對象,一個矩形區(qū)域。
rx
:x方向上的圓角半徑。
ry
:y方向上的圓角半徑。
paint
:繪制時所使用的畫筆。
Rect rect = new Rect(100,100,300,300); RectF rectf = new RectF(100,400,300,600); canvas.drawRect(rect, mPaint); canvas.drawRoundRect(rectf, 5, 20, mPaint); canvas.drawRoundRect(100, 700, 300, 900, 20, 5, mPaint);
5.繪制圓形drawCircle
public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) { super.drawCircle(cx, cy, radius, paint); }
參數(shù)解釋:
cx
: 圓心x
cy
: 圓心y
radius
:半徑
canvas.drawCircle(500, 300, 100, mPaint);
6.繪制路徑drawPath
public void drawPath(@NonNull Path path, @NonNull Paint paint) { super.drawPath(path, paint); }
需要一個Path
Path path = new Path(); path.moveTo(100, 100); path.lineTo(100, 200); path.lineTo(200, 300); mPaint2.setStrokeJoin(Paint.Join.MITER); canvas.drawPath(path, mPaint2);
7.繪制直線drawLine
//提供起點,終點和畫筆 public void drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint) { super.drawLine(startX, startY, stopX, stopY, paint); } public void drawLines(@Size(multiple = 4) @NonNull float[] pts, int offset, int count, @NonNull Paint paint) { super.drawLines(pts, offset, count, paint); } public void drawLines(@Size(multiple = 4) @NonNull float[] pts, @NonNull Paint paint) { super.drawLines(pts, paint); }
繪制線的集合,參數(shù)中pts
是點的集合,兩個值代表一個點,四個值代表一條線,互相之間不連接。
offset
跳過的點,count
跳過之后要繪制的點的總數(shù),可以用于集合中部分點的繪制。
canvas.drawLine(500, 500, 500, 300, mPaint); float[] pos = {20, 30, 40 , 100, 120, 160, 200, 290}; canvas.drawLines(pos, mPaint);
8.繪制圓弧drawArc
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint) { super.drawArc(oval, startAngle, sweepAngle, useCenter, paint); } public void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint) { super.drawArc(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint); }
RectF oval
:生成弧的矩形,中心為弧的圓心
float startAngle
:弧開始的角度,以X軸正方向為0度,順時針
float sweepAngle
:弧持續(xù)的角度
boolean useCenter
:是否有弧的兩邊,True
,還兩邊,False
,只有一條弧
RectF rectF = new RectF(100, 100, 400, 400); canvas.drawArc(rectF, 0, 270, false, mPaint2); rectF = new RectF(500, 500, 800, 800); canvas.drawArc(rectF, 0, 270, true, mPaint2);
9.繪制橢圓drawOval
public void drawOval(@NonNull RectF oval, @NonNull Paint paint) { super.drawOval(oval, paint); } public void drawOval(float left, float top, float right, float bottom, @NonNull Paint paint) { super.drawOval(left, top, right, bottom, paint); }
在矩形框內(nèi)畫一個橢圓,如果是個正方形會畫出一個圓。
RectF rectF = new RectF(100, 100, 400, 400); canvas.drawOval(rectF, mPaint2); rectF = new RectF(500, 500, 900, 800); canvas.drawOval(rectF, mPaint2);
10.繪制點drawPoint
public void drawPoint(float x, float y, @NonNull Paint paint) { super.drawPoint(x, y, paint); } public void drawPoints(@Size(multiple = 2) float[] pts, int offset, int count, @NonNull Paint paint) { super.drawPoints(pts, offset, count, paint); } public void drawPoints(@Size(multiple = 2) @NonNull float[] pts, @NonNull Paint paint) { super.drawPoints(pts, paint); }
只需要提供兩個點一個坐標就可以繪制點。
canvas.drawPoint(100, 100, mPaint2); float[] points = {30, 40, 50, 60, 70, 80}; canvas.drawPoints(points, mPaint2);
11.繪制文本drawText 沿路徑繪制文本drawTextOnPath
public void drawText(@NonNull char[] text, int index, int count, float x, float y, @NonNull Paint paint) { super.drawText(text, index, count, x, y, paint); } public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { super.drawText(text, x, y, paint); } public void drawText(@NonNull String text, int start, int end, float x, float y, @NonNull Paint paint) { super.drawText(text, start, end, x, y, paint); } public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, @NonNull Paint paint) { super.drawText(text, start, end, x, y, paint); }
canvas.drawText("MatumbaMan的博客",100,100,mTextPaint);
Path path = new Path(); path.addArc(new RectF(100, 100, 600, 600), 0, 260); canvas.drawTextOnPath("MatumbaMan的博客", path, 10, 20, mTextPaint); canvas.drawText("MatumbaMan的博客",100,100, mTextPaint);
drawTextOnPath
沿著一條 Path
來繪制文字
text
為所需要繪制的文字
path
為文字的路徑
hOffset
文字相對于路徑的水平偏移量,用于調(diào)整文字的位置
vOffset
文字相對于路徑豎直偏移量,用于調(diào)整文字的位置
12.繪制bitmap drawBitmap
public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint) { super.drawBitmap(bitmap, src, dst, paint); } public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) { super.drawBitmap(bitmap, src, dst, paint); }
Rect src
:指定繪制圖片的區(qū)域
Rect dst
或RectF dst
:指定圖片在屏幕上的繪制(顯示)區(qū)域
首先指定圖片區(qū)域,然后指定繪制圖片的區(qū)域。
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket2); canvas.drawBitmap(bitmap, 100, 100, mPaint); Rect src = new Rect(0, 0, 100,100); Rect dst = new Rect(200, 500, 300, 600); canvas.drawBitmap(bitmap, src, dst, mPaint);
到此這篇關于Android
原生繪圖工具Canvas
詳細的文章就介紹到這了,更多相關Android
原生繪圖工具Canvas
內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android開發(fā)人臉識別統(tǒng)計人臉數(shù)
這篇文章主要介紹了Android開發(fā)人臉識別統(tǒng)計人臉數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10Android Flutter圖片處理之高斯模糊的實現(xiàn)
這篇文章主要為大家詳細介紹了如何利用Android Flutter實現(xiàn)高斯模糊效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08Android開發(fā)時盡管已root但是ddms還是沒有data路徑怎么辦
這篇文章主要介紹了Android開發(fā)時盡管已root但是ddms還是沒有data路徑怎么辦的相關資料,需要的朋友可以參考下2015-12-12Android 給應用程序的icon添加未讀消息個數(shù)提示(紅圈內(nèi)數(shù)字)
本文主要介紹了Android 給應用程序的icon添加未讀消息個數(shù)提示(紅圈內(nèi)數(shù)字)的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android鍵盤輸入語言設置默認打開myanmar緬甸語的步驟
如何實現(xiàn)Android鍵盤輸入語言默認打開為myanmar緬甸語,如果要設置某種語言在輸入法默認打開可按一下步驟添加文件,我這里已經(jīng)驗證時OK的2013-06-06