J2ME中实现半透明

来源:互联网 发布:淘宝吉他店铺 编辑:程序博客网 时间:2024/05/16 19:18

在J2ME MIDP1.0规范中并未实现半透明 功能,只有少数手机厂商提供这方面的API,例如诺基亚.到了MIDP2.0,虽然已经提供了像getRGB(),drawRGB()这样的API,但仍然有很部分MIDP2.0的手机不能实现半透明(目前所知的摩托E398就无法实现半透明),鉴于这种情况,使得在游戏开发中,不可能用同一种方法实现各种不同品牌不同机型上的半透明效果),因而,必须对具体机型做针对性的处理.以下是两种创建半透明图片的方法:

1.利用诺基亚自身提供的API,适合诺基亚手机和支持诺基亚开发包的手机(如索爱K700)

/**
   * 
@param path 源图片的路径
   * 
@param w 源图片宽
   * 
@param h 源图片高
   * 
@return 半透明图
   
*/

  Image createAlphaImage(String path,
int w,int h) {
   DataInputStream dis 
= new DataInputStream(getClass().getResourceAsStream(path));
   ByteArrayOutputStream baos 
= new ByteArrayOutputStream();
  
try {
    
try {
      
while (true{
        baos.write(dis.readByte());
      }

    }

    
catch (IOException ex1) {
    }

    dis.close();
  }

  
catch (IOException ex) {
  }

   
//将源PNG图片数据转成字节数组
   byte[] data=baos.toByteArray();
   
short[] pixels=new short[w*h];
   
   
//创建可变图片
   Image img=DirectUtils.createImage(data,0,data.length);
   Graphics g
=img.getGraphics();
   DirectGraphics dg
=DirectUtils.getDirectGraphics(g);
   
//此处DirectGraphics.TYPE_USHORT_4444_ARGB只针对QD等1.0的手机 对于2.0手机根据具体颜色制式做调整
   dg.getPixels(pixels,0,w,0,0,w,h,DirectGraphics.TYPE_USHORT_4444_ARGB);
   
int length=pixels.length;
   
for(int i=0;i<length;i++){
       
//设置alpha值 即半透明度
     pixels[i]=(short)((pixels[i]&0x0fff)+0x3000);
   }

   dg.drawPixels(pixels,
true,0,w,0,0,w,h,0,dg.TYPE_USHORT_4444_ARGB);
   
return img;
  }

对于上面getPixels()方法,由于QD系列手机中分别采用4个bit来表示RGB三种颜色,另加4bit作为alpha通道,因此,它的颜色制式实现上只占用4*4=16bit,即一个short,所以用DirectGraphics.TYPE_USHORT_4444_ARGB制式.对于其他机型,可参考诺基亚J2ME开发文档.

以上方法适用于通过已有图片创建半透明图片.而对于想创建半透明的纯色图片,则更简单.

 

/**
  * 创建半透明图片
  * 
@param argb 半透明度 取值在0到100之间
  * 
@param width 图片宽度
  * 
@param height 图片高度
  * 
@return 创建好的半透明图片
  
*/

  
public static Image createAlpImageQD(int argb,int w,int h){
  
int length=w*h;
  
short[] pixels=new short[length];
  
try{
      
//创建一张黑色的可变图片
  Image img=DirectUtils.createImage(w,h,0x0000);
  Graphics g
=img.getGraphics();
  DirectGraphics dg
=DirectUtils.getDirectGraphics(g);
  
int alpha=(argb*0xf/100)&0xf;
  argb
=alpha<<12;
  
for(int i=0;i<length;i++){
  pixels[i]
=(short)(0x0|argb);
  }

  dg.drawPixels(pixels,
true,0,w,0,0,w,h,0,DirectGraphics.TYPE_USHORT_4444_ARGB);
//  numb2=200;
  return img;
  }
catch(Exception e){}
  
return null;
  }

2,使用MIDP2.0提供的API实现半透明

 

/**
  * 创建半透明图片(MIDP2.0)
  * 
@param path 源PNG图片路径
  * 
@return 创建好的半透明图片
  
*/

  Image createAlphaImage(String path)
{
      
try{
          Image tmpImage 
= Image.createImage(path);
      
int[]data=new int[tmpImage.getWidth()*tmpImage.getHeight()];
      tmpImage.getRGB(data,
0,tmpImage.getWidth(),0,0,tmpImage.getWidth(),tmpImage.getHeight());
      
int len=data.length;
      
for(int i=0;i<len;i++){
          
//0x65000000为半透明度,可根据需要适当调整
        data[i]=data[i]+0x65000000;
      }

      
return Image.createRGBImage(data,tmpImage.getWidth(),tmpImage.getHeight(),true);
      }

      
catch(Exception e){
          System.out.println(
"create Alpha Image fail!!");
          
return null;
      }

  }