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

動手實作心電圖動畫:Android 自定義 View 完整教學

編輯:關於 Android 程式設計

大家好,最近在學校待得太久,感覺自己都快發霉長出蘑菇了……

學而不思則罔,思而不學則殆。

老話說得沒錯,很多奇怪的困惑其實都源於「想太多、讀太少」。在知識儲備還不夠的時候埋頭苦想,往往只是白費力氣,所以革命尚未成功,同志仍需努力。

先來看看最終要實現的效果:

拿到目標圖,先做個簡單的拆解。整個畫面可以分成兩塊:一塊是背景網格,另一塊是跳動的心電圖折線。

一、繪製背景網格

思路很直接:先把畫布填成黑色,再用循環把橫線與豎線一條條畫出來即可。

二、繪製心電圖折線

觀察波形不難看出,用 Path 就能輕鬆模擬,於是便放手大幹一場。

按這兩個步驟畫完後會發現:圖是畫上去了,可關鍵是——怎麼讓它動起來?第一個想到的自然是 scrollTo / scrollBy。然而一試就發現問題:一滾動,背景網格也跟著跑偏了。

遇到問題就得解決。這裡我用了個取巧的辦法:把一個 View 拆成兩個——背景網格一個 View,折線波形另一個 View,各自獨立滾動,互不干擾。

首先建立背景 View。由於折線 View 需要複用一批相同的屬性,索性把這些成員都改成 protected,方便子類繼承使用。

public class CardiographView extends View {
    // 畫筆
    protected Paint mPaint;
    // 折線顏色
    protected int mLineColor = Color.parseColor("#76f112");
    // 網格顏色
    protected int mGridColor = Color.parseColor("#1b4200");
    // 小網格顏色
    protected int mSGridColor = Color.parseColor("#092100");
    // 背景顏色
    protected int mBackgroundColor = Color.BLACK;
    // 自身大小
    protected int mWidth, mHeight;
    // 網格寬度
    protected int mGridWidth = 50;
    // 小網格寬度
    protected int mSGridWidth = 10;
    // 心電圖折線
    protected Path mPath;
}

屬性定義好後,在構造方法中初始化畫筆與 Path:

public CardiographView(Context context) {
    this(context, null);
}

public CardiographView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CardiographView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mPaint = new Paint();
    mPath = new Path();
}

接著在 onSizeChanged 中拿到自身寬高。為了簡化示例,這裡不做測量處理:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mWidth = w;
    mHeight = h;
    super.onSizeChanged(w, h, oldw, oldh);
}

準備工作完成,開始繪製背景。新建 drawBackground 方法,用 for 循環畫出橫豎線:橫線起點 x 為 0、終點 x 為 mWidth,y 為 i 乘以網格寬度;網格數量則由寬高除以網格寬度得出。

private void drawBackground(Canvas canvas) {
    canvas.drawColor(mBackgroundColor);
    // 小網格豎線數量
    int vSNum = mWidth / mSGridWidth;
    // 小網格橫線數量
    int hSNum = mHeight / mSGridWidth;
    mPaint.setColor(mSGridColor);
    mPaint.setStrokeWidth(2);
    // 畫豎線
    for (int i = 0; i < vSNum + 1; i++) {
        canvas.drawLine(i * mSGridWidth, 0, i * mSGridWidth, mHeight, mPaint);
    }
    // 畫橫線
    for (int i = 0; i < hSNum + 1; i++) {
        canvas.drawLine(0, i * mSGridWidth, mWidth, i * mSGridWidth, mPaint);
    }
}

此時運行效果已經像模像樣了:

接著把 Path 加上去。新建一個 PathView 並放到佈局的底部:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.wingsofts.cardiograph.CardiographView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.wingsofts.cardiograph.PathView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

為求省事,讓 PathView 直接繼承 CardiographView,只需重寫 onDraw 即可,其餘屬性全部沿用。

private void drawPath(Canvas canvas) {
    // 重置 Path
    mPath.reset();
    // 用 Path 模擬一段心電圖波形
    mPath.moveTo(0, mHeight / 2);
    int tmp = 0;
    for (int i = 0; i < 10; i++) {
        mPath.lineTo(tmp + 20, 100);
        mPath.lineTo(tmp + 70, mHeight / 2 + 50);
        mPath.lineTo(tmp + 80, mHeight / 2);
        mPath.lineTo(tmp + 200, mHeight / 2);
        tmp = tmp + 200;
    }
    // 設置畫筆樣式
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(mLineColor);
    mPaint.setStrokeWidth(5);
    canvas.drawPath(mPath, mPaint);
}

波形畫出來後如下:

最後就是讓它動起來。這裡用到 scrollBy,順帶一提,scrollBy 與 scrollTo 的區別是面試常考點:scrollTo 是滑動到絕對坐標,scrollBy 是在當前基礎上相對滑動。每次滑動後再 postInvalidateDelayed 觸發重繪即可。

@Override
protected void onDraw(Canvas canvas) {
    drawPath(canvas);
    scrollBy(1, 0);
    postInvalidateDelayed(10);
}

大功告成!此時效果就與最開始的目標圖一致了:

當然,這只是一個簡單的 Demo,你可以根據自己的需求,在任意坐標繪製不同的波形,還原更真實的心電監護效果。

如果這篇文章對你有幫助,別忘了點個關注哦~

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