日期:2014-05-20  浏览次数:20621 次

J2ME 颜色渐变
import javax.microedition.lcdui.Graphics;


public class CrystalRGB {

/**
  * 竖直方向的颜色渐变
  * @param g
  * @param color
  * @param x
  * @param y
  * @param width
  * @param height
  */
public final static void drawRGBVRect(Graphics g, int color, int x, int y,int width, int height)
{
  int[] rgb = getRGBVColor(color, height);
  for (int i = 0; i < width; i += 4) {
   int nTemp = x + width - (i - x);
   nTemp = nTemp > 4 ? 4 : nTemp;
   g.drawRGB(rgb, 0, 4, i, y, nTemp, height, true);
  }
}

/**
  * 竖直方向获取颜色渐变RGB数组,
  *
  * @param width
  * @return
  */
public final static int[] getRGBVColor(int color, int h)
{
  int[] rgb;
  int RGB_L = h;
  int nRgbData = RGB_L * 4;
  int a;
  rgb = new int[nRgbData];

  for (int i = 0; i < RGB_L; i++) {
   a = i;
   if (a > 255) {
    a = 255;
   }
   int col = color | (a << 24);
   rgb[i * 4] = col;
   rgb[i * 4 + 1] = col;
   rgb[i * 4 + 2] = col;
   rgb[i * 4 + 3] = col;
  }
  return rgb;
}

/*========================================================================*/
/**
  * 水平方向颜色渐变
  */
public final static void drawRGBSRect(Graphics g, int color, int x, int y,int width, int height) {
  int[] rgb = getRGBSColor(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);
  }

}

/**
  * 水平方向获取颜色渐变RGB数组,
  *
  * @param width
  * @return
  */
public final static int[] getRGBSColor(int color, int h) {
  int[] rgb;
  int RGB_L = h;
  int nRgbData = RGB_L * 4;
  rgb = new int[nRgbData];
  int alpha = -127;
  for (int i = 0; i < RGB_L; i++) {
   alpha = -127 + i;
   int col = color | (128 - alpha << 24);
   rgb[i] = col;
   rgb[i + RGB_L] = col;
   rgb[i + RGB_L * 2] = col;
   rgb[i + RGB_L * 3] = col;
  }
  return rgb;
}


}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/laigb/archive/2009/03/24/4019144.aspx