BlackBerry應用開發(fā)指南 使用圖像對象畫圖
Graphics 對象允許應用程序完成繪圖功能和描繪(rendering)操作。使用 Graphics 類繪制整個屏幕或一個 BitmapField。如果你的應用程序不包含任何field,調(diào)用Screen.getGraphics()來獲得整個屏幕的繪圖上下文。
為了繪制一個指定的 BitmapField,應用程序通過傳入一個 field 到 Graphics 的 構(gòu)造子中,來為一個指定的 field 獲得一個繪圖上下文.當繪制一個 BitmapField 時,field 管理器在 field重繪時傳遞一個繪圖上下文給 field。為了完成繪制一個定制的 field,當你擴展 Field 類時覆寫 Graphics.paint()方法。
Graphics 類允許你繪制圖形,例如弧線,直線,矩形以及圓。
使用圖形上下文
為了利用 Graphics 類繪制,為每各自的 field 或整個屏幕獲得一個圖形上下文。
為了為各自的 Field 獲取一個圖形上下文,調(diào)用 Graphics 的構(gòu)造子。
- Bitmap surface = new Bitmap(100, 100);
- BitmapField surfaceField = new BitmapField (surface);
- Graphics graphics = new Graphics(surface);
為了為整個屏幕獲得一個圖形上下文,調(diào)用 Screen.getGraphics( )。
- Graphics graphics = Screen.getGraphics();
為使用任何圖形上下文繪圖,你的方法務必要在 field 或屏幕的界限內(nèi)完成它們的繪制功能。
- graphics.fillRect(10, 10, 30, 30);
- graphics.drawRect(15, 15, 30, 30);
如果你的圖形上下文沒有應用到整個屏幕,加入 BitmapField 到屏幕中。
- mainScreen.add(surfaceField);
創(chuàng)建一個與標準的 BlackBerry UI一致的界面
DrawStyle 接口提供了 Graphics 和 Field 對象使用的接口。DrawStyle 的實現(xiàn)允許你創(chuàng)建一個與標準 BlackBerry UI 一致的接口。如果你正擴展 Field 類來創(chuàng)建一個定制的 Field,你的代碼需要接受合適的樣式,這樣它與標準的 BlackBerry 應用程序類似。
DrawStyle 作為 style 參數(shù)應用在 field 上,如下面的例子:
- ButtonField buttonField = new ButtonField(DrawStyle.ELLIPSIS);
你可以在下面的對象中使用 DrawStyle 元素:
◆BitmapField
◆ButtonField
◆DateField
◆Graphics
◆LabelField
◆ObjectListField
用顏色繪制
利用彩色繪制只在彩屏的 BlackBerry 設備上適用。為了判斷 BlackBerry 設備是否支持彩色顯示 ,調(diào)用Graphics.isColor(). 為了決定BlackBerry設備支持的顏色象素,調(diào)用Graphics.numColors( )。
設置 alpha 值
全局 alpha 值決定他和繪制區(qū)域中象素的透明度,0(0x0000)是完全透明(不可見),255(0x00FF)是完全不透明。為了設置或得到全局 alpha 值,調(diào)用 Graphics.setGlobalAlpha()或 Graphics.getGlobalAlpha().
(注:BlackBerry 為特定的光柵操作使用 alpha 值。文本和繪制操作不會用到。)
決定光柵操作的支持
為決定一個 Graphics 對象是否支持一個特定的光柵操作,調(diào)用 Graphics.isRopSupported(int),使用下面提供的常數(shù)之一作為參數(shù)。
繪制一個路徑(Path)
為了繪制一組陰影填充的路徑,調(diào)用 Graphics.drawShadedFilledPath():
- public void
- drawShadedFilledPath(
- int [] xPts,
- int [] yPts,
- byte [] pointTypes,
- int [] colors,
- int [] offsets)
下面的例子繪制了一個從藍色到紅色混合的路徑。
- Bitmap surface = new Bitmap(240, 160);
- BitmapField surfaceField = new BitmapField(surface);
- add(surfaceField);
- Graphics graphics = new Graphics(surface);
- int [] X_PTS = { 0, 0, 240, 240 };
- int [] Y_PTS = { 20, 50, 50, 20 };
- int [] drawColors = { 0x0000CC, 0x0000CC, 0xCC0000, 0xCC0000 };
- try {graphics.drawShadedFilledPath(X_PTS, Y_PTS, null , drawColors , null);
- }
- catch (IllegalArgumentException iae){System.out.println("Bad arguments.");}
使用繪制格式
為了將繪制格式打開或關閉,調(diào)用 Graphics.setDrawingStyle(int drawStyle, Boolean on),在這里,on 指定是否打開(true)或關閉(false)繪制格式.為了判斷一個繪制格式是否已經(jīng)設置,調(diào)用 Graphics.isDrawingStyleSet(int drawStyle).
像印花一樣使用單色位圖 field
通過用顏色提交不透明的區(qū)域,STAMP_MONOCHROME 選項允許應用程序使用單色位圖,如同印花一樣。這個選項用于位圖,這個位圖是 1 位的,并且有定義的 alpha。
- BitmapField field = new BitmapField(original,
- BitmapField.STAMP_MONOCHROME);
從未處理的數(shù)據(jù)繪制一圖像
1. 創(chuàng)建一空的位圖。在本例中,類型和大小都復制到一個已經(jīng)存在的位圖。
2. 使用新建的位圖創(chuàng)建一個 Graphics 對象作為繪圖表面。
3. 調(diào)用 Graphics.rawRGB(),并使用從源處來的未處理的數(shù)據(jù)繪制一個新的圖像。
- Bitmap restored = new Bitmap(original.getType(), original.getWidth(),
- original.getHeight());
- Graphics graphics = new Graphics(restored);
- try {
- graphics.drawRGB(argb, 0, restored.getWidth(), 0,
- 0, restored.getWidth(),
- restored.getHeight());
- }
- catch (Exception e)
- {
- System.out.println("Error occurred during drawing: " + e);
- }
- }
使用位圖類型
(注:下面關于位圖類型的詳情只提供類型信息。應用程序應不要依賴位圖的實際位格式作為格式,因在手持設備的軟件的未來版本可能會變化。)
為了決定 Bitmap 類型,調(diào)用 Bitmap.getType()。這個方法返回下面常數(shù)中的一個:
◆在黑白屏幕的 BlackBerry 設備上,數(shù)據(jù)存儲在列中,因此,Bitmap.getType()返回COLUMNWISE_MONOCHROME。頭 2 個字節(jié)代表了位圖第一個列里的頭 16 個象素。
◆在彩屏的 BlackBerry 設備上,數(shù)據(jù)保存在行里,因此 itmap.getType()為黑白圖片返回ROWWISE_MONOCHROME,為彩色圖片返回ROWWISE_16BIT_COLOR。在黑白圖片里,頭 2 個字節(jié)代表了位圖的第一行的頭 16 個象素,從左到右。在彩色圖片里,頭2個字節(jié)代表了第一個象素。
下面 2 個 Bitmap 的構(gòu)造子允許你指定一個 type 參數(shù):
◆Bitmap(int type, int width, int height)
◆Bitmap(int type, int width, int height, byte[] data)
為了獲取 BlackBerry 設備的缺省位圖類型,調(diào)用靜態(tài)方法:
Bitmap.getDefaultType()#p#
代碼實例
DrawDemo.java 從預定義的位圖里獲取未處理的數(shù)據(jù),然后使用這些數(shù)據(jù)繪制一個新的位圖。最后顯示原始的和恢復的圖像。
例:DrawDemo.java
- /*
- * DrawDemo.java
- * Copyright (C) 2002-2005 Research In Motion Limited.
- */
- package com.rim.samples.docs.drawing;
- import net.rim.device.api.system.*;
- import net.rim.device.api.ui.*;
- import net.rim.device.api.ui.component.*;
- import net.rim.device.api.ui.container.*;
- /* The DrawDemo.java sample retrieves raw data from a predefined
- bitmap
- image, and then draws a new bitmap using the data. It then displays
- the original and restored images. */
- public class DrawDemo extends extends extends
- extends
- UiApplication {
- public static void main(String[] args) {
- }
- DrawDemo app = new DrawDemo();
- app.enterEventDispatcher();
- }
- public DrawDemo()
- {
- pushScreen(new DrawDemoScreen());
- }
- final class DrawDemoScreen extends MainScreen {
- public DrawDemoScreen()
- {
- super ();
- LabelField title = new LabelField(“UI Demo”,
- LabelField.USE_ALL_WIDTH);
- setTitle(title);
- Bitmap original =
- Bitmap.getPredefinedBitmap(Bitmap.INFORMATION);
- Bitmap restored = new Bitmap(original.getType(),
- original.getWidth(),
- original.getHeight());
- Graphics graphics = new Graphics(restored);// Retrieve raw data from original image.
- int [] argb = new int [original.getWidth() *
- original.getHeight()];
- original.getARGB(argb, 0, original.getWidth(), 0, 0,
- original.getWidth(),original.getHeight());
- // Draw new image using raw data retrieved from original image.
- try {
- graphics.drawRGB(argb, 0, restored.getWidth(), 0, 0,
- restored.getWidth(),restored.getHeight());
- }
- catch (Exception e) {
- System.out.println(“Error occurred during drawing: “ + e);
- }
- ififif
- if
- (restored.equals(original))
- {
- System.out.println(“Success! Bitmap renders correctly with
- RGB data.”);
- }
- else if (!restored.equals(original))
- {
- System.out.println(“Bitmap rendered incorrectly with RGB
- data.”);
- }
- BitmapField field1 = new BitmapField(original,
- BitmapField.STAMP_MONOCHROME);
- BitmapField field2 = new BitmapField(restored);
- add(new LabelField(“Original bitmap: “));
- add(field1);
- add(new LabelField(“Restored bitmap: “));
- add(field2);
- }
- }
【編輯推薦】