关键字: J2ME
设计游戏的朋友经常会遇到对图片资源的操作。今天浏览web发现此贴不错。所以抄过这边来
原文: www.linuxmine.com/45911.html
无论在midp1.0还是在 midp2.0中,系统都没有给我们提供对图片进行伸缩操作的api.但是其实我们只要在程序代码中略施小计,就能达到这个效果,只是效果要比美术做出来的图片,呵呵,差多啦,同时也会造成性能损失。伸缩图片的构造原理就是简单沿x,y轴按比例放缩,比如说我们需要把一张16*16的png图片转化成一张 32*32的图片,那么我们可以先对该图片做一个水平方向上的拉伸操作,然后再把水平拉伸后的图片按垂直方向再做一次拉伸操作。做拉伸操作时,比如水平方向上,我们需要构造一张32*16的mutable Image,获取其Graphics,利用该Graphics,绘制该mutable Graphics的每一列像素,这一列像素就来自于原始图片中的按比例对应的某一列像素。垂直方向上的拉伸操作也是如法炮制。因为是一种按比例的对应关系,图像的缩小操作也可按该办法进行。
效果如下图所示
好了,我们来看代码
java 代码
1
.
import
javax.microedition.lcdui.Graphics;
2 . import javax.microedition.lcdui.Image;
3 .
4 . /**
5. * 图像工具类
6. * @author Jagie
7. *
8. */
9 .
10 . public class ImageUtil {
11 .
12 . /**
13. * 图像放缩方法
14. * @param srcImage 原始的Image对象
15. * @param newW 放缩后的Image的宽度
16. * @param newH 放缩后的Image的高度
17. * @return 放缩后的Image对象
18. */
19 .
20 . public static final Image scale (Image srcImage, int newW, int newH) {
21 . int srcW = srcImage.getWidth();
22 . int srcH = srcImage.getHeight();
23 . // 先做水平方向上的伸缩变换
24 . Image tmp = Image.createImage(newW, srcH);
25 . Graphics g = tmp.getGraphics();
26 .
27 . for ( int x = 0 ; x < newW; x ++ ) {
28 . g.setClip(x, 0 , 1 , srcH);
29 . // 按比例放缩
30 . g.drawImage(srcImage,x - x * srcWnewW, 0 ,Graphics.LEFT | Graphics.TOP);
31 .
32 . }
33 .
34 . // 再做垂直方向上的伸缩变换
35 . Image dst = Image.createImage(newW, newH);
36 . g = dst.getGraphics();
37 .
38 . for ( int y = 0 ; y < newH; y ++ ) {
39 . g.setClip( 0 , y, newW, 1 );
40 . // 按比例放缩
41 . g.drawImage(tmp, 0 ,y - y * srcHnewH,Graphics.LEFT | Graphics.TOP);
42 .
43 . }
44 .
45 . return dst;
46 . }
47 .
48 .
49 . }
50 .
51 . 也许有同学会提出疑问,既然是按x,y方向按等比例放缩,那我写成这样岂不是代码更简洁:
52 .
53 . public static final Image scale2(Image srcImage, int newW, int newH) {
54 . int srcW = srcImage.getWidth();
55 . int srcH = srcImage.getHeight();
56 .
57 . Image dst = Image.createImage(newW,newH);
58 . Graphics g = dst.getGraphics();
59 . for ( int x = 0 ; x < newW; x ++ ) {
60 . for ( int y = 0 ; y < newH; y ++ ) {
61 . g.setClip(x, y, 1 , 1 );
62 . g.drawImage(srcImage, x - x * srcWnewW, y - y * srcH / newH, Graphics.LEFT
63 . | Graphics.TOP);
64 .
65 . }
66 .
67 . }
68 .
69 . return dst;
70 . }
2 . import javax.microedition.lcdui.Image;
3 .
4 . /**
5. * 图像工具类
6. * @author Jagie
7. *
8. */
9 .
10 . public class ImageUtil {
11 .
12 . /**
13. * 图像放缩方法
14. * @param srcImage 原始的Image对象
15. * @param newW 放缩后的Image的宽度
16. * @param newH 放缩后的Image的高度
17. * @return 放缩后的Image对象
18. */
19 .
20 . public static final Image scale (Image srcImage, int newW, int newH) {
21 . int srcW = srcImage.getWidth();
22 . int srcH = srcImage.getHeight();
23 . // 先做水平方向上的伸缩变换
24 . Image tmp = Image.createImage(newW, srcH);
25 . Graphics g = tmp.getGraphics();
26 .
27 . for ( int x = 0 ; x < newW; x ++ ) {
28 . g.setClip(x, 0 , 1 , srcH);
29 . // 按比例放缩
30 . g.drawImage(srcImage,x - x * srcWnewW, 0 ,Graphics.LEFT | Graphics.TOP);
31 .
32 . }
33 .
34 . // 再做垂直方向上的伸缩变换
35 . Image dst = Image.createImage(newW, newH);
36 . g = dst.getGraphics();
37 .
38 . for ( int y = 0 ; y < newH; y ++ ) {
39 . g.setClip( 0 , y, newW, 1 );
40 . // 按比例放缩
41 . g.drawImage(tmp, 0 ,y - y * srcHnewH,Graphics.LEFT | Graphics.TOP);
42 .
43 . }
44 .
45 . return dst;
46 . }
47 .
48 .
49 . }
50 .
51 . 也许有同学会提出疑问,既然是按x,y方向按等比例放缩,那我写成这样岂不是代码更简洁:
52 .
53 . public static final Image scale2(Image srcImage, int newW, int newH) {
54 . int srcW = srcImage.getWidth();
55 . int srcH = srcImage.getHeight();
56 .
57 . Image dst = Image.createImage(newW,newH);
58 . Graphics g = dst.getGraphics();
59 . for ( int x = 0 ; x < newW; x ++ ) {
60 . for ( int y = 0 ; y < newH; y ++ ) {
61 . g.setClip(x, y, 1 , 1 );
62 . g.drawImage(srcImage, x - x * srcWnewW, y - y * srcH / newH, Graphics.LEFT
63 . | Graphics.TOP);
64 .
65 . }
66 .
67 . }
68 .
69 . return dst;
70 . }
这种做法效果上和前者无异,但是并不可取,只要算算它的时间复杂度就知道,基本上是前者的平方。在我的机器上,做一次全屏幕的放缩操作,前者耗时60ms,而后者耗时7150ms。
其实上面的做法很简单,以后有需求可以整理成一个控件,这样就可以很方便的调用,与学习。