现在的位置: 首页 > 综合 > 正文

j2me 颜色渐变与图像透明效果

2013年08月09日 ⁄ 综合 ⁄ 共 6125字 ⁄ 字号 评论关闭

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
*
* @author Jagie
*
*/
public class ShadowMIDlet extends MIDlet {
    Canvas c = new ShadowCanvas();
 static ShadowMIDlet instance;  
    public ShadowMIDlet() {
  instance=this;
    }

    protected void startApp() throws MIDletStateChangeException {
        Display.getDisplay(this).setCurrent(c);

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }
 public static void ExitApp()
 {
  try
  {
   instance.destroyApp(true);
   instance.notifyDestroyed();
  }
  catch (Exception e)
  {
  }
  
 }

}

/**
*
* @author Jagie
*
*/
class ShadowCanvas extends Canvas implements Runnable {
    int w, h;

    // 原始图片
    Image srcImage;

    // 原始图片的像素数组
    int[] srcRgbImage;

    // 渐变图片的像素数组
    int[] shadowRgbImage;

    int imgWidth, imgHeight;

    int count;

    public ShadowCanvas() {
        w = this.getWidth();
        h = this.getHeight();
        try {
            srcImage = Image.createImage("/av.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        imgWidth = srcImage.getWidth();
        imgHeight = srcImage.getHeight();
        // 制造原始图片的像素数组,用一个int来代表每一个像素,按位表示方式是:0xAARRGGBB
        srcRgbImage = new int[imgWidth * imgHeight];
        // 获取原始图片的所有像素,参见MIDP APPI文档
        srcImage.getRGB(srcRgbImage, 0, imgWidth, 0, 0, imgWidth, imgHeight);

        shadowRgbImage = new int[srcRgbImage.length];

        System.arraycopy(srcRgbImage, 0, shadowRgbImage, 0,
                shadowRgbImage.length);

        // 渐变图片的所有像素已开始都是全透明的
        for (int i = 0; i < shadowRgbImage.length; i++) {
            shadowRgbImage[i] &= 0x00ffffff;
        }

       new Thread(this).start();

    }
 
    /**
     * 获取颜色渐变RGB数组,
     * 为了获取这个数据,而又跟CLDC1.0兼容,导致项目增加了一个Float类
     * 导致程序变大
     * @param width
     * @return
     */
    public final static int[] getShadeColor(int color , int width){
         int[] rgb;
    
          int shadeWidth = width;
          int nRgbData = shadeWidth<<2;  //shadeWidth*4
          
          rgb = new int[nRgbData];
      
          int alpha = -127;
          for (int i = 0; i < shadeWidth; i++)
          {
            alpha = -127 + i;
            //主要算法在这里。
            int col = color | (128 - alpha << 24);
            rgb[i]                  = col;
            rgb[i + shadeWidth    ] = col;
            rgb[i + shadeWidth * 2] = col;
            rgb[i + shadeWidth * 3] = col;              
          }
          return rgb;
    }

  public final static void drawShadeRect(Graphics g,   int color, int x , int y, int width, int height){
         int[] rgb = getShadeColor(color, width);
         for (int by = y; by < y + height; by +=2)
              {
                int nTemp = y + height - (by - y);
                nTemp = nTemp>2 ? 2:nTemp;
                  g.drawRGB(rgb, 0, width, x, by, width,
                      nTemp, true);
              }
    }

int counter=0;
int RBGColor=0x00FF0000;
public void keyPressed(int key)
{
 System.out.println("key code="+key);
 if(key==-6||key==-7||key==-21||key==-22)
 {
  ShadowMIDlet.ExitApp();
 }
 if(key==-5)
 {
  if(counter%4==0)
  {
   RBGColor=0x00FFab00;
  }
  if(counter%4==1)
  {
   RBGColor=0x0000ff00;
  }
  if(counter%4==2)
  {
   RBGColor=0x000000ff;
  }
  if(counter%4==3)
  {
   RBGColor=0x00ff0000;
  }
  counter++; 
  repaint();
 }

}
 
  
 
    public void paint(Graphics g) {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, w, h);
  
 // drawShadeRect(g,0x00FF0000,0,100,240,20);
  drawShadeRect(g,RBGColor,0,20,240,20);

        // 绘制渐变图片
       g.drawRGB(shadowRgbImage, 0, imgWidth, (w - imgWidth) / 2,
             (h - imgHeight) / 2, imgWidth, imgHeight, true);
      g.setColor(0, 255, 0);
     g.drawString("count=" + count, w / 2, 30, Graphics.HCENTER
          | Graphics.TOP);

  

    }

    public void run() {
        while (true) {

            boolean changed = false;
            // 改变渐变图片的每一个像素
            for (int i = 0; i < shadowRgbImage.length; i++) {
                // 获取渐变图片的某一像素的alpha值
                int alpha = (shadowRgbImage[i]& 0xff000000) >>> 24;
                // 原始图片的对应像素的alpha值
                int oldAlpha = (srcRgbImage[i] & 0xff000000) >>> 24;

                if (alpha < oldAlpha) {
                    // alpha值++
                    shadowRgbImage[i] = ((alpha + 1) << 24)
                            | (shadowRgbImage[i] & 0x00ffffff);
                    changed = true;
                }
            }

            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            count++;
            repaint();
            // 当所有像素的alpha值都达到原始值后,线程运行结束
            if (!changed) {
                System.out.println("over");
                break;
            }
        }

    }

}

 

 

final class MagicColorsUtiles
 {
  //grain ==是渐变的粒度
  public static final void drawStateBar(Graphics g,int rgb,int x,int y,int wid,int hig,int grain)
  {
   int[] Rgb1={(rgb&0xff0000)>>>16,(rgb&0x00ff00)>>>8,rgb&0x0000ff};
   int[] xyz=new int[3];
   for (int j = 0, k = 0; j <= hig/2; j++, k+=grain)
   {
      if((Rgb1[0]+k)<0xff)
    xyz[0]=Rgb1[0]+k;
     else
    xyz[0]=0xff;

     if((Rgb1[1]+k)<0xff)
    xyz[1]=Rgb1[1]+k;
     else
    xyz[1]=0xff;
 
     if((Rgb1[2]+k)<0xff)
    xyz[2]=Rgb1[2]+k;
     else
     xyz[2]=0xff; 
     g.setColor(xyz[0],xyz[1],xyz[2]);
     g.drawLine(x, y + j, x + wid, y + j);
     g.drawLine(x, y + hig - j, x + wid, y + hig - j);
     }
  }

     public final static void drawShadeRect(Graphics g,   int color, int x , int y, int width, int height)
     {        
      int[] rgb = getShadeColor(color, width);
            for (int by = y; by < y + height; by +=4)
            {
               int nTemp = y + height - (by - y);
               nTemp = nTemp>4 ? 4:nTemp;
               g.drawRGB(rgb, 0, width, x, by, width,nTemp, true);
           }
  
        }
     public final static int[] getShadeColor(int color , int width)
  {
         int[] rgb;    
          int shadeWidth = width;
         int nRgbData = shadeWidth<<2;  //shadeWidth*4
          rgb = new int[nRgbData];
          int alpha = -127;
          for (int i = 0; i < shadeWidth; i++)
          {
            alpha = -127 + i;
            //主要算法在这里。
            int col = color | (128 - alpha << 24);
            rgb[i]                  = col;
   rgb[i + shadeWidth    ] = col;
   rgb[i + shadeWidth * 2] = col;
   rgb[i + shadeWidth * 3] = col;              
          }
          return rgb;
  }
  
 }

 

 

 

抱歉!评论已关闭.