光暈特效在于凸顯一個光圈:
下面是效果圖

~~~
?/**?
? ? ?* 光暈效果?
? ? ?* @param bmp?
? ? ?* @param x 光暈中心點在bmp中的x坐標?
? ? ?* @param y 光暈中心點在bmp中的y坐標?
? ? ?* @param r 光暈的半徑?
? ? ?* @return?
? ? ?*/ ?
? ? public static Bitmap halo(Bitmap bmp, int x, int y, float r) ?
? ? { ?
? ? ? ? long start = System.currentTimeMillis(); ?
? ? ? ? // 高斯矩陣 ?
? ? ? ? int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 }; ?
? ? ? ? ??
? ? ? ? int width = bmp.getWidth(); ?
? ? ? ? int height = bmp.getHeight(); ?
? ? ? ? Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); ?
? ? ? ? ??
? ? ? ? int pixR = 0; ?
? ? ? ? int pixG = 0; ?
? ? ? ? int pixB = 0; ?
? ? ? ? ??
? ? ? ? int pixColor = 0; ?
? ? ? ? ??
? ? ? ? int newR = 0; ?
? ? ? ? int newG = 0; ?
? ? ? ? int newB = 0; ?
? ? ? ? ??
? ? ? ? int delta = 18; // 值越小圖片會越亮,越大則越暗 ?
? ? ? ? ??
? ? ? ? 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; ?
? ? ? ? ? ? ? ? int distance = (int) (Math.pow(k - x, 2) + Math.pow(i - y, 2)); ?
? ? ? ? ? ? ? ? // 不是中心區域的點做模糊處理 ?
? ? ? ? ? ? ? ? if (distance > r * r) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? 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 + (int) (pixR * gauss[idx]); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? newG = newG + (int) (pixG * gauss[idx]); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? newB = newB + (int) (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; ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? ??
? ? ? ? bitmap.setPixels(pixels, 0, width, 0, 0, width, height); ?
? ? ? ? long end = System.currentTimeMillis(); ?
? ? ? ? Log.d("may", "used time="+(end - start)); ?
? ? ? ? return bitmap; ?
? ? } ?
~~~
如上所說我們需要對圓做設置:1.圓心的XY。2.圓的半徑。
有了這些設定,我們就可以確定圓圈的范圍,對園內和圓外做不同的操作,這里我讓圓內保持不變,圓外改變
這樣就完成了,當然大家可以針對園內做成亮色,把離圓心近的地方做更深程度的亮色,這樣就有光線的感覺了