Transparent image processing in the development of J2ME mobile phone

Source: Internet
Author: User
Tags transparent image

In the development of J2ME mobile phone program, especially in the development of the game, transparent picture is almost an inevitable requirement, but in the game development we will use the Sprite Wizard to do graphical elements of the drawing, and these pictures may be from the resources of the file, If these files are formatted in a transparent format such as PNG, the transparent processing will be automatically implemented by the wizard class, without our special handling. If you encounter some special requirements, such as some of the pictures may need to be drawn by the program, or developers want to use the program to draw, then how to draw their own pictures are transparently drawn in the canvas?

A transparent picture is drawn in j2me not by a graphics method call, but by specifying the image object as a transparent picture object. There is a method in image that is called Creatergbimage (int[] rgb,int width,int height,boolean processalpha). This method creates a picture with a transparent and translucent effect, which is automatically displayed as a transparent or translucent color when the picture is drawn on the screen. But the image produced by this method has a problem, that is, the pixel color in this picture is immutable, that is to say, the object's getgraphics () cannot get the graphics object, and can not be customized to draw the picture through the program. All we need is a program to draw the picture and achieve the translucent effect. So what do we do?

We handle this, first we create a variable picture by using the CreateImage method of image, and then draw the custom picture data content. The example program is as follows: int w =;
int h =;
///create variable custom picture, specify width and height
Image bufimg = Image.createimage (W, h);
Graphics g = bufimage.getgraphics ();
G.setcolor (0xFFFFFF);   
//Use white to draw background
G.fillrect (0, 0, W, h);
//Draw desired picture content
G.setcolor (0xff0000);
G.fillrect Draw the picture complete, now need to get the data content in the picture.
//Prepare an array to hold the pixel color
int [] Imgdata = new int[w * h];
//Get pixel color
Bufimg.getrgb (imgdata, 0, W * H, 0, 0, W, h); /code>

The above program completes the custom picture pixel color extraction, in the J2ME image is this stipulation, if this is an immutable picture, obtains each pixel color is an int type value, if its high position is 00, namely the color is 0X00RRGGBB, This pixel is completely transparent, if it is 0xAARRGGBB, where a AA represents transparency, so that the color is translucent, now we have the custom variable picture pixel color values, next, we need to create immutable pictures, and to the white background set to completely transparent.  //将取得的保存在数组中的背景颜色的高位值设置为00
for(int i=0; i if(imgData[i] & 0x00FFFFFF == 0x00FFFFFF) {
imgData[i] = imgData[i] & 0x00FFFFFF;
}
}
  //创建不可变透明图片, 其中最后一个参数表示透明。
Image immutableImg = Image.createRGBImage(imgData, w, h, true);

All right, here's a transparent picture created, and this picture is drawn to canvas will be a completely transparent picture of the background. In fact, we can encapsulate a method on this function, the code is as follows:createAlphaImage(Image img, int alphaColor) {
int w = img.getWidth();
int h = img.getHeight();
int [] imgData = new int[w * h];
img.getRGB(imgData, 0, w * h, 0, 0, w, h);
for(int i=0; i if(imgData[i] & alphaColor == alphaColor) {
imgData[i] = imgData[i] & alphaColor;
}
}

The method parameters are described as follows:

Img-is an opaque original picture.

alphacolor-is the pixel color that needs to be set to transparent, in the form of 0X00RRGGBB.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.