Look into it ~

          present
          隨筆 - 32, 文章 - 0, 評(píng)論 - 3, 引用 - 0
          數(shù)據(jù)加載中……

          Fade in and out images in MIDP 2.0

          This tip describes how to change the alpha value of an image to make it appear blended. There's also an example MIDlet with source code.

          In MIDP 2.0 there's a new method in the Image class, getRGB(...) that will get all Alpha, Red, Green, Blue (ARGB) values from the image to an int array. We can use this method, and the resulting array, to change the alpha value of the image.

          Integers in J2ME are four bytes, each pixel in the image is described by the ARGB values where each color is one byte 0 to 255. If the alpha value is 0, the corresponding pixel will be transparent, if the alpha is 255, the pixel will be opaque.

          To get a specific color from the int array, it's possible to use the AND '&' operator and to accomplish the blending effect we need to get the colors, without the alpha value, from the int array.
          FF = 11111111 = 255
          0xFFFFFFFF - Alpha = 255, Red =255 Green = 255, Blue = 255
          (
          0xFFFFFFFF & 0x00FFFFFF= 0x00FFFFFF


          By doing this we'll only get the RGB colors from the int array, alpha equals zero.

          Now when we just have the RGB colors and alpha equals zero, we can just add our new alpha value.

          If we have the alpha value 255 and want to add this to our color, we need to use the shift left operator.

          To change:
          (00000000 00000000 00000000 11111111) to 
          (
          11111111 00000000 00000000 00000000)
          use the shift left 
          '<<' operator.
          (
          0xFF << 24= 0xFF000000.


          With this knowledge we now can change the alpha value or do masking for specific colors.

          • Use the getRGB(...) method in the image class to get all the ARGB values from the image to a int array.
          • Use the blend function below to change the alpha value for each value in the array.
          • Use the createRGBImage(...) method in the image class to create a new image from the edited int array.

          There are examples on how to use the getRGB and createRGBImage methods in the MIDlet below.
          public static void blend(int[] raw, int alphaValue){
              
          int len = raw.length;
              
          // Start loop through all the pixels in the image.
              for(int i=0; i<len; i++){
                  
          int a = 0;
                  
          int color = (raw[i] & 0x00FFFFFF); // get the color of the pixel.
                  a = alphaValue;     // set the alpha value we want to use 0-255.
           
                  a 
          = (a<<24);    // left shift the alpha value 24 bits.
                  
          // if color = 00000000 11111111 11111111 00000000 (0xFFFF00 = Yellow)
                  
          // and alpha= 01111111 00000000 00000000 00000000
                  
          // then c+a = 01111111 11111111 11111111 00000000
                  
          // and the pixel will be blended.
                  color += a;
                  raw[i] 
          = color;
              }
          }

          example code

          posted on 2008-08-15 10:18 LukeW 閱讀(199) 評(píng)論(0)  編輯  收藏 所屬分類: Tips, Tricks, Hints & Code

          主站蜘蛛池模板: 镇巴县| 名山县| 介休市| 宁强县| 云南省| 沧源| 栾川县| 青阳县| 锡林浩特市| 博客| 尤溪县| 日喀则市| 许昌县| 同心县| 崇礼县| 铁岭市| 容城县| 吉林省| 左权县| 抚顺县| 屏边| 时尚| 石泉县| 隆德县| 远安县| 邵东县| 广宁县| 樟树市| 清河县| 垫江县| 滁州市| 耿马| 乐陵市| 开原市| 广汉市| 赤城县| 珠海市| 白银市| 枝江市| 剑河县| 桃园县|