国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

介紹MIDP2.0新特性Alpha混合

2019-11-18 16:10:10
字體:
來源:轉載
供稿:網友

        在MIDP2.0中新增了Alpha混合特性,我們可以使用這個特性來對圖片進行一些處理。本文將結合一個具體的例子說明一下Alpha混合。

       首先我們參考一下MIDP2.0 java doc中關于Alpha PRocessing的說明:在可修改圖片中的每個像素都必須是完全模糊的,在不可修改圖片中的每個像素可以是完全透明的,完全模糊的或者介于兩者之間的,也就是半透明。系統的實現必須要支持存儲,處理和繪畫全透明和全模糊的像素,當從數據源創建一個圖片的時候(數據源可能來自PNG圖片或者一個字節數組),原圖片中的不透明像素和透明像素應該在新圖片中保持不變。對半透明像素的處理就和系統的實現相關了,要看Alpha混合是否被支持。如果系統實現支持Alpha混合,那么原圖中的半透明像素在新圖片中依然保持半透明,當然數值可能會根據系統支持的半透明的級別發生一些變化。如果系統實現不支持Alpha混合的話,任何半透明的像素在新圖片中應該使用全透明的像素來替換。

         接下來我們熟悉幾個方法,正是他們的引入增加了Alpha混合特性。
public static Image createRGBImage(int[] rgb,
                                   int width,
                                   int height,
                                   boolean processAlpha)

上面的方法來自于Image類,它允許我們從rgb數組創建一個不可修改的圖片,數組中的數值形式為0xAARRGGBB,其中AA代表透明度,后面的代表顏色值。具體方法的介紹請參考MIDP Java doc。

public void getRGB(int[] rgbData,
                   int offset,
                   int scanlength,
                   int x,
                   int y,
                   int width,
                   int height)

下面的方法同樣來自于Image類,這個方法可以讓我們從圖片的指定區域讀取ARGB像素值,并存儲到rgbData數組中。具體方法的介紹請參考MIDP Java doc。

public void drawRGB(int[] rgbData,
                    int offset,
                    int scanlength,
                    int x,
                    int y,
                    int width,
                    int height,
                    boolean processAlpha)


上面的方法是Graphics中新增的,使用這個方法我們可以在指定的區域繪畫一些列和設備無關的(RGB+透明)值,rgbData中的元素形式為0xAARRGGBB。其中AA代表透明度,00代表全透明  ,FF代表完全模糊。具體方法的介紹請參考MIDP Java doc。

       下面我們編寫一個例子來介紹如何使用Alpha混合,通過實例形象的說明這一效果。我們打開一個圖片,然后在圖像的上面進行半透明的掩飾,制造出下面的效果,參考下圖:
介紹MIDP2.0新特性Alpha混合

        制作這樣的效果并不難,我們只需要調用上面的drawRGB()方法即可,為了更好的掩飾這一效果。我們啟動一個新的線程來不斷的更新mask區域的高度,然后重新繪制屏幕(這里沒有考慮優化)。代碼如下:
    public void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());//paint Canvas background white
        g.drawImage(backgroundImage, x, y, Graphics.TOPGraphics.LEFT);
       
        //render the semi-transparent mask
        if (maskHeight != 0){
            g.drawRGB(maskArray, 0, imageWidth, x, y, imageWidth, maskHeight, true);
        }
    }
   
    public void run() {
        for(int i = 1; i <= imageHeight; i++) {
            maskHeight = i;
            repaint();
            try {
                Thread.sleep(50);
            }catch(InterruptedException ie) {}
        }
    }

       下面給出這個應用程序的源代碼,代碼出自Programming Java 2 Micro Edition on Symbian OS一書。筆者只是借花獻佛,因為最近論壇一直有人問Alpha相關的問題。
/*
 * Copyright 2003, 2004 Symbian Ltd.
 * For License terms see http://www.symbian.com/developer/techlib/codelicense.Html
 */

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

public class AlphaBlending extends MIDlet implements CommandListener {
   
    private Display display;
    private Form form;
    private Command exitCommand;
    private Command startCommand;
    private Command backCommand;
    private AlphaCanvas alphaCanvas;
   
    private static final String IMAGE_NAME = "/image.png";
   
   
    public AlphaBlending() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 2);
        startCommand = new Command("Start", Command.SCREEN, 1);
        backCommand = new Command("Back", Command.BACK, 1);
        alphaCanvas = new AlphaCanvas(IMAGE_NAME);
        form = new Form("Alpha Blending Demo");
        form.append(new StringItem(null, "Press 'Start' to run demo."));
        form.addCommand(exitCommand);
        form.addCommand(startCommand);
        form.setCommandListener(this);
    }
   
   
    public void startApp() {
        display.setCurrent(form);
    }
   
   
    public void pauseApp() {}
   
   
    public void destroyApp(boolean unconditional) {}
   
   
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }else if (c == startCommand) {
            display.setCurrent(alphaCanvas);
            alphaCanvas.addCommand(backCommand);
            alphaCanvas.setCommandListener(this);
           
            Thread t = new Thread(alphaCanvas);
            t.start();
        }else if (c == backCommand) {
           
            display.setCurrent(form);
        }
    }
   
   
}


/*
 * Copyright 2003, 2004 Symbian Ltd.
 * For License terms see http://www.symbian.com/developer/techlib/codelicense.html
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class AlphaCanvas extends Canvas implements Runnable {
   
    private Image backgroundImage;
    private int[] maskArray;
    private int imageWidth;
    private int imageHeight;
    private int x;
    private int y;
    private int maskHeight = 0;
   
    public AlphaCanvas(String imageName) {
        //create the background image
        try {
            backgroundImage = Image.createImage(imageName);
        }catch(IOException ioe) {
            ioe.printStackTrace() ;
        }
        imageWidth = backgroundImage.getWidth();
        imageHeight = backgroundImage.getHeight();
       
        //create a semi-transparent red mask to cover the background image
        maskArray = new int[imageWidth*imageHeight];
        for (int i = 0; i < imageWidth*imageHeight; i++) {
            maskArray[i] = 0x11FFF000;//alpha coefficient set to 0.3
        }
       
        x = (getWidth() - imageWidth)/2;
        y = (getHeight() - imageHeight)/2;
    }
   
    public void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());//paint Canvas background white
        g.drawImage(backgroundImage, x, y, Graphics.TOPGraphics.LEFT);
       
        //render the semi-transparent mask
        if (maskHeight != 0){
            g.drawRGB(maskArray, 0, imageWidth, x, y, imageWidth, maskHeight, true);
        }
    }
   
    public void run() {
        for(int i = 1; i <= imageHeight; i++) {
            maskHeight = i;
            repaint();
            try {
                Thread.sleep(50);
            }catch(InterruptedException ie) {}
        }
    }
   
}


從這里下載src

(出處:http://www.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永吉县| 牟定县| 常山县| 措美县| 乌兰察布市| 修文县| 梅河口市| 贵港市| 漳州市| 泗阳县| 湟中县| 蒙城县| 尚义县| 晋宁县| 溧水县| 霍山县| 翼城县| 遂昌县| 青铜峡市| 沈阳市| 虹口区| 丰原市| 万州区| 长汀县| 阜城县| 扎囊县| 高清| 黄梅县| 柏乡县| 林周县| 霍州市| 普定县| 东丽区| 莱州市| 昭苏县| 普兰县| 航空| 崇阳县| 耿马| 柳江县| 富平县|