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

自訂 View 實作:自由定製的優惠券邊緣背景

編輯:關於 Android 程式設計

1、功能介紹

現在的購物類 App 真是多不勝數,經常能在裡面看到優惠券的身影。今天我們就來動手實作一個優惠券的背景效果。實務上若想偷懶,直接用一張背景圖當底就完工了,但我們選擇手動繪製,其實並不難。效果如下:

邊緣樣式可以自由定製,有兩種類型:半圓形與三角形。圖中第一張左右為三角形、上下為半圓形;第二張左右為半圓形、上下為三角形;第三張四邊都是半圓形。這些都是透過 XML 佈局中的自訂屬性指定的,也可以設為 none 表示不繪製邊緣,預設就是 none。

2、實現思路

要做出這個自訂 View,先想清楚三個問題:

(1)繼承 View 還是 ViewGroup?因為要在裡面放 TextView、ImageView 等子控制項,若繼承 View 就裝不了子元素,所以得選 ViewGroup。但直接繼承 ViewGroup 又得自己實作 onLayout,太麻煩,因此為了省事直接繼承 LinearLayout,不必操心 onMeasure 與 onLayout,只需專注於繪製邊緣這件核心業務。

(2)邊緣怎麼處理?觀察效果圖,只要在控制項邊緣做文章即可。以半圓形為例,就是在邊上循環繪製半圓。關鍵是算出這一條邊能放幾個半圓——每個人設定的長度不同,於是我們固定圓半徑與半圓間距,用這兩個參數就能算出數量:

// 計算垂直方向需要畫圓或三角形的數量與初始偏移量
private void calculateVerticalCount(float gapSize) {
    mVerticalCount = (int) ((mHeight - gapSize) / (2 * mRadius + gapSize));
    mVerticalInitSize = (int) ((mHeight - (2 * mRadius * mVerticalCount + (mVerticalCount + 1) * gapSize)) / 2);
}
// 計算水平方向的數量與初始偏移量
private void calculateHorizontalCount(float gapSize) {
    mHorizontalCount = (int) ((mWidth - gapSize) / (2 * mRadius + gapSize));
    mHorizontalInitSize = (int) ((mWidth - (2 * mRadius * mHorizontalCount + (mHorizontalCount + 1) * gapSize)) / 2);
}

畫個圖就很清楚:n 個半圓之間有 n+1 個間距,因此寬度 = n × 2 × radius + (n+1) × gapSize,由此可解出 n。由於邊長在半徑與間距下不一定能被整除,為了讓兩側留白相等,我們額外算了一個初始偏移量。

(3)繪制的時機?View 的繪製順序是:先畫背景、再畫自己(onDraw)、然後畫子佈局、最後畫裝飾。放在 onDraw 或 dispatchDraw 都行。我在這裡選擇 dispatchDraw,讓子 View 先全部畫完,再畫邊緣,避免邊緣被內部內容蓋住。(若在 ViewGroup 的 onDraw 中繪製,記得設背景或呼叫 setWillNotDraw(false),否則可能不會進入 onDraw。)

繪製時不斷循環、不斷更新座標即可:

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    if (vertical_style == 1)      drawVerticalCircle(canvas);   // 垂直半圓
    else if (vertical_style == 2) drawVerticalTriangle(canvas); // 垂直三角
    if (horizontal_style == 1)      drawHorizontalCircle(canvas);
    else if (horizontal_style == 2) drawHorizontalTriangle(canvas);
}

// 水平方向繪製三角形
private void drawHorizontalTriangle(Canvas canvas) {
    calculateHorizontalCount(0);
    Path path = new Path();
    float x = 0;
    for (int i = 0; i < mHorizontalCount; i++) { // 上面
        path.reset();
        x = mHorizontalInitSize + i * 2 * mRadius;
        path.moveTo(x, 0);
        x += mRadius; path.lineTo(x, mRadius);
        x += mRadius; path.lineTo(x, 0);
        path.close(); canvas.drawPath(path, mPaint);
    }
    x = 0;
    for (int i = 0; i < mHorizontalCount; i++) { // 下面
        path.reset();
        x = mHorizontalInitSize + i * 2 * mRadius;
        path.moveTo(x, mHeight);
        x += mRadius; path.lineTo(x, mHeight - mRadius);
        x += mRadius; path.lineTo(x, mHeight);
        path.close(); canvas.drawPath(path, mPaint);
    }
}

// 水平方向繪製半圓
private void drawHorizontalCircle(Canvas canvas) {
    calculateHorizontalCount(mGapSize);
    float x = mHorizontalInitSize + mGapSize + mRadius;
    for (int i = 0; i < mHorizontalCount; i++) { // 上面
        canvas.drawCircle(x, 0, mRadius, mPaint);
        x += 2 * mRadius + mGapSize;
    }
    x = mHorizontalInitSize + mGapSize + mRadius;
    for (int i = 0; i < mHorizontalCount; i++) { // 下面
        canvas.drawCircle(x, mHeight, mRadius, mPaint);
        x += 2 * mRadius + mGapSize;
    }
}

// 垂直方向繪製半圓(用畫弧呈現)
private void drawVerticalCircle(Canvas canvas) {
    calculateVerticalCount(mGapSize);
    RectF rectF = new RectF();
    for (int i = 0; i < mVerticalCount; i++) { // 左邊
        rectF.left = -mRadius;
        rectF.top = mVerticalInitSize + mGapSize * (i + 1) + i * 2 * mRadius;
        rectF.right = mRadius;
        rectF.bottom = rectF.top + 2 * mRadius;
        canvas.drawArc(rectF, -90, 180, false, mPaint);
    }
    for (int i = 0; i < mVerticalCount; i++) { // 右邊
        rectF.left = mWidth - mRadius;
        rectF.top = mVerticalInitSize + mGapSize * (i + 1) + i * 2 * mRadius;
        rectF.right = rectF.left + 2 * mRadius;
        rectF.bottom = rectF.top + 2 * mRadius;
        canvas.drawArc(rectF, 90, 180, false, mPaint);
    }
}

3、使用方法

因為繼承自 LinearLayout,在佈局中直接當作 LinearLayout 用,透過自訂屬性指定邊緣類型:

<declare-styleable name="CouponStyle">
    <attr name="vertical_style" format="enum">
        <enum name="none" value="0" />
        <enum name="circle" value="1" />
        <enum name="triangle" value="2" />
    </attr>
    <attr name="horizontal_style" format="enum">
        <enum name="none" value="0" />
        <enum name="circle" value="1" />
        <enum name="triangle" value="2" />
    </attr>
</declare-styleable>

於是就能在 XML 裡自由指定,例如:

<com.scu.lly.couponbgdemo.view.CouponBgView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="horizontal"
    android:background="#47BDBD"
    android:gravity="center_vertical"
    coupon:horizontal_style="circle"
    coupon:vertical_style="triangle">
    <!-- 內部放入 TextView、ImageView 等內容 -->
</com.scu.lly.couponbgdemo.view.CouponBgView>

是不是相當簡單?翻購物 App 時常看到加入購物車的各種動畫,下一篇就來玩玩購物車的加入動畫。

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