自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

BlackBerry應用開發(fā)指南 使用圖像對象畫圖

移動開發(fā)
本文為《BlackBerry應用開發(fā)指南》UI設計篇的使用圖像對象畫圖,使用 Graphics 類繪制整個屏幕或一個 BitmapField。如果你的應用程序不包含任何field,調(diào)用Screen.getGraphics()來獲得整個屏幕的繪圖上下文。

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)造子。

  1. Bitmap  surface  =  new Bitmap(100,  100);  
  2. BitmapField  surfaceField  =  new BitmapField  (surface);  
  3. Graphics  graphics  =  new Graphics(surface); 

為了為整個屏幕獲得一個圖形上下文,調(diào)用 Screen.getGraphics( )。

  1. Graphics  graphics  =  Screen.getGraphics(); 

為使用任何圖形上下文繪圖,你的方法務必要在 field 或屏幕的界限內(nèi)完成它們的繪制功能。

  1. graphics.fillRect(10,  10,  30,  30);  
  2. graphics.drawRect(15,  15,  30,  30); 

如果你的圖形上下文沒有應用到整個屏幕,加入 BitmapField 到屏幕中。

  1. 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 上,如下面的例子:

  1. 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():

  1. public void  
  2. drawShadedFilledPath(  
  3. int []  xPts,  
  4. int []  yPts,  
  5. byte []  pointTypes,  
  6. int  []  colors,  
  7. int  []  offsets) 

下面的例子繪制了一個從藍色到紅色混合的路徑。

  1. Bitmap  surface  =  new Bitmap(240,  160);   
  2.  BitmapField  surfaceField  =  new BitmapField(surface);   
  3.  add(surfaceField);    
  4. Graphics  graphics  =  new Graphics(surface);   
  5.  int []  X_PTS  =  {  0,  0,  240,  240  };  
  6.  int []  Y_PTS  =  {  20,  50,  50,  20  };  
  7. int []  drawColors  =  {  0x0000CC,  0x0000CC,  0xCC0000,  0xCC0000  };  
  8. try {graphics.drawShadedFilledPath(X_PTS,  Y_PTS,  null ,  drawColors , null);  
  9. }  
  10. 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。

  1. BitmapField field = new BitmapField(original,  
  2. BitmapField.STAMP_MONOCHROME); 

從未處理的數(shù)據(jù)繪制一圖像

1.    創(chuàng)建一空的位圖。在本例中,類型和大小都復制到一個已經(jīng)存在的位圖。

2.    使用新建的位圖創(chuàng)建一個 Graphics 對象作為繪圖表面。

3.    調(diào)用 Graphics.rawRGB(),并使用從源處來的未處理的數(shù)據(jù)繪制一個新的圖像。

  1. Bitmap  restored  =  new Bitmap(original.getType(),  original.getWidth(),  
  2. original.getHeight());  
  3. Graphics  graphics  =  new Graphics(restored);  
  4. try   {  
  5. graphics.drawRGB(argb,  0,  restored.getWidth(),  0,  
  6. 0,  restored.getWidth(),  
  7. restored.getHeight());  
  8. }  
  9. catch (Exception  e)  
  10. {  
  11. System.out.println("Error  occurred  during  drawing:  "  +  e);  
  12. }  

使用位圖類型

(注:下面關于位圖類型的詳情只提供類型信息。應用程序應不要依賴位圖的實際位格式作為格式,因在手持設備的軟件的未來版本可能會變化。)

為了決定 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

  1. /*  
  2. *  DrawDemo.java  
  3. *  Copyright  (C)  2002-2005  Research  In  Motion  Limited.  
  4. */  
  5. package com.rim.samples.docs.drawing;  
  6. import  net.rim.device.api.system.*;  
  7. import  net.rim.device.api.ui.*;  
  8. import  net.rim.device.api.ui.component.*;  
  9. import  net.rim.device.api.ui.container.*;  
  10. /*  The  DrawDemo.java  sample  retrieves  raw  data  from  a  predefined  
  11. bitmap  
  12. image,  and  then  draws  a  new  bitmap  using  the  data.  It  then  displays  
  13. the  original  and  restored  images.  */  
  14. public  class  DrawDemo  extends extends extends  
  15. extends  
  16. UiApplication  {  
  17. public    static  void   main(String[]  args)  {  
  18. }  
  19. DrawDemo  app  =  new  DrawDemo();  
  20. app.enterEventDispatcher();  
  21. }  
  22. public    DrawDemo()  
  23. {  
  24. pushScreen(new DrawDemoScreen());  
  25. }  
  26. final    class    DrawDemoScreen  extends  MainScreen  {  
  27. public   DrawDemoScreen()  
  28. {  
  29. super    ();  
  30. LabelField  title  =  new LabelField(“UI  Demo”,  
  31. LabelField.USE_ALL_WIDTH);  
  32. setTitle(title);  
  33. Bitmap  original  =  
  34. Bitmap.getPredefinedBitmap(Bitmap.INFORMATION);  
  35. Bitmap  restored  =  new Bitmap(original.getType(),  
  36. original.getWidth(),  
  37. original.getHeight());  
  38. Graphics  graphics  =  new Graphics(restored);//  Retrieve  raw  data  from  original  image.  
  39. int  []  argb  =  new  int  [original.getWidth()  *  
  40. original.getHeight()];  
  41. original.getARGB(argb,  0,  original.getWidth(),  0,  0,  
  42. original.getWidth(),original.getHeight());  
  43. //  Draw  new  image  using  raw  data  retrieved  from  original  image.  
  44. try  {  
  45. graphics.drawRGB(argb,  0,  restored.getWidth(),  0,  0,  
  46. restored.getWidth(),restored.getHeight());  
  47. }  
  48. catch   (Exception  e)  {  
  49. System.out.println(“Error  occurred  during  drawing:  “  +  e);  
  50. }  
  51. ififif  
  52. if  
  53. (restored.equals(original))  
  54. {  
  55. System.out.println(“Success!  Bitmap  renders  correctly  with  
  56. RGB  data.”);  
  57. }  
  58. else  if (!restored.equals(original))  
  59. {  
  60. System.out.println(“Bitmap  rendered  incorrectly  with  RGB  
  61. data.”);  
  62. }  
  63. BitmapField  field1  =  new BitmapField(original,  
  64. BitmapField.STAMP_MONOCHROME);  
  65. BitmapField  field2  =  new BitmapField(restored);  
  66. add(new LabelField(“Original  bitmap:  “));  
  67. add(field1);  
  68. add(new LabelField(“Restored  bitmap:  “));  
  69. add(field2);  
  70. }  

【編輯推薦】

  1. BlackBerry應用開發(fā)指南
  2. BlackBerry應用開發(fā)指南 UI設計之圖片操作
  3. BlackBerry應用開發(fā)者指南 創(chuàng)建客戶定制的UI組件
  4. BlackBerry應用開發(fā)者指南 UI API篇之管理UI組件
  5. BlackBerry應用開發(fā)者指南 UI API篇之顯示UI組件
責任編輯:佚名 來源: 網(wǎng)絡整理
相關推薦

2011-04-18 11:00:34

使用音頻BlackBerry

2011-04-15 16:05:00

監(jiān)聽UI對象的改變BlackBerry

2011-04-15 14:22:20

圖片操作UIBlackBerry

2011-06-07 09:10:41

BlackBerry 開發(fā)

2011-04-02 13:44:08

2011-04-13 11:31:06

PIM APIBlackBerry

2011-04-13 13:38:57

選項APIBlackBerry

2011-04-13 09:55:16

Mail APIBlackBerry

2011-12-05 14:50:13

Knockout

2011-12-05 15:44:45

Knockout

2012-01-04 16:21:11

2011-11-29 16:38:58

Knockout

2011-04-13 14:10:27

.alx文件BlackBerry

2010-05-22 16:57:09

BlackBerry開

2011-11-29 16:56:30

Knockout

2011-11-30 16:29:41

2022-08-02 08:01:09

開發(fā)插件Chrome前端技術

2011-07-25 16:21:22

Sencha touc

2011-04-14 10:34:08

BlackBerry

2009-06-24 16:30:21

JSF組件模型
點贊
收藏

51CTO技術棧公眾號