飄飄白雲([email protected])
在J2ME中實現淡進淡出後果,據我所知至少有三種方法。
第一種是取得需要變換圖片的像素,依次設置每個象素的alpha通道值,讓它在0~100之間變更。
第二種是修正圖片的調色板數據,讓其在調色板原始數據到255之間變更。
第三種,實在也是利用上面的措施,先描畫圖片,然後在圖片上籠罩一個玄色矩形,轉變這個玄色矩形透明度就可以實現淡進淡出的後果。
前兩種方法相比擬的話,第一種方法運算量是比擬大的,而且第一種方法由於midp1.0不支撐alpha通道,在一些手機上無法實現。
下面給出第二種方法的示例,在我們開端之前,應當熟悉png文件格局,假如還不是很明白的話,可以google一下,或者查看前面的帖子中的相干連接。
代碼很明白,下面是源代碼:
------------CODE__START-----------------------
import Java.io.DataInputStream;
import Java.util.Timer;
import Java.util.TimerTask;
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;
/**
* Discription : 修正調色板數據實現淡進淡出後果 Author : 飄飄白雲([email protected]) Created date :
* 2006/07/13 18:06:39 ModifIEd history :
*/
public class PngFadeInOut extends MIDlet
{
GameCanvas canvas;
Display display;
public PngFadeInOut()
{
super();
try
{
display = Display.getDisplay(this);
canvas = new GameCanvas(this);
} catch (Exception e)
{
}
}
protected void startApp() throws MIDletStateChangeException
{
if (canvas != null)
{
display.setCurrent(canvas);
canvas.start();
}
}
protected void pauseApp()
{
canvas.isExit = true;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
}
class GameCanvas extends Canvas
{
static String imageName = "/fadeInOut.png";
public static final int MAX_TARDINESS = 30;
// ---------------------------------------------------
PngFadeInOut app;
boolean isInited = false;
public boolean isExit = false;
Image offImage = null;
public Graphics g = null;
int scrW;
int scrH;
// ---------------------------------------------------
int gameMode;
int subMode;
static final int smInit = 0;
static final int smProc = 1;
static final int smEnd = 2;
static final int gmStart = 0;
// ---------------------------------------------------
Image testImage = null;
byte[] oldRGBData = null;
byte[] imgData = null;
static final int FADE_IN = 0;
static final int FADE_OUT = 1;
static final int FADE_STEP = 1;
static int fadeType = FADE_IN;
// ---------------------------------------------------
// ---Timer process
// ---------------------------------------------------
TimerTask task;
Timer timer;
int timerDelayTime;
public boolean isTimerRunning;
void startTimer(int delayTime)
{
timerDelayTime = delayTime;
isTimerRunning = true;
timer = new Timer();
task = new ProcessingRunner();
timer.scheduleAtFixedRate(task, 0, delayTime);
}
public void stopTimer()
{
if (task != null)
task.cancel();
isTimerRunning = false;
timer = null;
task = null;
}
// -----------------------------------------------------
class ProcessingRunner extends TimerTask
{
public void run()
{
if (isExit)
{
stopTimer();
app.notifyDestroyed();
}
if (!isTimerRunning
|| System.currentTimeMillis() - scheduledExecutionTime() >= timerDelayTime)
{
return;
}
gameMain();
gameDraw();
repaint();
serviceRepaints();
System.gc();
}
}
// -------------------------------------------------
public GameCanvas(MIDlet midlet)
{
app = (PngFadeInOut) midlet;
}
public void start()
{
setFullScreenMode(true);
while (!isShown())
;
if (!isInited)
{
gameInit();
isInited = true;
}
startTimer(MAX_TARDINESS);
}
void gameInit()
{
scrW = getWidth();
scrH = getHeight();
if (g == null || offImage == null)
{
offImage = Image.createImage(scrW, scrH);
g = offImage.getGraphics();
}
changeGameMode(gmStart);
}
public void changeGameMode(int gm)
{
gameMode = gm;
subMode = smInit;
}
// ---------------------------------------------------
// 游戲主邏輯處理
// ---------------------------------------------------
void gameMain()
{
if (gameMode == gmStart)
{
if (subMode == smInit)
{
// 取得圖片的二進制數據
imgData = loadFile(imageName);
// 保留原始圖片的調色板rgb數據
oldRGBData = getPlteRGBData(imgData);
if (fadeType == FADE_IN)
{
// 設置調色板色彩為白色,淡進後果
for (int i = rgbDataStartPos; i < rgbDataStartPos
+ rgbDataLength; i++)
{
imgData = (byte) 255;
}
}
}
}
}
}