Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義控制項入門:Canvas 與 Paint 繪製基礎圖形詳解

編輯:關於 Android 程式設計

前言

上一篇寫自定義 View 已經是大半年前的事了,其間一直想接著寫,但總覺得自己火候不夠,怕誤人子弟。這段時間專案剛收尾,終於有點空檔,於是跟著前輩們的思路,把自定義 View 的基礎知識重新梳理一遍。

Canvas 與 Paint

把 Canvas 和 Paint 理解成現實中的畫布與畫筆就好,這兩樣是繪圖的標配。下面分別拆解。

Paint 的常用方法

先掃一遍基礎設定:

· setColor(int color):設定畫筆顏色。
· setStrokeWidth(float width):設定畫筆寬度。
· setAntiAlias(boolean aa):設定抗鋸齒。
· setStyle(Style style):設定填充樣式。
· setShadowLayer(float radius, float dx, float dy, int shadowColor):設定陰影。

顏色和寬度不用多說。抗鋸齒值得留意,它能讓斜線與圓弧邊緣更平滑;除了 Paint 上的 setAntiAlias(),也可以從 Canvas 這一端打開:

canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));

setStyle()

這個方法有三種取值:STROKE(只描邊)、FILL(只填充內部)、FILL_AND_STROKE(兩者都要)。新建一個專案,在 onDraw 裡各畫一個圓來對比:

protected void onDraw(Canvas canvas) {
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(mLeftX, mLeftY, 100, mPaint);

    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(mLeftX * 4, mLeftY, 100, mPaint);

    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    canvas.drawCircle(mLeftX * 7, mLeftY, 100, mPaint);
}

執行結果:

這時你會發現:FILL 和 FILL_AND_STROKE 看起來沒什麼區別。原因很簡單——預設的畫筆寬度是 1,填充和描邊幾乎重合。把寬度調大就看出差別了:

mPaint.setStrokeWidth(50);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(mLeftX, mLeftY, 50, mPaint);

mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawCircle(mLeftX * 4, mLeftY, 50, mPaint);

執行結果:

顯而易見,FILL_AND_STROKE 會把那圈寬度也一起塗滿。

setShadowLayer()

四個參數的含義:radius 是陰影的模糊半徑,dx 是水平位移,dy 是垂直位移,shadowColor 自然是陰影顏色。給文字加一層陰影試試:

mPaint.setTextSize(50);
mPaint.setColor(Color.BLUE);
mPaint.setShadowLayer(5, 10, 10, Color.GREEN);
canvas.drawText("Airsaid", mLeftX, mLeftY, mPaint);

執行結果:

Canvas 的常用方法

Canvas 作為畫布,提供了各類圖形的繪製函數。下面按圖形分類整理。

繪製背景

drawColor(int)、drawRGB(int r, int g, int b)、drawARGB(int a, int r, int g, int b) 三者效果相同,只是入參形式不同,取值範圍都是 0 到 255。

繪製一條直線

drawLine(float startX, float startY, float stopX, float stopY, Paint paint),起點與終點座標加上畫筆。

mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(10);
canvas.drawLine(0, 0, getWidth(), 0, mPaint);

繪製多條直線

drawLines(float[] pts, Paint paint):pts 是座標集合,每四個一組畫一條線。

mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(10);
float pts[] = {0, 10, getWidth(), 10, 0, 50, getWidth(), 50};
canvas.drawLines(pts, mPaint);

drawLines(float[] pts, int offset, int count, Paint paint):多了 offset 與 count,可以跳過前面若干個資料,只畫其中一段。下面的例子跳過第一條線:

float pts[] = {0, 10, getWidth(), 10, 0, 50, getWidth(), 50};
canvas.drawLines(pts, 4, 4, mPaint);

繪製單點

drawPoint(float x, float y, Paint paint):一個點其實就是一段被畫筆寬度撐開的極短線段,所以寬度決定點的大小。

mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(20);
canvas.drawPoint(100, 100, mPaint);

繪製多點

drawPoints(float[] pts, Paint paint):每兩個一組畫一個點。

float pts[] = {100, 100, 200, 100, 300, 100};
canvas.drawPoints(pts, mPaint);

同樣有帶 offset 與 count 的重載版本,可以跳過前面幾個點:

canvas.drawPoints(pts, 2, 4, mPaint); // 跳過第一個點,只畫後兩個

繪製矩形

三個重載:傳入 Rect、傳入 RectF、直接給四邊座標。

mPaint.setColor(Color.RED);

Rect rect = new Rect(100, 30, 200, 100);
canvas.drawRect(rect, mPaint);

RectF rectF = new RectF(300, 30, 400, 100);
canvas.drawRect(rectF, mPaint);

canvas.drawRect(500, 30, 600, 100, mPaint);

Rect 與 RectF 是矩形輔助類,用兩個對角點描述一塊矩形區域。兩者的關鍵差異在於精度:Rect 用 int,RectF 用 float。

繪製圓角矩形

drawRoundRect(RectF rect, float rx, float ry, Paint paint),rx 與 ry 分別是 x 軸與 y 軸方向的圓角橢圓半徑。另有直接傳四邊座標的重載,但那是 API 21 才加入的,使用前要做版本判斷。

mPaint.setColor(Color.RED);
RectF rectF = new RectF(0, 0, 300, 100);
canvas.drawRoundRect(rectF, 30f, 30f, mPaint);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    canvas.drawRoundRect(400f, 0f, 700f, 100f, 30f, 30f, mPaint);
}

繪製圓形

drawCircle(float cx, float cy, float radius, Paint paint),圓心座標加半徑。

mPaint.setColor(Color.RED);
canvas.drawCircle(100f, 50f, 50f, mPaint);

繪製橢圓

drawOval(RectF oval, Paint paint):橢圓由傳入的矩形決定,矩形的長是橢圓的 x 軸,寬是 y 軸。傳四邊座標的重載同樣從 API 21 開始提供。

mPaint.setColor(Color.RED);
RectF rectF = new RectF(50, 0, 200, 100);
canvas.drawOval(rectF, mPaint);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    canvas.drawOval(300, 0, 450, 100, mPaint);
}

繪製弧形

drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)。oval 決定弧所在的橢圓,startAngle 是起始角度,sweepAngle 是掃過的角度,useCenter 決定是否連同兩條半徑一起連到圓心(也就是要不要形成扇形)。

mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);

RectF rectF = new RectF(50, 0, 200, 100);
canvas.drawArc(rectF, 0f, 180f, false, mPaint);   // 不含圓心,就是半條弧線

RectF rectF2 = new RectF(250, 0, 400, 100);
canvas.drawArc(rectF2, 0f, 180f, true, mPaint);   // 含圓心,描邊呈扇形輪廓

RectF rectF3 = new RectF(450, 0, 600, 100);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(rectF3, 0f, 180f, true, mPaint);   // 含圓心並填充,就是半圓

小結

把上面這些圖形繪製方法串起來看,其實規律很清楚:所有繪製 API 都是「座標/區域描述 + 一支 Paint」的組合,差別只在於你怎麼描述形狀。真正需要反覆琢磨的反而是 Paint——抗鋸齒、樣式、寬度、陰影這幾個開關的組合,決定了一個自定義控制項的視覺質感。

把這些基礎打牢,再往 Path、二維變換、離屏繪製這些進階主題走,會順暢很多。

參考資料

Android 官方開發者文件:https://developer.android.com/

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved