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

Android 2D 繪圖入門:用 Canvas 與 Paint 手繪太極圖

編輯:關於 Android 開發

Android 2D 繪圖入門:用 Canvas 與 Paint 手繪太極圖

Android 呈現 2D 圖形主要靠 graphics 這組類別,裡面有 Canvas、Paint、Color、Bitmap 等角色,能畫點、畫線、填色、繪製幾何圖形,也能做圖像處理。其中 Color 與 Bitmap 大家都熟,真正該花時間搞懂的是 Canvas(畫布)與 Paint(畫筆)。下面就用一個太極圖當練習題,把繪圖機制走一遍。

Paint:先調好你的畫筆

就像實體作畫要先挑筆,Android 繪圖第一步是設定畫筆屬性。常用的 API 有這些:

setColor();        // 顏色
setAntiAlias();    // 抗鋸齒
setARGB();         // A、R、G、B 四個通道
setAlpha();        // 透明度
setTextSize();     // 字級
setStyle();        // 空心或實心
setStrokeWidth();  // 空心邊框寬度
getColor();        // 讀取目前顏色

Canvas:決定要畫什麼

Canvas 是畫布,負責決定「畫什麼」,Paint 則決定「用什麼樣的筆觸去畫」。兩者分工不同,混在一起想就會暈。常用的繪製方法:

canvas.drawLine(startX, startY, stopX, stopY, paint);       // 直線
canvas.drawRect(left, top, right, bottom, paint);            // 矩形
canvas.drawCircle(cx, cy, radius, paint);                    // 圓
canvas.drawArc(rect, startAngle, sweepAngle, useCenter, paint); // 弧線/扇形
canvas.drawText(text, x, y, paint);                          // 文字
canvas.drawBitmap(bitmap, left, top, paint);                 // 點陣圖

分層來畫太極圖

太極圖看起來複雜,其實可以借「圖層」的概念拆成三層:最外層一個大圓(左黑右白)、中間兩個上下相切的半圓、最上方兩個小圓。

先設定座標:圓心 x = getWidth() / 2、y = getHeight() / 2,半徑 r = getHeight() / 2。

第一層:最外側大圓

關鍵是先用 RectF 算出圓的外接矩形,再用 drawArc 畫半圓。四個參數分別是左邊 x、頂端 y、右邊 x、底端 y。

// 繪製最外層大圓
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
RectF rect = new RectF(getWidth() / 2 - getHeight() / 2, 0,
                       getWidth() / 2 + getHeight() / 2, getHeight());
canvas.drawArc(rect, 270, 180, false, mPaint);
mPaint.setColor(Color.WHITE);
canvas.drawArc(rect, 90, 180, false, mPaint);

drawArc 的參數依序是:外接矩形、起始角度(以 x 軸為 0 度、順時針遞增)、掃過的角度、以及畫筆。

第二層:中間兩個半圓

做法一樣,只是把外接矩形的尺寸縮小一半、位置往上/下移半個高度:

// 中間層上半圓
mPaint.setColor(Color.BLACK);
rect = new RectF(getWidth() / 2 - getHeight() / 4, 0,
                 getWidth() / 2 + getHeight() / 4, getHeight() / 2);
canvas.drawArc(rect, 90, 180, false, mPaint);

// 中間層下半圓
mPaint.setColor(Color.WHITE);
rect = new RectF(getWidth() / 2 - getHeight() / 4, getHeight() / 2,
                 getWidth() / 2 + getHeight() / 4, getHeight());
canvas.drawArc(rect, 270, 180, false, mPaint);

第三層:兩個小圓

// 上方白圓
mPaint.setColor(Color.WHITE);
canvas.drawCircle(getWidth() / 2, getHeight() / 4, getHeight() / 10, mPaint);

// 下方黑圓
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(getWidth() / 2, getHeight() * 3 / 4, getHeight() / 10, mPaint);

drawCircle 的四個參數分別是圓心 x、圓心 y、半徑、畫筆。三層疊完,太極圖就成形了。

完整的自訂 View

public class CustomeView extends View {
    private Paint mPaint = new Paint();

    public CustomeView(Context context) { super(context); }
    public CustomeView(Context context, AttributeSet attrs) { super(context, attrs); }
    public CustomeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        RectF rect = new RectF(getWidth() / 2 - getHeight() / 2, 0,
                               getWidth() / 2 + getHeight() / 2, getHeight());
        canvas.drawArc(rect, 270, 180, false, mPaint);
        mPaint.setColor(Color.WHITE);
        canvas.drawArc(rect, 90, 180, false, mPaint);

        mPaint.setColor(Color.BLACK);
        rect = new RectF(getWidth() / 2 - getHeight() / 4, 0,
                         getWidth() / 2 + getHeight() / 4, getHeight() / 2);
        canvas.drawArc(rect, 90, 180, false, mPaint);
        mPaint.setColor(Color.WHITE);
        rect = new RectF(getWidth() / 2 - getHeight() / 4, getHeight() / 2,
                         getWidth() / 2 + getHeight() / 4, getHeight());
        canvas.drawArc(rect, 270, 180, false, mPaint);

        mPaint.setColor(Color.WHITE);
        canvas.drawCircle(getWidth() / 2, getHeight() / 4, getHeight() / 10, mPaint);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth() / 2, getHeight() * 3 / 4, getHeight() / 10, mPaint);
    }
}

在版面檔裡直接放這個自訂 View:

<com.chuck.mobile.changecountview.widget.CustomeView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@color/gray"/>

想讓它轉起來,可以用屬性動畫,也可以開一條執行緒不斷旋轉 Canvas。我當初是用後者。把 Canvas 與 Paint 玩熟之後,任何圖案都能自己畫出來。

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