<span style="background-color: rgb(255, 255, 255);"><span style="color:#ffffff;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre class="reply-text mb10" style="margin-top: 0px; margin-bottom: 0px; background-color: rgb(254, 254, 242); padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 13px; line-height: 19px;"></span></span>
【原文:http://www.cnblogs.com/lipeil/archive/2012/09/21/2696519.html】
float mSize = 0.5f;
public Bitmap render(Bitmap bitmap)
{
if(bitmap == null)return null;
final int SIZE = 32768;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int ratio = width >height ? height * SIZE /width : width * SIZE/height;//這裡有額外*2^15 用於放大比率;之後的比率使用時需要右移15位,或者/2^15.
int cx = width>>1;
int cy = height>>1;
int max = cx*cx + cy*cy;
int min = (int)(max *(1-mSize));
int diff= max -min;// ===>> int diff = (int)(max * mSize);
int[] pixels = new int[width * height];
bitmap.getPixels(pixels ,0 , width , 0 , 0 , width , height);
for(int i=0 ; i<height ; i++)
{
for(int j=0 ; j<width ; j++)
{
int pixel = pixels[i*width +j];
int r = (pixel & 0x00ff0000)>>16;
int g = (pixel & 0x0000ff00)>>8;
int b = (pixel & 0x000000ff);
int dx = cx - j;
int dy = cy - i;
if(width > height)
{
dx= (dx*ratio)>>15;
}
else
{
dy = (dy * ratio)>>15;
}
int dstSq = dx*dx + dy*dy;
float v = ((float) dstSq / diff)*255;
r = (int)(r +v);
g = (int)(g +v);
b = (int)(b +v);
r = (r>255 ? 255 : (r<0? 0 : r));
g = (g>255 ? 255 : (g<0? 0 : g));
b = (b>255 ? 255 : (b<0? 0 : b));
pixels[i*width +j] = (pixel & 0xff000000) + (r<<16)+ (g<<8) +b;
}
}
return Bitmap.createBitmap(pixels ,width , height , Config.ARGB_8888);
}
在PHOTOSHOP裡,羽化就是使你選定範圍的圖邊緣達到朦朧的效果。
羽化值越大,朦朧範圍越寬,羽化值越小,朦朧範圍越窄。可根據你想留下圖的大小來調節。
演算法分析:
1、通過對rgb值增加額外的V值實現朦朧效果
2、通過控制V值的大小實現範圍控制。
3、V = 255 * 當前點Point距中點距離的平方s1 / (頂點距中點的距離平方 *mSize)s2;
4、s1 有根據 ratio 修正 dx dy值。