日期:2014-05-20  浏览次数:20844 次

用getRGB的方法实现淡入淡出,但是效果不太好 求解!
Java code

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


class Game extends GameCanvas implements Runnable,GameData
{
    MIDlet Maze;
    Graphics gra;
    Image logo;
    Image donghua;
    int GameStatus;
    int w,h;
    int offsetx,offsety;
    int value;
    long time1;
    
    Game(MIDlet Maze)
    {
        super(false); 
        this.setFullScreenMode(true);
        this.Maze=Maze;
        w=this.getWidth();
        h=this.getHeight();
        value=0xff;

        
        GameStatus=LOGO;
        logo=createImage("/logo.png");
        donghua=createImage("/donghua.png");
        offsetx=(w-logo.getWidth())/2;
        offsety=(h-logo.getHeight())/2;
        time1=System.currentTimeMillis();
        new Thread(this).start();    
    }
    
    
    public Image createImage(String filename)
    {
        Image image=null;
        try
        {
            image=Image.createImage(filename);    
        }
        catch(java.io.IOException ex){}
        return image;        
    }
    
    public Image makeTransparent(Image img, int value) 
    {
           int w = img.getWidth();
        int h = img.getHeight();

        int[] imgData = new int[w*h];

        img.getRGB(imgData, 0, w, 0, 0, w, h);

        for (int i = 0; i < imgData.length; i++) 
        {
            imgData[i] = imgData[i] & 0x00ffffff + (value << 24);
        }

        return Image.createRGBImage(imgData, w, h, true);
    }
   
   
   
    public void paint(Graphics gra)
    {
        switch(GameStatus)
        {           
            case LOGO:
                gra.setColor(0);
                gra.fillRect(0,0,w,h);
                logo=makeTransparent(logo, value);
                value-=0x04;
                gra.drawImage(logo,offsetx,offsety,0);
        break;
                
            case DONGHUA:
                gra.setColor(0);
                gra.fillRect(0,0,w,h);
                gra.drawImage(donghua,offsetx,offsety,0);
                break;
        
            case END:
                break;
        }
            
    }
    
    public void run()
    {
        gra=getGraphics();
        while(true)
        {
            input();
            long st=System.currentTimeMillis();
            switch(GameStatus)
            {
                case LOGO:
                    paint(gra);
                   // if(System.currentTimeMillis()-time1>3000)
                    if(value<=0x00)
            {
                        GameStatus=DONGHUA;
                        time1=System.currentTimeMillis();
              gra.setColor(0);
                          gra.fillRect(0,0,w,h);
                    }
            break;
                    
                case DONGHUA:
                    if(System.currentTimeMillis()-time1>1000&&System.currentTimeMillis()-time1<3000)
                    paint(gra);
                    else if(System.currentTimeMillis()-time1>3000)
                    {
                        GameStatus=END;
                        time1=System.currentTimeMillis();
            gra.setColor(0);
                    gra.fillRect(0,0,w,h);
                    }
            break;    
        
                case END:
                    if(System.currentTimeMillis()-time1>1000)
                    Maze.notifyDestroyed();    
                   break; 
        }    
            flushGraphics();
            long tk=System.currentTimeMillis()-st;
            if(tk<100)
            {
                try{Thread.sleep(100-tk);}
                catch(Exception e){}    
            }  
        }    
    }
    
    public void input()
    {
        int keyState=getKeyStates();
    //System.out.println(keyState);
        switch(GameStatus)
        {
            case LOGO:    
                if((keyState&FIRE_PRESSED)!=0)    
                {
                    GameStatus=DONGHUA;
                    time1=System.currentTimeMillis();
                    gra.setColor(0);
                    gra.fillRect(0,0,w,h);
                }
                break;
                
            case DONGHUA:
                if((keyState&FIRE_PRESSED)!=0)    
                {
                    GameStatus=END;
                    time1=System.currentTimeMillis();
                    gra.setColor(0);
                    gra.fillRect(0,0,w,h);
        }
                break;
            case END:
                break;  
        }
    }
    
    
}

出现的图片渐变很不流畅 像是很卡很卡的效果 求高手指点!