2012-01-23 25 views
2

我有一個大圖像。該圖像有一些用戶的文字。所以我不能調整圖像以適應更小的屏幕。圖像比設備屏幕尺寸大得多。我的意圖是在畫布上繪製完整的圖像,而不包含大小。我想在用戶按鍵事件(左,右,上,下)之後逐位移動圖像,如滾動。如何在畫布上繪製大圖像並移動至關鍵事件

我可以通過畫布繪製圖像: -

g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT); 

我不知道如何根據關鍵事件來使圖像的其他部分一樣滾動行動。
我見過很多j2me遊戲都有這樣的功能。
在哪裏查找此信息?

+0

檢查[圖形中的剪輯和翻譯API](http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/lcdui/Graphics.html#clip )。另一個值得期待的API是[TiledLayer](http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/lcdui/game/TiledLayer.html) – gnat

回答

1

您可以使用Pngcrush其主要目的是通過嘗試不同的壓縮級別和寬度或長度的PNG過濾methods.If大小減少PNG IDAT數據流的規模是非常大的,你打算後繪製在畫布上,創建圖像,可以在canvas的繪製方法中使用Graphics的drawRegion方法在其上繪製所需的圖像。您可以通過更改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)在畫布中繪製圖片。