当前位置: 代码迷 >> J2ME >> j2me图片渐变成效(转)
  详细解决方案

j2me图片渐变成效(转)

热度:908   发布时间:2016-04-17 21:24:50
j2me图片渐变效果(转)
  1. 01.import java.io.IOException;  02.  03.import javax.microedition.lcdui.Canvas;  04.import javax.microedition.lcdui.Graphics;  05.import javax.microedition.lcdui.Image;  06.  07.//ShadowCanvas.java   08./**  09. * @author boolean liuhao137310@163.com:  10. * @version 创建时间:2009-2-7 下午02:01:27  11. * @TODO 图片的渐变效果 在第一张图片的基础上渐变出第二张图片 12. */  13.class ShadowCanvas extends Canvas implements Runnable {   14.    int w, h;   15.  16.    /**原始图片*/   17.    Image img2;  18.      19.    /**渐变图片*/  20.    Image srcImage;  21.  22.    // 原始图片的像素数组    23.    int[] srcRgbImage;   24.  25.    // 渐变图片的像素数组    26.    int[] shadowRgbImage;   27.  28.    int imgWidth, imgHeight;   29.  30.    int count;   31.  32.    /**原始图片只画一次即可 此变量记录是否是第一次画原始图片*/  33.    private boolean first = true;  34.      35.    public ShadowCanvas() {   36.        w = this.getWidth();   37.        h = this.getHeight();   38.        try {   39.            srcImage = Image.createImage("/logoBg.png");   40.            img2 = Image.createImage("/test_bg.png");  41.        } catch (IOException e) {   42.            e.printStackTrace();   43.        }   44.        imgWidth = srcImage.getWidth();   45.        imgHeight = srcImage.getHeight();   46.        // 制造原始图片的像素数组,用一个int来代表每一个像素,按位表示方式是:0xAARRGGBB    47.        srcRgbImage = new int[imgWidth * imgHeight];   48.        // 获取原始图片的所有像素,参见MIDP APPI文档    49.        srcImage.getRGB(srcRgbImage, 0, imgWidth, 0, 0, imgWidth, imgHeight);   50.  51.        shadowRgbImage = new int[srcRgbImage.length];   52.  53.        System.arraycopy(srcRgbImage, 0, shadowRgbImage, 0,   54.                shadowRgbImage.length);   55.  56.        // 渐变图片的所有像素一开始都是全透明的    57.        for (int i = 0; i < shadowRgbImage.length; i++) {   58.            shadowRgbImage[i] &= 0x00ffffff;   59.        }   60.  61.        new Thread(this).start();   62.  63.    }   64.  65.    public void paint(Graphics g) {   66.//        g.setColor(0, 0, 0);    67.//        g.fillRect(0, 0, w, h);    68.        if(first){  69.            g.drawImage(img2, 0, 0, 0);  70.            first = false;  71.        }  72.        // 绘制渐变图片    73.        g.drawRGB(shadowRgbImage, 0, imgWidth, (w - imgWidth) / 2,   74.                (h - imgHeight) / 2, imgWidth, imgHeight, true);   75.  76.        g.setColor(0, 255, 0);   77.        g.drawString("count=" + count, w / 2, 30, Graphics.HCENTER   78.                | Graphics.TOP);   79.    }   80.  81.    public void run() {   82.        while (true) {   83.  84.            boolean changed = false;   85.            // 改变渐变图片的每一个像素    86.            for (int i = 0; i < shadowRgbImage.length; i++) {   87.                // 获取渐变图片的某一像素的alpha值    88.                int alpha = (shadowRgbImage[i] & 0xff000000) >>> 24;   89.                // 原始图片的对应像素的alpha值    90.                int oldAlpha = (srcRgbImage[i] & 0xff000000) >>> 24;   91.  92.                if (alpha < oldAlpha) {   93.                    // alpha值++  写的太精妙了 把移位用到了极致   94.                    shadowRgbImage[i] = ((alpha + 1) << 24)   95.                            | (shadowRgbImage[i] & 0x00ffffff);   96.                    changed = true;   97.                }   98.            }   99.  100.            try {   101.                Thread.sleep(10);   102.            } catch (InterruptedException e) {   103.                e.printStackTrace();   104.            }   105.            count ++;   106.            repaint();   107.            // 当所有像素的alpha值都达到原始值后,线程运行结束    108.            if (!changed) {   109.                System.out.println("over");   110.                break;   111.            }   112.        }   113.  114.    }   115.  116.}   
    ?引用:http://blog.csdn.net/hearrt/article/details/3867521
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!