您的位置:首页 > 其它

转:J2ME 颜色渐变

2011-07-22 16:04 239 查看
转:J2ME 颜色渐变 2010-04-27 11:33用程序实现绘制渐变颜色大体思想就是用渐变的颜色值平铺,通常考虑上速度和性能就要分两种了:
一:按块平铺,就是通常的设置一个块绘制好了在绘制另一个块(这里的块大于一个像素哟,不然就没必要分了)
二:按像素区域填充RGB值,
优缺点:第一种优点:速度快、占用内存少、支持机型多便于移植,缺点:效果不是最理想的
第二种优点:效果很好渐变过渡平滑,缺点:速度慢、占用内存大、不方便移植
但在小型设备商我们一般都要考虑内存和速度,所以我们需要实际考虑;在这里我只作出第一种的源码和实现方式:如下:
一:Android实现:

private void DrawList(Paint paint,Canvas canvas,int x,int y, int w,int h, int colorBegin,int colorEnd) {
//具体实现就不用多说了,大致就是把RGB各个颜色值进行加减运算,然后组合成一种RGB颜色值,这个方法是我封装好的现成的
int r0 = (colorBegin >> 16) & 0xff;
int r1 = (colorEnd >> 16) & 0xff;
int g0 = (colorBegin >> 8)& 0xff;
int g1 = (colorEnd >> 8) & 0xff;
int b0 = (colorBegin) & 0xff;
int b1 = (colorEnd) & 0xff;
int F,r,g,b;
for(int i =0;i<h;++i){
F = (i << 16)/h;
r = r0 +((F * (r1-r0)) >> 16);
g = g0 +((F * (g1-g0)) >> 16);
b = b0 +((F * (b1-b0)) >> 16);
paint.setARGB(0xff, r, g, b);
canvas.drawRect(x, y+i, 320,y+i+1, paint);
}
}
二:j2me实现:原理都是第一种
/**绘制渐变颜色函数
* @param graphics 画笔
* @param x 绘制起始位置x坐标
* @param y 绘制起始位置y坐标
* @param w 绘制列表的宽度
* @param h 绘制列表的高度
* @param colorBegin 列表填充起始位置颜色
* @param colorEnd 列表填充结束位置颜色
*/
public void DrawList(Graphics graphics,int x,int y, int w,int h, int colorBegin,int colorEnd) {
if(colorBegin == colorEnd){
graphics.setColor(colorBegin);
graphics.fillRect(x, y, w, h);
}else{
int r0 = (colorBegin >> 16) & 0xff;
int r1 = (colorEnd >> 16) & 0xff;
int g0 = (colorBegin >> 8)& 0xff;
int g1 = (colorEnd >> 8) & 0xff;
int b0 = (colorBegin) & 0xff;
int b1 = (colorEnd) & 0xff;
int F,r,g,b;
for(int i =0;i<h;++i){
F = (i << 16)/h;
r = r0 +((F * (r1-r0)) >> 16);
g = g0 +((F * (g1-g0)) >> 16);
b = b0 +((F * (b1-b0)) >> 16);
graphics.setColor(r<<16|g<<8|b);
graphics.drawRect(x, y+i, w, 1);
}
}
}
当然这里只是列出上下的渐变,关于圆渐变、侧方向渐变当然也可以用同样的方式来实现的,但是如果很复杂的渐变我觉得你还是用效果图来得直接(如果你 是一个程序员就应该用代码试试这些)。
原文:http://hi.baidu.com/fox_message/blog/item/757574016aa6011a728b65da.html
--------------------------------------------------------------------------------------------------------------------------------
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;
}

}

原文:http://hi.baidu.com/zj360202/blog/item/d88f284b0b00fcfb82025c8f.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: