編輯:關於 Android 程式設計
專案裡需要一個 BMI 指標刻度條,先上效果圖:

刻度覆蓋 18 到 35 這個區間,指針區塊的顏色會隨數值偏移而連續變化,上方數字顯示當前 BMI,下方的分級文字則依數值落點對應「偏瘦/標準/超重/肥胖」。由於這個控制項的行為和 appearance 高度耦合,最後選擇用自定義 View 一次搞定。
<LinearLayout android:layout_width="fill_parent" android:layout_height="100dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="50dp" android:background="@color/white" android:orientation="horizontal" > <TextView android:layout_marginTop="@dimen/login_hei" android:text="@string/bmi_text" android:textColor="@color/gray" android:textSize="@dimen/login_edit_border_margin" /> <com.jxj.jwotchhelper.view.NewBmiView android:id="@+id/bmiview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
左邊是文字標籤,右邊放自定義 View。
新建 NewBmiView 並繼承 View,補齊三個構造方法,統一委派給初始化方法:
public class NewBmiView extends View { /** 分段顏色 */ private static final int[] SECTION_COLORS = { Color.rgb(255, 204, 47), Color.GREEN, Color.RED }; /** 畫筆 */ private Paint mPaint; private Paint textPaint; private Paint drawablePaint; private Paint drawableBMIPaint; private Paint bmiTextpaint; private int bmiwidth, mWidth, mHeight, widthSum; private double value; private double i; private double bmi; private float valueWidth; private String bmiText; // 定義計算顏色的參數 private int x, y, z; public NewBmiView(Context context) { super(context); initviews(context); } public NewBmiView(Context context, AttributeSet attrs) { super(context, attrs); initviews(context); } public NewBmiView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initviews(context); } private void initviews(Context context) { }
接著重寫 onMeasure 與 onDraw。onMeasure 的作用是拿到 View 的實際寬高,關於測量流程可以參考鴻洋的相關說明:https://www.jb51.net/article/86061.htm
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) { widthSum = widthSpecSize; } else { widthSum = 0; } if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) { mHeight = dipToPx(15); } else { mHeight = heightSpecSize; } setMeasuredDimension(widthSum, mHeight); }
重點在 onDraw:
// 畫自定義的漸變條 mPaint = new Paint(); // 去除鋸齒 mPaint.setAntiAlias(true); // 自定義圓角的弧度 int round = mHeight / 20; // 新建矩形 RectF rectBg = new RectF(bmiwidth, mHeight - (mHeight * 1 / 2), mWidth + bmiwidth, mHeight - (mHeight * 2 / 5)); // 設置漸變色 // CLAMP重復最後一個顏色至最後 // MIRROR重復著色的圖像水平或垂直方向已鏡像方式填充會有翻轉效果 // REPEAT重復著色的圖像水平或垂直方向 LinearGradient shader = new LinearGradient(bmiwidth, mHeight - (mHeight * 1 / 2), mWidth + bmiwidth, mHeight - (mHeight * 2 / 5), SECTION_COLORS, null, Shader.TileMode.MIRROR); mPaint.setShader(shader); canvas.drawRoundRect(rectBg, round, round, mPaint); // 畫下面的小箭頭 drawablePaint = new Paint(); drawablePaint.setAntiAlias(true); Bitmap arrowBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_up); canvas.drawBitmap(arrowBitmap, mWidth * 2 / 17 + bmiwidth, mHeight - (mHeight * 2 / 5) + 5, drawablePaint); canvas.drawBitmap(arrowBitmap, mWidth * 7 / 17 + bmiwidth, mHeight - (mHeight * 2 / 5) + 5, drawablePaint); canvas.drawBitmap(arrowBitmap, mWidth * 12 / 17 + bmiwidth, mHeight - (mHeight * 2 / 5) + 5, drawablePaint); // 畫下方的文字 String text = "偏瘦"; Rect textBounds = new Rect(); textPaint = new Paint(); textPaint.setAntiAlias(true); textPaint.setColor(Color.GRAY); textPaint.setTextSize(30); // 獲取字體的高寬 textPaint.getTextBounds(text, 0, text.length(), textBounds); float textWidth = textBounds.width(); float textHeight = textBounds.height(); canvas.drawText("偏瘦", (mWidth * 2 / 17) / 2 - textWidth / 2 + bmiwidth, mHeight * 7 / 10 + textHeight / 2 + 10, textPaint); canvas.drawText("標准", (mWidth * 2 / 17) + (mWidth * 5 / 17) / 2 - textWidth / 2 + bmiwidth, mHeight * 7 / 10 + textHeight / 2 + 10, textPaint); canvas.drawText("超重", (mWidth * 7 / 17) + (mWidth * 5 / 17) / 2 - textWidth / 2 + bmiwidth, mHeight * 7 / 10 + textHeight / 2 + 10, textPaint); canvas.drawText("肥胖", (mWidth * 12 / 17) + (mWidth * 5 / 17) / 2 - textWidth / 2 + bmiwidth, mHeight * 7 / 10 + textHeight / 2 + 10, textPaint); // 畫上方偏移的小方塊 drawableBMIPaint = new Paint(); drawableBMIPaint.setAntiAlias(true); // 通過BMI來RGB計算顏色 i = (value - 18) * (34 / 17); if (i >= 0 && i <= 17) { x = (int) ((17 - i) * (255 / 17)); y = 204; z = 47; } if (i > 17 && i <= 34) { x = (int) ((i - 17) * (255 / 17)); y = (int) ((34 - i) * (255 / 17)); z = 0; } drawableBMIPaint.setColor(Color.rgb(x, y, z)); canvas.drawRect(getvalue(), mHeight / 6, getvalue() + bmiBitmap.getWidth(), bmiBitmap.getHeight()+mHeight / 6, drawableBMIPaint); canvas.drawBitmap(bmiBitmap, getvalue(), mHeight / 6, drawablePaint); // 畫上方偏移的小方塊裡面的文字 String bmitext = "40.0"; Rect bmitextBounds = new Rect(); bmiTextpaint = new Paint(); bmiTextpaint.setAntiAlias(true); bmiTextpaint.setTextSize(35); bmiTextpaint.setColor(Color.WHITE); // 獲取字體的高寬 bmiTextpaint.getTextBounds(bmitext, 0, bmitext.length(), bmitextBounds); canvas.drawText(bmiText, getvalue() - (bmitextBounds.width() / 2) + bmiwidth, mHeight / 3 + (bmitextBounds.height() / 3), bmiTextpaint);
這裡有兩個細節值得留意。第一是指針方塊的顏色:我沒有在系統的漸變 API 裡找到能直接取出對應色值的入口,於是根據 BMI 數值自己推算 RGB 三原色的過渡值,實作起來簡單,代價是與設計稿可能存在一些誤差。第二是文字繪製:TextView 自帶寬高計算,改用 canvas 直接畫字時必須先量出文字實際佔用的尺寸,再據此做居中偏移,否則位置一定會歪。
對外的資料入口透過 setter 提供:
public void setBmi(double bmi) { this.value = bmi; // 設置顏色 if (value < 18) { this.value = 18; } else if (value > 35) { this.value = 35; } invalidate(); } public void setBmiText(String bmiText) { this.bmiText = bmiText; }
順帶在這裡做了區間夾制,超出 18~35 的極端數值不會讓指針跑出刻度外。
bmiview= (NewBmiView) getView().findViewById(R.id.bmiview); //將BMI指數傳遞過去 bmiview.setBmi(35); bmiview.setBmiText("35.0");
這樣就達到了預期效果。整體代碼寫得比較趕,結構上還有整理空間,但思路是通的。
深入解析 Android 四大組件之 Activity
一、Activity 是什麼 Activity 是 Android 應用中負責用戶交互的核心組件,可以把它理解為應用程序的「界面單元」。每一個 Activity 都會創建
Android 線程池學習總結:四類內建線程池與自定義參數
什麼是線程池 線程池會緩存一定數量的線程,透過複用避免頻繁建立與銷毀線程帶來的系統開銷。Android 的線程池體系源自 Java,核心是 Executor 介面,真正的
Android 自定義垂直多色 SeekBar 實作
需求背景 某些場景需要垂直方向的多色進度條,例如溫度或亮度調整。系統自帶 SeekBar 不一定符合設計,因此可自訂一個垂直彩色 SeekBar。 繪製思路 自訂 Vi
Android Activity 啟動模式之 standard 實例詳解
本文通過實例講解 Android Activity 的默認啟動模式 standard,供感興趣的讀者參考。 Android 的活動由任務(Task)管理,一個任務就是放置