|
J2ME图片的缩放
|
Author: 一滴蔚蓝色 | Date: 2007-02-22 |
View: 2775 |
开发技术 - 程序设计 | Digg:
1
|
最近忽然萌生了想在手机上制作一款第一人称的FPS游戏,首先遇到的一个问题就是图片的缩放,经过网上的搜索和自己的实践,产生了如下的方法。
|
public static final Image scale (Image srcImage, int newW, int newH) {
int srcW = srcImage.getWidth();
int srcH = srcImage.getHeight();
//先做水平方向上的伸缩变换
Image tmp = Image.createImage(newW, srcH);
Graphics g = tmp.getGraphics();
for (int x = 0; x < newW; x++) {
g.setClip(x, 0, 1, srcH);
//按比例放缩
g.drawImage(srcImage,x-x*srcW/newW,0,Graphics.LEFT | Graphics.TOP);
}
//再做垂直方向上的伸缩变换
Image dst = Image.createImage(newW, newH);
g = dst.getGraphics();
for (int y = 0; y < newH; y++) {
g.setClip(0, y, newW, 1);
//按比例放缩
g.drawImage(tmp,0,y-y*srcH/newH,Graphics.LEFT | Graphics.TOP);
}
return dst;
} |
我分别在诺基亚3250和6230i上面测试了这段代码,使用的图片尺寸为60*60,先后将其缩放为30*30和120*120,缩小到30*30的时候,速度很快,放大到120*120的时候感觉稍有停顿,不过总的来说,还是完全可以在游戏中使用了,希望能够早日完成我的目标:)
更多阅读:
|
|
最近更新 ( 2008-09-10 )
|