j2me 颜色渐变与图像透明效果
2009-02-24 14:15
495 查看
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;
}
}
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;
}
}
相关文章推荐
- Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度
- Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度
- CSS3.0特性—图像的背景 背景透明颜色 图像的透明 颜色的渐变 圆角 阴影
- 图像处理详解之颜色渐变效果
- Android Gallary图片游览效果--透明的倒影,层叠的图片,渐变的颜色透明度
- 一个模仿图片透明渐变做的表格颜色渐变效果 js脚本特效
- Android Gallary图片游览效果--透明的倒影,层叠的图片,渐变的颜色透明度
- Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度
- Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度
- 一个模仿图片透明渐变做的表格颜色渐变效果 js脚本特效
- Android之绚丽的图片游览效果--有点像W7效果,透明的倒影,层叠的图片,渐变的颜色透明度 .
- 一个模仿图片透明渐变做的表格颜色渐变效果
- j2me 颜色渐变与透明处理
- JavaScript 颜色梯度和渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- UITableView头部ImageView下拉放大效果,导航栏透明渐变
- Javacript实现颜色渐变的效果
- 图像处理之透明混合 - Alpha Blending效果
- 颜色渐变效果
- j2me图片渐变效果(转)