程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 一個漸隱/漸現顯示圖片的Java類

一個漸隱/漸現顯示圖片的Java類

編輯:J2ME

相當的明了,也沒有盤踞過剩的內存。呵呵,重要就是*創立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 protected])
 *
 * 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);
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved