BlackBerry应用开发指南 使用图像对象画图

移动开发
本文为《BlackBerry应用开发指南》UI设计篇的使用图像对象画图,使用 Graphics 类绘制整个屏幕或一个 BitmapField。如果你的应用程序不包含任何field,调用Screen.getGraphics()来获得整个屏幕的绘图上下文。

Graphics 对象允许应用程序完成绘图功能和描绘(rendering)操作。使用 Graphics 类绘制整个屏幕或一个 BitmapField。如果你的应用程序不包含任何field,调用Screen.getGraphics()来获得整个屏幕的绘图上下文。

为了绘制一个指定的 BitmapField,应用程序通过传入一个 field 到 Graphics 的  构造子中,来为一个指定的 field 获得一个绘图上下文.当绘制一个 BitmapField 时,field 管理器在 field重绘时传递一个绘图上下文给 field。为了完成绘制一个定制的 field,当你扩展 Field 类时覆写 Graphics.paint()方法。

Graphics 类允许你绘制图形,例如弧线,直线,矩形以及圆。

使用图形上下文

为了利用 Graphics 类绘制,为每各自的 field 或整个屏幕获得一个图形上下文。

为了为各自的 Field 获取一个图形上下文,调用 Graphics 的构造子。

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

为了为整个屏幕获得一个图形上下文,调用 Screen.getGraphics( )。

  1. Graphics  graphics  =  Screen.getGraphics(); 

为使用任何图形上下文绘图,你的方法务必要在 field 或屏幕的界限内完成它们的绘制功能。

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

如果你的图形上下文没有应用到整个屏幕,加入 BitmapField 到屏幕中。

  1. mainScreen.add(surfaceField); 

创建一个与标准的  BlackBerry UI一致的界面

DrawStyle 接口提供了 Graphics 和 Field 对象使用的接口。DrawStyle 的实现允许你创建一个与标准 BlackBerry UI 一致的接口。如果你正扩展 Field 类来创建一个定制的 Field,你的代码需要接受合适的样式,这样它与标准的 BlackBerry 应用程序类似。

DrawStyle 作为 style 参数应用在 field 上,如下面的例子:

  1. ButtonField  buttonField  =  new ButtonField(DrawStyle.ELLIPSIS); 

你可以在下面的对象中使用 DrawStyle 元素:

◆BitmapField

◆ButtonField

◆DateField

◆Graphics

◆LabelField

◆ObjectListField

用颜色绘制

利用彩色绘制只在彩屏的 BlackBerry 设备上适用。为了判断 BlackBerry 设备是否支持彩色显示 ,调用Graphics.isColor(). 为了决定BlackBerry设备支持的颜色象素,调用Graphics.numColors( )。

设置 alpha 值

全局 alpha 值决定他和绘制区域中象素的透明度,0(0x0000)是完全透明(不可见),255(0x00FF)是完全不透明。为了设置或得到全局 alpha 值,调用 Graphics.setGlobalAlpha()或 Graphics.getGlobalAlpha().

(注:BlackBerry 为特定的光栅操作使用 alpha 值。文本和绘制操作不会用到。)

决定光栅操作的支持

为决定一个 Graphics 对象是否支持一个特定的光栅操作,调用 Graphics.isRopSupported(int),使用下面提供的常数之一作为参数。

 

绘制一个路径(Path)  

为了绘制一组阴影填充的路径,调用 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.");} 

使用绘制格式

为了将绘制格式打开或关闭,调用 Graphics.setDrawingStyle(int drawStyle,  Boolean on),在这里,on 指定是否打开(true)或关闭(false)绘制格式.为了判断一个绘制格式是否已经设置,调用 Graphics.isDrawingStyleSet(int drawStyle).

 像印花一样使用单色位图 field  

通过用颜色提交不透明的区域,STAMP_MONOCHROME 选项允许应用程序使用单色位图,如同印花一样。这个选项用于位图,这个位图是 1 位的,并且有定义的 alpha。

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

从未处理的数据绘制一图像

1.    创建一空的位图。在本例中,类型和大小都复制到一个已经存在的位图。

2.    使用新建的位图创建一个 Graphics 对象作为绘图表面。

3.    调用 Graphics.rawRGB(),并使用从源处来的未处理的数据绘制一个新的图像。

  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 类型,调用 Bitmap.getType()。这个方法返回下面常数中的一个:

◆在黑白屏幕的 BlackBerry 设备上,数据存储在列中,因此,Bitmap.getType()返回COLUMNWISE_MONOCHROME。头 2 个字节代表了位图第一个列里的头 16 个象素。

◆在彩屏的 BlackBerry 设备上,数据保存在行里,因此 itmap.getType()为黑白图片返回ROWWISE_MONOCHROME,为彩色图片返回ROWWISE_16BIT_COLOR。在黑白图片里,头 2 个字节代表了位图的第一行的头 16 个象素,从左到右。在彩色图片里,头2个字节代表了第一个象素。

下面 2 个 Bitmap 的构造子允许你指定一个 type 参数:

◆Bitmap(int type, int width, int height)

◆Bitmap(int type, int width, int height, byte[] data)

为了获取 BlackBerry 设备的缺省位图类型,调用静态方法:

Bitmap.getDefaultType()#p#

代码实例

DrawDemo.java 从预定义的位图里获取未处理的数据,然后使用这些数据绘制一个新的位图。最后显示原始的和恢复的图像。

例: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应用开发指南
  2. BlackBerry应用开发指南 UI设计之图片操作
  3. BlackBerry应用开发者指南 创建客户定制的UI组件
  4. BlackBerry应用开发者指南 UI API篇之管理UI组件
  5. BlackBerry应用开发者指南 UI API篇之显示UI组件
责任编辑:佚名 来源: 网络整理
相关推荐

2011-04-18 11:00:34

使用音频BlackBerry

2011-04-15 16:05:00

监听UI对象的改变BlackBerry

2011-04-15 14:22:20

图片操作UIBlackBerry

2011-06-07 09:10:41

BlackBerry 开发

2011-04-02 13:44:08

2011-04-13 09:55:16

Mail APIBlackBerry

2011-04-13 13:38:57

选项APIBlackBerry

2011-04-13 11:31:06

PIM APIBlackBerry

2011-12-05 15:44:45

Knockout

2011-12-05 14:50:13

Knockout

2012-01-04 16:21:11

2010-05-22 16:57:09

BlackBerry开

2011-04-13 14:10:27

.alx文件BlackBerry

2011-11-29 16:38:58

Knockout

2011-11-29 16:56:30

Knockout

2011-11-30 16:29:41

2022-08-02 08:01:09

开发插件Chrome前端技术

2011-07-25 16:21:22

Sencha touc

2011-04-14 10:34:08

BlackBerry

2009-06-24 16:30:21

JSF组件模型
点赞
收藏

51CTO技术栈公众号