Fade in J2ME, as far as I know there are at least three ways.
The first is to get the pixels that need to transform the picture, set the alpha channel value of each pixel in turn, and let it change between 0~100.
The second is to modify the color palette data of the image so that it changes between the palette's original data and 255.
The third, in fact, is also the use of the above method, first painting the picture, and then overlay a black rectangle on the picture, change the transparency of the black rectangle can achieve the effect of fading fade.
The first two methods are relatively large in comparison, and the first method cannot be implemented on some phones because midp1.0 does not support alpha channels. Here is an example of the second method, before we begin, should be familiar with the PNG file format, if not very clear, you can Google the code is very clear, the following is the source code:
------------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;
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;
}
// ---------------------------------------------------
Game Master Logic Processing
// ---------------------------------------------------
void Gamemain ()
{
if (Gamemode = = Gmstart)
{
if (Submode = = Sminit)
{
Get binary data for a picture
Imgdata = LoadFile (imageName);
Save the original picture's palette RGB data
Oldrgbdata = Getpltergbdata (Imgdata);
if (Fadetype = = fade_in)
{
Set palette color to white, fade in effect
for (int i = Rgbdatastartpos; I < Rgbdatastartpos
+ rgbdatalength; i++)
{
Imgdata = (byte) 255;
}
}
}
}
}
}