J2me中实现淡入淡出效果
飘飘白云(l_zhaohui@163.com)
在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 : 飘飘白云(l_zhaohui@163.com)
* 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[i] = (byte)255;
}