j2me颜色渐变的问题
我自己写了一个颜色渐变的方法,如下:
public static void fillLinerRect(Graphics gr, int x, int y, int w, int h,
int startcolor, int endcolor)
{
int r_start = ((startcolor >>> 16) & 0xFF);
int g_start = ((startcolor >>> 8) & 0xFF);
int b_start = ((startcolor >>> 0) & 0xFF);
int r_end = ((endcolor >>> 16) & 0xFF);
int g_end = ((endcolor >>> 8) & 0xFF);
int b_end = ((endcolor >>> 0) & 0xFF);
int c_r = r_end - r_start;
int c_g = g_end - g_start;
int c_b = b_end - b_start;
for (int i = 0; i < h; i++)
{
int r = r_start + c_r * i / h;
int g = g_start + c_g * i / h;
int b = b_start + c_b * i / h;
gr.setColor(r, g, b);
gr.fillRect(x, y + i, x + w, y + i);
}
}
但是实际的渐变效果不平滑,很不好,在网上搜到一个算法,如下:
/**
* 绘制渐变色选择条
* @param g
* @param color
* @param x
* @param y
* @param width
* @param height
*/
public final static void drawRGBRect(Graphics g, int color, int x, int y,int width, int height) {
int[] rgb = getRGBColor(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[] getRGBColor(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;
}
这个实现起来效果很平滑,但有一点我怎么改都没改好,就是这个算法实现的效果是从左往右颜色渐变,但我想实现的是从上往下,改了一段时间没改好,哪位高手帮改一下,让效果变成从上往下颜色渐变,另这个算法能在midp1.0上面运行吗