import Java.io.IOException;
import Javax.microedition.lcdui.Canvas;
import Javax.microedition.lcdui.Graphics;
import Javax.microedition.lcdui.Image;
/**
* @author 劉軍
* @version 1.0
*/
public class TCanvas extends Canvas implements Runnable{
Image image;//要處理的圖片
int argb[];
int a= 0;//把象素的透明度初始值設置為0,然後在線程中不斷地增加這個值
public TCanvas() {
super();
try {
image = Image.createImage("/test.png");//導入圖片
} catch (IOException e) {
e.printStackTrace();
}
argb = new int[image.getWidth()*image.getHeight()];
image.getRGB(argb,0,image.getWidth(),0,0,image.getWidth(),image.getHeight());//獲得圖片的ARGB值
int temp;
for(int i=0;i<argb.length;i++)
{
argb[i]=(a<<24) | (argb[i] & 0x00FFFFFF);// 修改最高2位的值
}
Thread t = new Thread(this);
t.start();
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0);
g.drawImage(image,0,0,Graphics.TOP|Graphics.LEFT);
g.drawRGB(argb,0,image.getWidth(),0,100,image.getWidth(),image.getHeight(),true);// 畫象素數組
g.drawString(a+"",10,90,Graphics.TOP|Graphics.LEFT);
}
public void run()
{
while(a<256)
{
//改變象素內容
for(int i=0;i<argb.length;i++)
{
argb[i]=(a<<24) | (argb[i] & 0x00FFFFFF);// 修改最高2位的值
}
repaint();//重繪屏幕
a++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}