上次我们说到了怎么把一个图片处理成一个半透明的效果,下面是一个图片从透明到不透明的的变化效果
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();
}
}
}
}