編輯:關於 Android 程式設計
有些介面的配色看起來令人心情舒暢,有些則讓人忍不住皺眉。Android L 的 Palette 能從圖片中萃取並篩選出協調的顏色,效果往往相當討喜。本文就帶你看看 Palette 調色板背後到底做了哪些事。
讀完原始碼後,我把整個流程整理成以下幾個關鍵步驟:
縮圖能有效降低運算量與記憶體佔用,而且對最終取色結果幾乎沒有影響,可說是CP值極高的一招。
private static Bitmap scaleBitmapDown(Bitmap bitmap) { final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight()); if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) { return bitmap; } final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension; return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio), Math.round(bitmap.getHeight() * scaleRatio), false); }
縮圖完成後,接著把所有像素資料讀取到一個 int 陣列裡,方便後續統計。
static ColorCutQuantizer fromBitmap(Bitmap bitmap, int maxColors) { final int width = bitmap.getWidth(); final int height = bitmap.getHeight(); final int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); return new ColorCutQuantizer(new ColorHistogram(pixels), maxColors); }
把 int 陣列由小到大排序,等於把相同顏色集中在一起,接著就能統計出「一共有幾種顏色、每種顏色各佔多少」。這部分由 ColorHistogram(顏色直方圖)負責,用直方圖的語言來說,就是有多少根柱子、每根柱子有多高。
final class ColorHistogram { private final int[] mColors; private final int[] mColorCounts; private final int mNumberColors; ColorHistogram(final int[] pixels) { Arrays.sort(pixels); mNumberColors = countDistinctColors(pixels); mColors = new int[mNumberColors]; mColorCounts = new int[mNumberColors]; countFrequencies(pixels); } int getNumberOfColors() { return mNumberColors; } int[] getColors() { return mColors; } int[] getColorCounts() { return mColorCounts; } private static int countDistinctColors(final int[] pixels) { if (pixels.length < 2) { return pixels.length; } int colorCount = 1; int currentColor = pixels[0]; for (int i = 1; i < pixels.length; i++) { if (pixels[i] != currentColor) { currentColor = pixels[i]; colorCount++; } } return colorCount; } private void countFrequencies(final int[] pixels) { if (pixels.length == 0) { return; } int currentColorIndex = 0; int currentColor = pixels[0]; mColors[currentColorIndex] = currentColor; mColorCounts[currentColorIndex] = 1; if (pixels.length == 1) { return; } for (int i = 1; i < pixels.length; i++) { if (pixels[i] == currentColor) { mColorCounts[currentColorIndex]++; } else { currentColor = pixels[i]; currentColorIndex++; mColors[currentColorIndex] = currentColor; mColorCounts[currentColorIndex] = 1; } } } }
接著把每個顏色從 RGB 轉成 HSL(H 為色相、S 為飽和度、L 為明度),再依條件過濾。例如明度太接近純白或純黑,或是落在 isNearRedILine(接近紅色I線區域)的顏色,都會被排除。關於 HSL 與轉換公式,可參考相關百科的詳細說明。
private ColorCutQuantizer(ColorHistogram colorHistogram, int maxColors) { final int rawColorCount = colorHistogram.getNumberOfColors(); final int[] rawColors = colorHistogram.getColors(); final int[] rawColorCounts = colorHistogram.getColorCounts(); mColorPopulations = new SparseIntArray(rawColorCount); for (int i = 0; i < rawColors.length; i++) { mColorPopulations.append(rawColors[i], rawColorCounts[i]); } mColors = new int[rawColorCount]; int validColorCount = 0; for (int color : rawColors) { if (!shouldIgnoreColor(color)) { mColors[validColorCount++] = color; } } if (validColorCount <= maxColors) { mQuantizedColors = new ArrayList(); for (final int color : mColors) { mQuantizedColors.add(new Swatch(color, mColorPopulations.get(color))); } } else { mQuantizedColors = quantizePixels(validColorCount - 1, maxColors); } }
最後,針對不同情境(例如「有活力的暗色」、「有活力的亮色」、「柔和的顏色」、「柔和的暗色」、「柔和的亮色」),在對應的明度與飽和度區間裡找出最合適的顏色。
private Swatch findColor(float targetLuma, float minLuma, float maxLuma, float targetSaturation, float minSaturation, float maxSaturation) { Swatch max = null; float maxValue = 0f; for (Swatch swatch : mSwatches) { final float sat = swatch.getHsl()[1]; final float luma = swatch.getHsl()[2]; if (sat >= minSaturation && sat <= maxSaturation && luma >= minLuma && luma <= maxLuma && !isAlreadySelected(swatch)) { float thisValue = createComparisonValue(sat, targetSaturation, luma, targetLuma, swatch.getPopulation(), mHighestPopulation); if (max == null || thisValue > maxValue) { max = swatch; maxValue = thisValue; } } } return max; }
來看看實際效果:

Web App 與原生 App 的差異與選擇指南
Web App 與原生 App 的差異與選擇指南 許多團隊在規劃行動產品時,都會遇到一個關鍵問題:該使用 HTML5 封裝的 Web App,還是直接開發原生 App?兩
RecyclerView 詳解:一套代碼實現 ListView、GridView 與瀑布流
RecyclerView 是 Google 推出的、用來取代 ListView 與 GridView 的新一代容器。它只專注於「回收複用」,而把布局交给 LayoutMa
動手寫 Android Studio 專案模板(二):一次產生多個檔案
上一篇介紹的做法只能產生單一個類別檔案,沒辦法同時輸出 XML 版面、資源檔與設定檔。本篇要談的,是如何設計一個能一次產生多個檔案、多種檔案類型的模板。 回想一下我們平時
Android 以 PopupWindow 實作滑動刪除(精簡版)
本文參考〈Android 仿騰訊 QQ 實現滑動刪除〉一文進行學習。該文實作的功能是:在 ListView 的 Item 上從右向左滑動時出現刪除按鈕,點擊刪除按鈕即可刪