# android使用 2D 方法实现倒影特效

private void TintThePicture(int deg) {

int[] pix = new int[picw * pich];

mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;

double angle = (3.14159d * (double)deg) / 180.0d;

int S = (int)(256.0d * Math.sin(angle));

int C = (int)(256.0d * Math.cos(angle));

for (int y = 0; y < pich; y++)

for (int x = 0; x < picw; x++)

{

int index = y * picw + x;

int r = (pix[index] >> 16) & 0xff;

int g = (pix[index] >> 8) & 0xff;

int b = pix[index] & 0xff;

RY = ( 70 * r – 59 * g – 11 * b) / 100;

GY = (-30 * r + 41 * g – 11 * b) / 100;

BY = (-30 * r – 59 * g + 89 * b) / 100;

Y = ( 30 * r + 59 * g + 11 * b) / 100;

RYY = (S * BY + C * RY) / 256;

BYY = (C * BY – S * RY) / 256;

GYY = (-51 * RYY – 19 * BYY) / 100;

R = Y + RYY;

R = (R < 0) ? 0 : ((R > 255) ? 255 : R);

G = Y + GYY;

G = (G < 0) ? 0 : ((G > 255) ? 255 : G);

B = Y + BYY;

B = (B < 0) ? 0 : ((B > 255) ? 255 : B);

pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;

}

Bitmap bm = Bitmap.createBitmap(picw, pich, false);

bm.setPixels(pix, 0, picw, 0, 0, picw, pich);

// Put the updated bitmap into the main view

mIV.setImageBitmap(bm);

mIV.invalidate();

mBitmap = bm;

pix = null;

}

1.倒影是上、下翻转的图像；

2.从上到下透明度越来越大。

Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

for (int y = 0; y < h; y++)

{

bm.setPixels(srcPix, y * w, w, 0, h - y - 1, w, 1);

}

int alpha = 0x00000000;

mBitmap.getPixels (pix, 0, w, 0, 0, w, h);

for (int y = 0; y<h; y++) {

for (int x=0; x<w; x++) {

int index = y * w + x;

int r = (pix[index] >> 16) & 0xff;

int g = (pix[index] >> 8) & 0xff;

int b = pix[index] & 0xff;

pix[index] = alpha | (r<<16) | (g<<8) | b;

}

alpha = alpha + 0x01000000;

}

Top