本例的代码取自我写的上阳图像处理演示程序(ImageAlbum),自己实现的方法,提供另一种思路。大家应该尽量使用系统内置的API
大家可以下载(包括所有源代码)看看实际效果
http://blog.csdn.net/the3gwireless/archive/2006/04/27/679608.aspx
/*
* blend方法由下边的作者所写,感谢他所做的工作
*
* By: Oscar Wivall Date: 2004-05-06
*
* Description: Create cool effects by fading in/out the images in MIDP 2.0.
* This midlet shows how to change the alpha value of an Image. This MIDlet
* is tested on Z1010 and K700.
*
* This method changes the alpha value of an image array. All the pixels in
* the image contains Alpha, Red, Green, Blue (ARGB) colors where each of
* the color is a value from 0 to 255. If Alpha is 255 the pixel will be
* opaque, if its 0 the pixel is transparent. 0xFFFF0000 = 11111111 11111111
* 00000000 00000000 - this is a red opaque pixel.
*
* To get the RGB values from the array we can use the AND '&' operator.
* (11111101 & 01111110) = 01111100, only the 1:s where & = 1 will get
* through.
*
* to change 11111111 to 11111111 00000000 00000000 00000000 we can use the
* shift left operator '<<', (00000001 << 7) = 10000000 in dec (1<<7) =
* 128
*
* To change the alpha value we loop through all the pixels in the image.
*
* With the blend method its also possible to mask and dontmask specific
* colors.
*/
// raw是图像数据
public static void blend(int[] raw, int alphaValue, int maskColor,
int dontmaskColor) {
int len = raw.length;
// 开始循环,获得图像里每个像素的颜色,然后处理
for (int i = 0; i < len; i++) {
int a = 0;
int color = (raw[i] & 0x00FFFFFF); // 获得像素的颜色
if (maskColor == color) {
a = 0;
} else if (dontmaskColor == color) {
a = 255;
} else if (alphaValue > 0) {
a = alphaValue; // 设置我们希望的alpha值 0-255.
}
a = (a << 24); // 把alpha左移24位(left shift the alpha value 24 bits)
// if color = 00000000 11111111 11111111 00000000
// and alpha= 01111111 00000000 00000000 00000000
// then c+a = 01111111 11111111 11111111 00000000
// and the pixel will be blended.
color += a;
raw[i] = color;
}
}
public void run() {
// System.out.println("Run IN");
int stopIndex = 0;
while (mTrucking) {
if (mUpdate) {
// 每循环一次改变一次alpha值 255=不透明, 0=透明
if (mAlpha >= 255) {
stopIndex++;
// System.out.println(stopIndex);
changeAlphaOff = changeAlphaOff * -1;
} else if (mAlpha <= 0) {
changeAlphaOff = changeAlphaOff * -1;
}
mAlpha += changeAlphaOff;
// 使用ImageEffect里的blend方法来改变alphi值并且生成一个新的图象
ImageEffect.blend(rawInt, mAlpha);
image = Image.createRGBImage(rawInt, this.image.getWidth(),
this.image.getHeight(), true);
repaint();
if (stopIndex == 2) {
// System.out.println(this.mAlpha);
// System.out.println(this.changeAlphaOff);
this.mAlpha = 255;
this.changeAlphaOff = 15;
break;
}
// mUpdate = false;
}
}
ImageEffect.blend(rawInt, mAlpha);
image = Image.createRGBImage(rawInt, this.image.getWidth(), this.image
.getHeight(), true);
repaint();
// System.out.println("Run OUT");
}