編輯:關於 Android 程式設計
在 Android 中對圖片做特效處理是很實用的技巧。本文整理三種常見效果的實作:黑白化、圓角與高斯模糊。
將彩色圖轉為黑白圖的核心,是把每個像素的 RGB 依權重換算成灰階值:
/**
* 將彩色圖轉換為黑白圖
* @return 返回轉換好的位圖
*/
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth(); // 獲取位圖的寬
int height = bmp.getHeight(); // 獲取位圖的高
int[] pixels = new int[width * height]; // 建立像素點陣列
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
return newBmp;
}

利用 Canvas 搭配 PorterDuff 的 Xfermode 就能裁出圓角:
/**
* 轉換成圓角
* @param roundPx 圓角半徑
*/
public static Bitmap convertToRoundedCorner(Bitmap bmp, float roundPx) {
Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(newBmp);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// 第二、三個參數相同則畫正圓一角,否則為橢圓一角
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bmp, rect, rect, paint);
return newBmp;
}

透過 3×3 的高斯矩陣對周圍像素加權平均,就能模擬模糊效果:
/**
* 高斯模糊
*/
public static Bitmap convertToBlur(Bitmap bmp) {
// 高斯矩陣
int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
int pixR = 0, pixG = 0, pixB = 0;
int pixColor = 0;
int newR = 0, newG = 0, newB = 0;
int delta = 16; // 值越小圖片越亮,越大則越暗
int idx = 0;
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
idx = 0;
for (int m = -1; m <= 1; m++) {
for (int n = -1; n <= 1; n++) {
pixColor = pixels[(i + m) * width + k + n];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = newR + pixR * gauss[idx];
newG = newG + pixG * gauss[idx];
newB = newB + pixB * gauss[idx];
idx++;
}
}
newR /= delta;
newG /= delta;
newB /= delta;
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
return newBmp;
}
三種特效各有千秋:黑白化適合懷舊風格,圓角能讓介面更柔和,高斯模糊則常用於背景虛化或毛玻璃效果,可依實際需求靈活選用。
Android 隨談(22):那些「叼到飛起」的 Android Studio 快捷鍵
這裡開源一個自己寫的 Demo,希望能給 Androider 們有所幫助,水平有限,敬請諒解。https://github.com/zhiaixinyang/MyFir
Android WebView 基本用法重點整理
前言 WebView(網路視圖)在 Android 中就是用來顯示網頁的元件。下面我們一起來看看它該怎麼使用。 一、基本使用 1. 宣告權限。WebView 勢必會用到網
手機QQ設備鎖怎麼設置 讓帳號更安全
想預防QQ被盜、讓帳號多一層防護?那就來試試設備鎖。小編下面帶來設置教學,此功能需手機QQ 4.62或以上版本。 開啟設備鎖後,只有在通過認證的手機、平板或電腦上才能登入
Android 翻頁效果實作:上下與左右 Flip 視圖
想要在 Android 中做出類似 Flipboard 的翻頁效果,可以借助開源元件 android-flip。它能讓頁面以立體翻轉的方式呈現,適合電子書、雜誌或圖片瀏覽