編輯:關於 Android 開發
Android 呈現 2D 圖形主要靠 graphics 這組類別,裡面有 Canvas、Paint、Color、Bitmap 等角色,能畫點、畫線、填色、繪製幾何圖形,也能做圖像處理。其中 Color 與 Bitmap 大家都熟,真正該花時間搞懂的是 Canvas(畫布)與 Paint(畫筆)。下面就用一個太極圖當練習題,把繪圖機制走一遍。
就像實體作畫要先挑筆,Android 繪圖第一步是設定畫筆屬性。常用的 API 有這些:
setColor(); // 顏色 setAntiAlias(); // 抗鋸齒 setARGB(); // A、R、G、B 四個通道 setAlpha(); // 透明度 setTextSize(); // 字級 setStyle(); // 空心或實心 setStrokeWidth(); // 空心邊框寬度 getColor(); // 讀取目前顏色
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、半徑、畫筆。三層疊完,太極圖就成形了。
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 玩熟之後,任何圖案都能自己畫出來。
MSM8909 與 Android 5.1.1 鍵盤驅動分析:I2C、Input 子系統與掃描碼
概述 本文以 MSM8909 平台與 Android 5.1.1 為例,分析鍵盤驅動的基本實現方式。系統採用 SN7326 鍵盤擴展晶片,透過 I2C 介面讀取狀態暫存器
我的 Android 進階之旅:常用特殊字符(箭頭、音符等)的 Unicode 碼值速查
在開發中,我們常常需要在 Button、TextView 等控件裡顯示一些特殊符號,如下圖所示:這些符號固然可以用圖片來顯示,但其實直接使用 Unicode 碼就能呈現,
Button 上的英文字為什麼自動變大寫?textAllCaps 陷阱與解法
Button 上的英文字為什麼自動變大寫?textAllCaps 陷阱與解法 最近碰到一個小但惱人的問題:在 Android 5.1 上,Button 裡的英文字串會自動
Android 屬性動畫實戰:用 ValueAnimator 與 ObjectAnimator 做出跳跳球
在動手做效果之前,先把屬性動畫的幾個核心概念理清楚,後面的程式碼才不會只是「抄來能用」。 一、從兩種動畫到第三種 Android 3.0 之前,系統提供兩種動畫機制: