相当的明了,也没有占据多余的内存。呵呵,主要就是*创建Mutable Image,然后修正Alpha, drawRGB到Mutable Image里面,其他就不多说了,有想用不明白的朋友就尽管问好了。。。

基本用法很简单:
// 构造函数第二个参数是表示是否循环渐现渐隐, false不循环,true循环。
// 构造函数第三个参数(可选)是Alpha的变化因子,正数是渐现, 负数是渐隐,推荐值(5到20, 或者-20到-5)
FadingImage fimg = new FadingImage("picture.png", false);
......
// 在循环里
  fimg.fading();
  fimg.draw(g);

 代码:

/*
 * Copyright (c) 2007       Gimware Studio
 * Author:
 *     Liang   (该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。)
 *
 * Class of fading Image
 */
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;

class FadingImage {
    private Image fadingImage;
    private int[] fadingRGB;
    private Graphics fadingGraphics;
    private boolean needLoop; // need loop of not
    private int lowAlpha;
    private int highAlpha;
    private int nowAlpha; // current alpha
    private int incAlpha; // increment of fading

    private void createFadingImage(String name, boolean loop, int lAlpha,
            int hAlpha, int now, int inc) {
        Image rawImage = null;

        try {
            rawImage = Image.createImage(name);
        } catch (Exception e) {
            System.out.println("Can't create rawImage");
        }

        fadingRGB = new int[rawImage.getWidth() * rawImage.getHeight()];
        rawImage.getRGB(fadingRGB, 0, rawImage.getWidth(), 0, 0, rawImage
                .getWidth(), rawImage.getHeight());

        // create mutable image
        fadingImage = Image.createImage(rawImage.getWidth(), rawImage
                .getHeight());
        fadingGraphics = fadingImage.getGraphics();

        rawImage = null; // Useless now, gc it
        System.gc();

        needLoop = loop;
        lowAlpha = lAlpha;
        highAlpha = hAlpha;
        nowAlpha = now;
        incAlpha = inc;
    }

    public FadingImage(String name, boolean loop) {
        createFadingImage(name, loop, 0, 255, 0, 20);
    }

    public FadingImage(String name, boolean loop, int inc) {
        if (inc > 0) // increment
            createFadingImage(name, loop, 0, 255, 0, inc);
        else
            // decrement
            createFadingImage(name, loop, 0, 255, 255, inc);
    }

    public void fading() {
        nowAlpha += incAlpha;

        if (nowAlpha >= highAlpha) {
            nowAlpha = highAlpha;
            if (needLoop) {
                // reverse fading
                incAlpha = -1 * incAlpha;
            } else {
                // don't need fade anymore
                return;
            }
        } else if (nowAlpha <= lowAlpha) {
            nowAlpha = lowAlpha;
            if (needLoop) {
                // reverse fading
                incAlpha = -1 * incAlpha;
            } else {
                // don't need fade anymore
                return;
            }
        }
        // fade RGB
        blend(nowAlpha);
        fadingGraphics.setColor(0xFFFFFF);
        fadingGraphics.fillRect(0, 0, fadingImage.getWidth(), fadingImage
                .getHeight());

        // update the mutable image
        fadingGraphics.drawRGB(fadingRGB, 0, fadingImage.getWidth(), 0, 0,
                fadingImage.getWidth(), fadingImage.getHeight(), true);
    }

    public void draw(Graphics g) {
        g.drawImage(fadingImage, 0, 0, 0);
    }

    private void blend(int alphaValue, int maskColor, int dontmaskColor) {
        int len = fadingRGB.length;

        // Start loop through all the pixels in the image.
        for (int i = 0; i < len; i++) {
            int a = 0;
            int color = (fadingRGB & 0x00FFFFFF); // get the RGB of the pixel.

            if (maskColor == color) {
                a = 0;
            } else if (dontmaskColor == color) {
                a = 255;
            } else if (alphaValue > 0) {
                a = alphaValue; // set the alpha value
            }

            a = (a << 24); // left shift the alpha value 24 bits.

            color += a;
            fadingRGB = color; // update the value of the pixel
        }
    }

    private void blend(int alphaValue) {
        blend(alphaValue, 0xFFFFFFFF, 0xFFFFFFFF);
    }
}