程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 圖片透明效果

圖片透明效果

編輯:J2ME
透明效果 作者:佚名    文章來源:轉載    更新時間:2007-8-27 15:05:58 668 透明效果的設計,是開發游戲以及UI的常談話題,小弟整理了下關於透明效果的嗲們,有興趣的朋友可以看看

代碼
/**
*
* @author JagIE
*
*/
public class ShadowMIDlet extends MIDlet {
Canvas c = new ShadowCanvas();
public ShadowMIDlet() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(c);
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}
/**
*
* @author JagIE
*
*/
class ShadowCanvas extends Canvas implements Runnable {
int w, h;
// 原始圖片
Image srcImage;
// 原始圖片的像素數組
int[] srcRgbImage;
// 漸變圖片的像素數組
int[] shadowRgbImage;
int imgWidth, imgHeight;
int count;
public ShadowCanvas() {
w = this.getWidth();
h = this.getHeight();
try {
srcImage = Image.createImage("/av.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imgWidth = srcImage.getWidth();
imgHeight = srcImage.getHeight();
// 制造原始圖片的像素數組,用一個int來代表每一個像素,按位表示方式是:0xAARRGGBB
srcRgbImage = new int[imgWidth * imgHeight];
// 獲取原始圖片的所有像素,參見MIDP APPI文檔
srcImage.getRGB(srcRgbImage, 0, imgWidth, 0, 0, imgWidth, imgHeight);
shadowRgbImage = new int[srcRgbImage.length];
System.arraycopy(srcRgbImage, 0, shadowRgbImage, 0,
shadowRgbImage.length);
// 漸變圖片的所有像素已開始都是全透明的
for (int i = 0; i < shadowRgbImage.length; i++) {
shadowRgbImage &= 0x00ffffff;
}
new Thread(this).start();
}
public void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, w, h);
// 繪制漸變圖片
g.drawRGB(shadowRgbImage, 0, imgWidth, (w - imgWidth) / 2,
(h - imgHeight) / 2, imgWidth, imgHeight, true);
g.setColor(0, 255, 0);
g.drawString("count=" + count, w / 2, 30, Graphics.HCENTER
| Graphics.TOP);
}
public void run() {
while (true) {
boolean changed = false;
// 改變漸變圖片的每一個像素
for (int i = 0; i < shadowRgbImage.length; i++) {
// 獲取漸變圖片的某一像素的alpha值
int alpha = (shadowRgbImage & 0xff000000) >>> 24;
// 原始圖片的對應像素的alpha值
int oldAlpha = (srcRgbImage & 0xff000000) >>> 24;
if (alpha < oldAlpha) {
// alpha值++
shadowRgbImage = ((alpha + 1) << 24) | (shadowRgbImage & 0x00ffffff);
changed = true;
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
repaint();
// 當所有像素的alpha值都達到原始值後,線程運行結束
if (!changed) {
System.out.println("over");
break;
}
}
}
}






  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved