【J2me】Image 对象的一点小常识

在工作里面发现即使对于我们常常使用的Image对象,我们很多时候还是缺乏深刻了解的。

 

一般我们使用图片的时候会按照如下的操作:

 

Image s_image = Image.createImage(SCREEN_W, SCREEN_H);

Graphics s_g = s_image.getGraphics();

 

有时候就会遇见一些bug,比如无法获取s_g,明明自己调用了Image的getGraphics()方法的。

 

其实,主要的原因在于:

 

Image 分为可变图片和不可变图片,而任何从资源创建的都是不可变图片,任何使用了图片data数据在create的图片都是不可变的。

 

在API里面只有一种方法可以创建可变图片。即:

Image s_image = Image.createImage(SCREEN_W, SCREEN_H);

 

附上API里面的Image创建方法吧:

static Image

createImage(byte[] imageData, int imageOffset, int imageLength)
          Creates an immutable image which is decoded from the data stored in the specified byte array at the specified offset and length.

static Image

createImage(Image source)
          Creates an immutable image from a source image.

static Image

createImage(Image image, int x, int y, int width, int height, int transform)
          Creates an immutable image using pixel data from the specified region of a source image, transformed as specified.

static Image

createImage(InputStream stream)
          Creates an immutable image from decoded image data obtained from an InputStream.

static Image

createImage(int width, int height)
          Creates a new, mutable image for off-screen drawing.

static Image

createImage(String name)
          Creates an immutable image from decoded image data obtained from the named resource.

static Image

createRGBImage(int[] rgb, int width, int height, boolean processAlpha)
          Creates an immutable image from a sequence of ARGB values, specified as 0xAARRGGBB.

 

对了,API里面提供了一种方法来检测Image 图片是否为可变图片

isMutable()

 

所以在容易出错的地方,我们在获取G的时候可以用如下的代码增强健壮性:

 

Graphics s_g;

 

if ( s_image.isMutable())

{

s_g = s_image.getGraphics();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值