现在的位置: 首页 > 综合 > 正文

J2me中实现淡入淡出效果

2013年08月14日 ⁄ 综合 ⁄ 共 16494字 ⁄ 字号 评论关闭
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;
                                                                                    }
                                                                      }
                                                                      // 生成新的图片
                                                                      testImage = createFadeImage(imgData,rgbDataStartPos,rgbDataLength);
 
                                                                      subMode = smProc ;
                                                        }
                                                       
                                                        else if( subMode == smProc )
                                                        {
                                                                      try {
                                                                                    // 淡入淡出效果处理
                                                                                    fade(fadeType);
                                                                                    testImage = createFadeImage(imgData,rgbDataStartPos,rgbDataLength);
                                                                      }
                                                                      catch ( Exception e ) {
                                                                                    println("Create fade image error. /n" + e);
                                                                      }
                                                        }
                                          }
                            }
                           
                            //---------------------------------------------------
                            // 游戏描画处理
                            //---------------------------------------------------
                           
                            protected void paint( Graphics _g){
                                          if( offImage != null)
                                                        _g.drawImage(offImage, 0, 0, 0);
                            }
                           
                            void gameDraw()
                            {
                                          if( gameMode == gmStart )
                                          {
                                                        if( subMode == smProc ){
                                                                      g.setColor(0xffffff);
                                                                      g.fillRect(0,0,scrW,scrH);
                                                                      g.drawImage(testImage,20,140,0);
                                                        }
                                          }
                            }
                           
                            /**
                             * 淡入淡出效果处理
                             * @param type
                             */
                            public void fade( int type)
                            {
                                          int m = 0;
                                          int n = 0;
                                          // 淡入
                                          if ( type == FADE_IN ) {
                                                        for( int i = 0; i < rgbDataLength; i++ )
                                                        {
                                                                      m = imgData[rgbDataStartPos + i] & 0x000000ff;
                                                                      n = oldRGBData[i] & 0x000000ff;
                                                                      m = m < 0? m + 256 : m;
                                                                      n = n < 0? n + 256 : n;
                                                                     
                                                                      m = m > n ? m - FADE_STEP : n;
                                                                     
                                                                      imgData[rgbDataStartPos + i] = (byte)(m & 0xff);
                                                        }
                                          }
                                          // 淡出
                                          else if ( type == FADE_OUT ) {
                                                        for( int i = 0; i < rgbDataLength; i++ )
                                                        {
                                                                      m = imgData[rgbDataStartPos + i] & 0x000000ff;
                                                                      m = m < 0? m + 256 : m;
                                                                      m = m < 255 ? m + FADE_STEP : 255;
                                                                     
                                                                      imgData[rgbDataStartPos + i] = (byte)(m & 0xff);
                                                        }
                                          }
                            }
                           
                            /**
                             * 这里不是单纯创建一幅图片,crc32校验码也在这个函数里更新
                             * @param data
                             * @param startPos 需要校验的数据的起始位置
                             * @return
                             */
                            public Image createFadeImage(byte[] data,int startPos,int length)
                            {
                                          if (crcTable == null)
                                                        makeCrcTable();
                                         
                                          int endPos = startPos + length ;
                                          int crc = updateCrcChunk( data, startPos -8,endPos );
                                          data[endPos + 0] = (byte)(crc >> 24 & 0x000000FF);
                                          data[endPos + 1] = (byte)(crc >> 16 & 0x000000FF);
                                          data[endPos + 2] = (byte)(crc >> 8 & 0x000000FF);
                                          data[endPos + 3] = (byte)(crc & 0x000000FF);
                                         
                                          return Image.createImage(data,0,data.length);
                            }
                           
                            static int rgbDataStartPos = 0;
                            static int rgbDataLength = 0;
                           
                            /**
                             * @param imgData png图片的二进制数据
                             * @return 调色板颜色数据的字节数组
                             */
                            public byte[] getPlteRGBData(byte[] imgData)
                            {
                                          if (imgData == null || imgData.length <= 1)
                                                        return null;
 
                                          // PLTE chunk数据域的类型标识

抱歉!评论已关闭.