您可以使用Pngcrush它的主要目的是通过尝试各种压缩级别和PNG过滤器方法来减小PNG IDAT数据流的大小。如果宽度或长度的大小非常大并且您打算将其绘制在画布上,创建图像后,您可以在canvas的paint方法中使用Graphics的drawRegion方法在其上绘制所需的图像。您可以通过更改参数来更改绘制的图像(例如当用户按键时) drawRegion() 方法和重绘画布:
public class CanvasButterfly extends Canvas implements ... {
private int ix, iy;
//image
private Image picture;
/*
* Constructor
*/
public CanvasButterfly() {
init();
}
/* Function : paint(Graphics)
* Description : This method is used for rendering Graphics
* Input : Graphics
* return : Void
*/
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
if (picture != null) {
g.drawRegion(picture, ix, iy,
picture.getWidth() - ix, picture.getHeight() - iy,
Sprite.TRANS_NONE, 0, 0, Graphics.TOP | Graphics.LEFT);
}
}
/* Function : moveImage(int)
* Description : This method handle Canvas events
* Input : void
* return : Void
*/
private void moveImage(int keyCode) {
int key = -1;
try {
key = getGameAction(keyCode);
} catch (Exception ex) {
key = keyCode;
}
switch (key) {
case Canvas.DOWN:
iy = Math.min(iy + 1,picture.getHeight());
break;
case Canvas.UP:
iy = Math.max(iy - 1,0);
break;
case Canvas.LEFT:
ix = Math.max(ix - 1,0);
break;
case Canvas.RIGHT:
ix = Math.min(ix + 1,picture.getWidth());
break;
}
}
//keyPressed
public void keyPressed(int keyCode) {
moveImage(keyCode);
repaint();
}
//keyRepeated
public void keyRepeated(int keyCode) {
moveImage(keyCode);
repaint();
}
/* Function : init()
* Description : This method initialized the class objects
* Input : void
* return : Void
*/
private void init() {
//
ix = ...
iy = ...
try {
picture= Image.createImage("/" + image + ".png");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
这里,第一次从画布中的坐标(ix,iy)绘制图片。