介紹MIDP2.0新特性Alpha混合

來源:互聯網
上載者:User

   在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混合,通過執行個體形象的說明這一效果。我們開啟一個圖片,然後在映像的上面進行半透明的掩飾,製造出下面的效果,參考:

        製作這樣的效果並不難,我們只需要調用上面的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.TOP|Graphics.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.TOP|Graphics.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) {}
        }
    }
  
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.