正文
我們知道,在MIDP1.0中,除非我們利用特定廠商的API(比如Nokia),我們是沒法對圖片的像素進行操作的,但是在MIDP2.0中,Image和Graphics的功能都大大增強了。比如,我們可以獲取Image的所有像素值,然後利用程序來修改這些像素(比如說ARGB各自的值),最後再把修改後的像素圖繪制出來。通過直接操作圖片像素,我們就獲得了一種很強大的能力,用編程的方式實現出很多有趣的效果來,而不用額外制作新圖片。比如說透明度漸變,顏色反轉等。下面就是2個例子,分別實現透明度漸變和顏色反轉的功能。
例題一: 透明度漸變效果的實現
給定一張圖片,假如我們想實現這麼一種效果:圖片由全透明狀態逐漸清晰,最後達到正常狀態。要實現這一個過程,我們首先要獲取該圖片的所有像素值,逐步讓這些像素的alpha值從0轉變到正常,每改變圖片的所有像素值一次,我們就請求刷屏一次,把最新的像素圖畫出來,這樣我們就能實現透明度漸變的效果了。代碼實現如下:
-
- import Java.io.IOException;
-
- import Javax.microedition.lcdui.Canvas;
- import Javax.microedition.lcdui.Display;
- import Javax.microedition.lcdui.Graphics;
- import Javax.microedition.lcdui.Image;
- import Javax.microedition.midlet.MIDlet;
- import Javax.microedition.midlet.MIDletStateChangeException;
-
- /**
- *
- * @author JagIE
- *
- */
- public class ShadowMIDlet extends MIDlet {
- Canvas c = new ShadowCanvas();
-
- public ShadowMIDlet() {
-
- }
-
- protected void startApp() throws MIDletStateChangeException {
- Display.getDisplay(this).setCurrent(c);
-
- }
-
- protected void pauseApp() {
- // TODO Auto-generated method stub
-
- }
-
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- // TODO Auto-generated method stub
-
- }
-
- }
-
- /**
- *
- * @author JagIE
- *
- */
- class ShadowCanvas extends Canvas implements Runnable {
- int w, h;
-
- // 原始圖片
- Image srcImage;
-
- // 原始圖片的像素數組
- int[] srcRgbImage;
-
- // 漸變圖片的像素數組
- int[] shadowRgbImage;
-
- int imgWidth, imgHeight;
-
- int count;
-
- public ShadowCanvas() {
- w = this.getWidth();
- h = this.getHeight();
- try {
- srcImage = Image.createImage("/av.png");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- imgWidth = srcImage.getWidth();
- imgHeight = srcImage.getHeight();
- // 制造原始圖片的像素數組,用一個int來代表每一個像素,按位表示方式是:0xAARRGGBB
- srcRgbImage = new int[imgWidth * imgHeight];
- // 獲取原始圖片的所有像素,參見MIDP APPI文檔
- srcImage.getRGB(srcRgbImage, 0, imgWidth, 0, 0, imgWidth, imgHeight);
-
- shadowRgbImage = new int[srcRgbImage.length];
-
- System.arraycopy(srcRgbImage, 0, shadowRgbImage, 0,
- shadowRgbImage.length);
-
- // 漸變圖片的所有像素已開始都是全透明的
- for (int i = 0; i < shadowRgbImage.length; i++) {
- shadowRgbImage[i] &= 0x00ffffff;
- }
-
- new Thread(this).start();
-
- }
-
- public void paint(Graphics g) {
- g.setColor(0, 0, 0);
- g.fillRect(0, 0, w, h);
- // 繪制漸變圖片
- g.drawRGB(shadowRgbImage, 0, imgWidth, (w - imgWidth) / 2,
- (h - imgHeight) / 2, imgWidth, imgHeight, true);
-
- g.setColor(0, 255, 0);
- g.drawString("count=" + count, w / 2, 30, Graphics.HCENTER
- | Graphics.TOP);
- }
-
- public void run() {
- while (true) {
-
- boolean changed = false;
- // 改變漸變圖片的每一個像素
- for (int i = 0; i < shadowRgbImage.length; i++) {
- // 獲取漸變圖片的某一像素的alpha值
- int alpha = (shadowRgbImage[i] & 0xff000000) >>> 24;
- // 原始圖片的對應像素的alpha值
- int oldAlpha = (srcRgbImage[i] & 0xff000000) >>> 24;
-
- if (alpha < oldAlpha) {
- // alpha值++
- shadowRgbImage[i] = ((alpha + 1) << 24)
- | (shadowRgbImage[i] & 0x00ffffff);
- changed = true;
- }
- }
-
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- count++;
- repaint();
- // 當所有像素的alpha值都達到原始值後,線程運行結束
- if (!changed) {
- System.out.println("over");
- break;
- }
- }
-
- }
-
- }
透明度漸變效果如下:
例題二:顏色反轉
在手機游戲中,我們經常會碰上這樣一種情況,比如我方飛機和敵方飛機外觀是完全一樣的,唯一的區別就是顏色不同,比如說敵方飛機是紅色的,而我方飛機是綠色的。在MIDP1.0中,我們就只好制作2張圖片來表示2種飛機,自然,這樣會造成jar空間的極大浪費。但是在MIDP2.0中,通過對圖片直接進行像素操作,反轉RGB中的一個值,我們只需要一張圖片就夠了,樣例代碼如下:
ColorMIDlet.Java
- import Java.io.IOException;
-
- import Javax.microedition.lcdui.Canvas;
- import Javax.microedition.lcdui.Display;
- import Javax.microedition.lcdui.Graphics;
- import Javax.microedition.lcdui.Image;
- import Javax.microedition.midlet.MIDlet;
- import Javax.microedition.midlet.MIDletStateChangeException;
-
- /**
- *
- * @author JagIE
- *
- */
- public class ColorMIDlet extends MIDlet {
- Canvas c = new ColorCanvas();
-
- public ColorMIDlet() {
- super();
- // TODO Auto-generated constructor stub
- }
-
- protected void startApp() throws MIDletStateChangeException {
- Display.getDisplay(this).setCurrent(c);
-
- }
-
- protected void pauseApp() {
- // TODO Auto-generated method stub
-
- }
-
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- // TODO Auto-generated method stub
-
- }
-
- }
-
- /**
- *
- * @author JagIE
- *
- */
- class ColorCanvas extends Canvas {
- Image srcImage;
-
- int[] targetImage1;
-
- int[] targetImage2;
-
- public ColorCanvas() {
- try {
- srcImage = Image.createImage("/av.png");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- targetImage1 = GraphicsUtil.flipImageColor(srcImage,
- GraphicsUtil.SHIFT_RED_TO_BLUE);
-
- targetImage2 = GraphicsUtil.flipImageColor(srcImage,
- GraphicsUtil.SHIFT_RED_TO_GREEN);
-
- }
-
- public void paint(Graphics g) {
-
- g.setColor(0, 0, 0);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
- g.setColor(0x00ff00);
- g.drawString("origin:", getWidth() / 2, 0, Graphics.TOP
- | Graphics.HCENTER);
- g.drawImage(srcImage, 30, 20, Graphics.LEFT | Graphics.TOP);
- g.drawString("SHIFT_RED_TO_BLUE:", getWidth() / 2,
- srcImage.getHeight() + 20, Graphics.TOP | Graphics.HCENTER);
- g.drawRGB(targetImage1, 0, srcImage.getWidth(), 30, srcImage
- .getHeight() + 40, srcImage.getWidth(), srcImage.getHeight(),
- true);
- g.drawString("SHIFT_RED_TO_GREEN:", getWidth() / 2, srcImage
- .getHeight() * 2 + 40, Graphics.TOP | Graphics.HCENTER);
- g.drawRGB(targetImage2, 0, srcImage.getWidth(), 30, srcImage
- .getHeight() * 2 + 60, srcImage.getWidth(), srcImage
- .getHeight(), true);
-
- }
-
- }
GraphicsUtil.Java
- import Javax.microedition.lcdui.Image;
-
- /**
- *
- * @author JagIE
- *
- */
- public class GraphicsUtil {
-
- public static final int SHIFT_RED_TO_GREEN = 0;
-
- public static final int SHIFT_RED_TO_BLUE = 1;
-
- public static final int SHIFT_GREEN_TO_BLUE = 2;
-
- public static final int SHIFT_GREEN_TO_RED = 3;
-
- public static final int SHIFT_BLUE_TO_RED = 4;
-
- public static final int SHIFT_BLUE_TO_GREEN = 5;
-
- public static int[] flipImageColor(Image source, int shiftType) {
- // we start by getting the image data into an int array - the number
- // of 32-bit ints is equal to the width multiplIEd by the height
- int[] rgbData = new int[(source.getWidth() * source.getHeight())];
- source.getRGB(rgbData, 0, source.getWidth(), 0, 0, source.getWidth(),
- source.getHeight());
-
- // now go through every pixel and adjust it's color
- for (int i = 0; i < rgbData.length; i++) {
- int p = rgbData[i];
-
- // split out the different byte components of the pixel by
- // applying
- // a mask so we only get what we need, then shift it to make it
- // a normal number we can play around with
- int a = ((p & 0xff000000) >> 24);
- int r = ((p & 0x00ff0000) >> 16);
- int g = ((p & 0x0000ff00) >> 8);
- int b = ((p & 0x000000ff) >> 0);
-
- int ba = a, br = r, bb = b, bg = g; // backup copIEs
-
- // flip the colors around according to the Operation required
- switch (shiftType) {
- case SHIFT_RED_TO_GREEN:
- g = r;
- r = bg;
- break;
- case SHIFT_RED_TO_BLUE:
- b = r;
- r = bb;
- break;
- case SHIFT_GREEN_TO_BLUE:
- g = b;
- b = bg;
- break;
- case SHIFT_GREEN_TO_RED:
- g = r;
- r = bg;
- break;
- case SHIFT_BLUE_TO_RED:
- b = r;
- r = bb;
- break;
- case SHIFT_BLUE_TO_GREEN:
- b = g;
- g = bb;
- break;
- }
-
- // shift all our values back in
- rgbData[i] = (a << 24) | (r << 16) | (g << 8) | b;
- }
-
- return rgbData;
- }
-
- }