关于J2me手游fillrect和drawImage效率问题

前一阵子两个同事,做游戏中按钮的时候,一个说用矩形去画按钮,另一位说让美术出图,效果一样,最有就讨论起这两者的效率问题,说画矩形并填充颜色也很耗内存和资源。因为是一个一个点打出来的,今天做了下测试,用一张纯黑的图,重复画N次,2用fillrect画黑矩形

程序如下:

 

 import java.io.IOException; import javax.microedition.amms.control.imageeffect.WhiteBalanceControl; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.game.GameCanvas; public class MyCanvas extends GameCanvas implements Runnable { public static Graphics graphics ; public int num = 10000; protected MyCanvas(boolean suppressKeyEvents) { super(suppressKeyEvents); graphics = this.getGraphics(); this.setFullScreenMode(true); } public void run() { Image image = null; long startTime = System.currentTimeMillis(); try { image = Image.createImage("/black300.png"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < num; i++) { graphics.drawImage(image,0,0,0); } long endTime = System.currentTimeMillis(); System.out.println("drawImage "+(endTime-startTime)); long fillStartTime = System.currentTimeMillis(); graphics.setColor(0); for (int i = 0; i < num; i++) { graphics.fillRect(0,0,300,300); } long fillEndTime = System.currentTimeMillis(); System.out.println("fillRece "+(fillEndTime-fillStartTime)); long StringStartTime = System.currentTimeMillis(); for (int i = 0; i < num; i++) { graphics.drawString("HelloWorld",0,0,0); } long StringEndTime = System.currentTimeMillis(); System.out.println("drawString "+(StringEndTime-StringStartTime)); } }

结果很明显,都是50*50的像素时,结果为:250 31

                        200*200时:2750 469

                        300*300时:6312 1016

很明显,画图片是最耗时间的。。。

后面又加入了drawString的测试:发现drawString 没有想象中的耗资源,跟fillrect比较接近,在区域比较少的情况下。

所以还是少用drawImage,多用fillRect,靠自己吧。。。哈哈哈

你可能感兴趣的:(关于J2me手游fillrect和drawImage效率问题)