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

Android 自定義 3D 立體圖:用 Canvas 畫出銷售漏斗

編輯:關於 Android 程式設計

先看效果:

實現思路

核心其實非常樸素:一個圓柱 = 上下兩個橢圓 + 中間一片包圍面。上下橢圓用 drawOval 畫,中間部分用 Path 把左右兩側的切點連起來閉合成一個區域再 drawPath 填充,立體感就出來了。

要做出「漏斗」的層層遞進效果,只需要讓每一層圓柱的左右邊界依次向內收攏,並逐層向下偏移一段距離即可。

關鍵代碼

先定義需要用到的成員變量:

public class CylinderView extends View {

    Context context;
    Paint paint;
    Path path;

    // 橢圓內縮量、圓柱內縮量、兩圓柱的垂直間距
    int sub_line = 2, sub_cicle = 24, distance = 120;

    int width, height;   // 屏幕寬高

    // 構成圓柱的上下兩個橢圓的位置坐標
    float left, right, top, bottom, left2, right2, top2, bottom2;
    float start, middle, temp_top, temp_left;
    float c_height1, c_height2, c_height3, c_height4, c_height5, c_height6;

    RectF rel;   // 橢圓

    ArrayList planList = new ArrayList<>();   // 計劃值
    ArrayList logList = new ArrayList<>();    // 實際值
    ArrayList titleList = new ArrayList<>();  // 標題

    public CylinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void renewCylinderView(ArrayList planList, ArrayList logList,
                                  ArrayList titleList) {
        this.planList = planList;
        this.logList = logList;
        this.titleList = titleList;
        invalidate();
    }
}

onDraw 中先準備畫筆,再按數據個數分派繪製(支持 4 層和 6 層兩種形態)。注意繪製順序是從最後一層往前畫,這樣靠上層的圖形才會覆蓋在下層之上,形成正確的前後遮擋關係:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    width = dm.widthPixels;
    height = dm.heightPixels;
    rel = new RectF();

    sub_line = ScreenUtil.dp2px(context, 1);
    sub_cicle = ScreenUtil.dp2px(context, 8);
    distance = ScreenUtil.dp2px(context, 40);

    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);              // 去鋸齒
    paint.setTextSize(ScreenUtil.dp2px(context, 16));
    paint.setColor(ContextCompat.getColor(context, R.color.cricle_purplebac));

    if (planList.size() == 4 || logList.size() == 4) {
        draw1stMipsBac(canvas);
        drawSecMipsBac(canvas);
        drawThrMipsBac(canvas);
        draw4stMipsBac(canvas);
        draw4stMips(canvas);
        drawThrMips(canvas);
        drawSecMips(canvas);
        draw1stMips(canvas);
    } else if (planList.size() == 6 || logList.size() == 6) {
        draw1stMipsBac(canvas);
        drawSecMipsBac(canvas);
        drawThrMipsBac(canvas);
        draw4stMipsBac(canvas);
        draw5stMipsBac(canvas);
        draw6stMipsBac(canvas);
        draw6stMips(canvas);
        draw5stMips(canvas);
        draw4stMips(canvas);
        drawThrMips(canvas);
        drawSecMips(canvas);
        draw1stMips(canvas);
    }
}

先畫背景層:一個完整尺寸的圓柱

背景圓柱的作用是給數據圓柱提供一個「容器」參照。它的高度會被記錄下來(c_height1),後續繪製數據時按完成比例縮放:

// 第1個圓柱背景
private void draw1stMipsBac(Canvas canvas) {
    left = width / 4;
    right = 3 * width / 4;
    top = height / 24;
    bottom = top + (right - left) / 5;

    left2 = left + sub_line;
    right2 = right - sub_line;
    top2 = top + (right - left) / 8;
    bottom2 = top2 + (right - left) / 6;

    c_height1 = bottom - top;
    start = bottom - (bottom - top) / 2;    // 下橢圓的 y 坐標
    middle = bottom2 - (bottom2 - top2) / 2; // 上橢圓的 y 坐標

    // 繪製橢圓2
    rel.set(left2, top2, right2, bottom2);
    canvas.drawOval(rel, paint);

    // 繪製中間包圍面
    Path path = new Path();
    path.moveTo(right, start);
    path.lineTo(right2, middle);
    path.lineTo(left2, middle);
    path.lineTo(left, start);
    path.close();
    canvas.drawPath(path, paint);

    // 繪製橢圓1
    rel.set(left, top, right, bottom);
    canvas.drawOval(rel, paint);
}

再畫數據層:按完成比例縮放

數據圓柱的做法是在背景圓柱的基礎上,根據「實際值 / 計劃值」把左右邊界向中間收攏:

// 第1個圓柱
private void draw1stMips(Canvas canvas) {
    paint.setColor(ContextCompat.getColor(context, R.color.common_btn_nomal));
    canvas.drawText(
            Integer.valueOf(logList.get(0).toString().replace(".0", "")) + "/"
            + Integer.valueOf(planList.get(0).toString().replace(".0", "")),
            width / 5 * 4, height / 24 + ScreenUtil.dp2px(context, 36), paint);
    canvas.drawText(titleList.get(0), width / 12,
            height / 24 + ScreenUtil.dp2px(context, 36), paint);

    float f;
    if (planList.get(0) == 0 && logList.get(0) > 0) {
        f = 1;
    } else if (planList.get(0) == 0 && logList.get(0) == 0) {
        return;
    } else {
        f = logList.get(0) / planList.get(0);
    }
    if (f > 1) {
        f = 1;   // 超額完成也按 100% 畫
    }
    f = 1 - f;   // 剩餘比例決定收攏程度

    // 左右邊界向中間收攏
    left = width / 4;
    right = 3 * width / 4;
    temp_left = left;
    left = temp_left + (right - temp_left) * f / 2;
    right = right - (right - temp_left) * f / 2;

    // 上下邊界按高度比例收攏
    top = height / 24;
    bottom = top + (right - left) / 5;
    temp_top = top;
    top = top + (c_height1) * f;
    bottom = bottom + (c_height1) * f;

    left2 = left + sub_line;
    right2 = right - sub_line;
    top2 = top + (right - left) / 8;
    bottom2 = top2 + (right - left) / 6;
    start = bottom - (bottom - top) / 2;
    middle = bottom2 - (bottom2 - top2) / 2;

    // 繪製橢圓2
    rel.set(left2, top2, right2, bottom2);
    canvas.drawOval(rel, paint);

    // 繪製中間包圍面
    Path path = new Path();
    path.moveTo(right, start);
    path.lineTo(right2, middle);
    path.lineTo(left2, middle);
    path.lineTo(left, start);
    path.close();
    canvas.drawPath(path, paint);

    // 繪製橢圓1(頂面用淺色,做出受光效果)
    rel.set(left, top, right, bottom);
    paint.setColor(ContextCompat.getColor(context, R.color.common_btn_pressed));
    canvas.drawOval(rel, paint);
}

其餘各層:同一套公式,換一組偏移量

第 2 到第 6 層的實現與第一層完全一致,只是三個參數按層級遞變:

  • 左右邊界:每往下一層,左右各多收攏一個 sub_cicle,形成漏斗輪廓。
  • 垂直位置:top 加上 distance * 層級序號,並對後幾層做少量像素微調,讓間距視覺上更均勻。
  • 顏色:頂面與底面分別取同色系的一深一淺(例如 cricle_orange1 / cricle_orange2),營造光照下的立體感。

下面以第三層為例,對比一下差異所在:

// 第3個圓柱
private void drawThrMips(Canvas canvas) {
    paint.setColor(ContextCompat.getColor(context, R.color.cricle_orange2));
    canvas.drawText(
            Integer.valueOf(logList.get(2).toString().replace(".0", "")) + "/"
            + Integer.valueOf(planList.get(2).toString().replace(".0", "")),
            width / 5 * 4, height / 24 + ScreenUtil.dp2px(context, 26) + distance * 2, paint);
    canvas.drawText(titleList.get(2), width / 12,
            height / 24 + ScreenUtil.dp2px(context, 26) + distance * 2, paint);

    float f;
    if (planList.get(2) == 0 && logList.get(2) > 0) {
        f = 1;
    } else if (planList.get(2) == 0 && logList.get(2) == 0) {
        return;
    } else {
        f = logList.get(2) / planList.get(2);
    }
    if (f > 1) f = 1;
    f = 1 - f;

    left = width / 4 + sub_cicle * 2;      // 關鍵:逐層向內收攏
    right = 3 * width / 4 - sub_cicle * 2;
    temp_left = left;
    left = temp_left + (right - temp_left) * f / 2;
    right = right - (right - temp_left) * f / 2;

    top = height / 24 + distance * 2 - ScreenUtil.dp2px(context, 2);
    bottom = top + (right - left) / 5;
    temp_top = top;
    top = top + (c_height3) * f;
    bottom = bottom + (c_height3) * f;

    left2 = left + sub_line;
    right2 = right - sub_line;
    top2 = top + (right - left) / 8;
    bottom2 = top2 + (right - left) / 6;
    start = bottom - (bottom - top) / 2;
    middle = bottom2 - (bottom2 - top2) / 2;

    rel.set(left2, top2, right2, bottom2);
    canvas.drawOval(rel, paint);

    Path path = new Path();
    path.moveTo(right, start);
    path.lineTo(right2, middle);
    path.lineTo(left2, middle);
    path.lineTo(left, start);
    path.close();
    canvas.drawPath(path, paint);

    rel.set(left, top, right, bottom);
    paint.setColor(ContextCompat.getColor(context, R.color.cricle_orange1));
    canvas.drawOval(rel, paint);
}

第 4、5、6 層(draw4stMips / draw5stMips / draw6stMips)以及對應的背景方法(drawSecMipsBac 到 draw6stMipsBac)結構完全相同,把序號、sub_cicle 倍數、distance 倍數和顏色資源替換即可。

幾點經驗

  1. 把重複代碼收斂掉。這裡六層圓柱的繪製邏輯一模一樣,實際項目中完全應該抽成一個 drawCylinder(canvas, level, colorTop, colorBottom) 方法,把層級當參數傳進去,代碼量能砍掉八成。
  2. 用 dp2px 而不是裸像素。所有尺寸間距都經過 dp 換算,才能在不同屏幕上保持一致的比例。
  3. 注意繪製順序。先背景後數據、自下向上,遮擋關係才正確。
  4. 打開硬件加速後 Path 的用法不變,但盡量避免在 onDraw 裡反覆 new 對象,Paint、Path、RectF 都可以提升為成員變量復用。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved