JavaMe開發自適應滾動顯示_java

來源:互聯網
上載者:User

【問題描述】

我們常看到一些滾動顯示的執行個體,比如UC瀏覽器中,顯示網頁的內容。當內容比較多時,採用滾動分頁顯示是合理的。在Canvas中繪圖中,多餘的內容被截斷了。如何?滾動分頁顯示呢?

【原理】

JavaMe中有一個座標變換的功能。當觸發相應的按鍵事件時,我們就讓其顯示相應的頁,並且使捲軸滾動到相應的位置。

【代碼清單】

ShowHelp.javapackage com.token.view;  import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.game.GameCanvas;  import com.token.util.StringDealMethod; import com.token.util.UIController; import com.token.view.components.*;  public class ShowHelp extends GameCanvas {  private UIController controller;  private Graphics graphics;  private Font ft;  private int width;  private int height;    private Menu menu;  private Head head;  private BackGroud backGroud;    private int page = 0;  private int currentPageIndex = 0;  private int bodyHeight;  private int dir = 0;    public ShowHelp(UIController control)   {   super(false);   this.controller=control;   setFullScreenMode(true);      width = getWidth();   height = getHeight();      menu = new Menu(this);   head = new Head(this);   backGroud = new BackGroud(this);   }    public void show()  {   int margin = 0;   graphics = getGraphics();      graphics.clipRect(0,0, width, height);   backGroud.drawBackGroud(this, graphics);   head.drawHead(this, graphics, "協助");   menu.drawMenu(this, graphics, "","返回");   //flushGraphics();      ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_MEDIUM);      String info = "1 滾動分頁顯示;\n"     +"2 滾動分頁顯示;\n"     +"3 滾動分頁顯示;\n"     +"4 滾動分頁顯示;\n"     +"5 滾動分頁顯示;\n"     +"6 滾動分頁顯示;\n"     +"7 滾動分頁顯示;\n"     +"8 滾動分頁顯示;\n"     +"9 滾動分頁顯示;\n"     +"10 滾動分頁顯示;\n"     +"11 滾動分頁顯示;\n"     +"12 滾動分頁顯示;\n"     +"13 滾動分頁顯示;\n"     +"14 滾動分頁顯示;\n"     +"15 滾動分頁顯示;\n"     +"16 滾動分頁顯示;\n"     +"17 滾動分頁顯示;\n"     +"18 滾動分頁顯示;\n"     +"19 滾動分頁顯示;\n"     +"20 滾動分頁顯示;\n"     +"21 滾動分頁顯示;\n"     +"22 滾動分頁顯示;\n"     +"23 滾動分頁顯示;\n"     +"24 滾動分頁顯示;\n"     +"25 滾動分頁顯示;\n"     +"26 滾動分頁顯示;\n"     +"27 滾動分頁顯示;\n"     +"28 滾動分頁顯示;\n"     +"29 滾動分頁顯示;\n"     +"30 滾動分頁顯示;\n"     +"31 滾動分頁顯示;\n"     +"32 滾動分頁顯示;\n"     +"33 滾動分頁顯示;\n"     +"34 滾動分頁顯示;\n";        String info_wrap1[] = StringDealMethod.format(info, width-15, ft);    page = info_wrap1.length*ft.getHeight()/(height-head.menuHeight-menu.menuHeight-2*margin)+1;   bodyHeight = ((int) (height-head.menuHeight-menu.menuHeight)/ft.getHeight())*ft.getHeight();   margin = (height-head.menuHeight-menu.menuHeight-bodyHeight)/2;      graphics.setFont(ft);   graphics.setColor(Color.text);   graphics.clipRect(0, head.menuHeight+margin, width, bodyHeight);   graphics.translate(0, dir*currentPageIndex*bodyHeight);      for(int i=0; i<info_wrap1.length;i++)   {    graphics.drawString(info_wrap1[i],5, i * ft.getHeight()+head.menuHeight+margin, Graphics.TOP|Graphics.LEFT);   }      graphics.translate(0, -dir*currentPageIndex*bodyHeight);      drawScrollBar();   flushGraphics();      //System.out.println(graphics.getTranslateY());     }    private void drawScrollBar()  {   int barHeight = height-head.menuHeight-menu.menuHeight;      graphics.setColor(Color.menuFrame);   graphics.fillRect(width-3, head.menuHeight, 2, barHeight);   graphics.setColor(Color.selectBg);   graphics.fillRect(width-4, head.menuHeight+(currentPageIndex)*barHeight/page, 4, barHeight/page);  }    protected void keyPressed(int keyCode)  {   //System.out.println(keycode);   switch(keyCode)   {    case KeyID.SOFT_RIGHT:    {     String flag = "0";     Object [] args = {flag,""};     controller.handleEvent(UIController.EventID.EVENT_MAIN_SCREEN,args);     break;    }    default:      ;   }      keyCode = getGameAction(keyCode);   //System.out.println(page);      switch(keyCode)   {        case UP:    {     dir = -1;               if(currentPageIndex>0)     {      currentPageIndex--;     }     else     {      //dir = 0;     }          show();     break;         }    case DOWN:    {     dir = -1;     if(currentPageIndex<page-1)     {      currentPageIndex++;     }     else     {       //dir = 0;     }          show();     break;    }   }  }  } 

*UIController請參考JavaMe連載(3)-也說MVC設計模式,此處不再贅述。

【分析】

1 字串拆分

String info_wrap1[] = StringDealMethod.format(info, width-15, ft);
具體請參考JavaMe連載(4)-繪製可自動換行文本

2 避免字截斷

如何在指定的地區內繪製整行文本,而不會因為字型或螢幕高度的改變使文本出現截斷的問題,使文本出現“半截字”的問題呢?

bodyHeight = ((int) (height-head.menuHeight-menu.menuHeight)/ft.getHeight())*ft.getHeight();
經過上述處理後,捲動區域的高度bodyHeight總會是字型高度的整數倍,這樣就不會出現上述字截斷的問題了。

3 繪製文本

for(int i=0; i<info_wrap1.length;i++) {  graphics.drawString(info_wrap1[i],5, i * ft.getHeight()+head.menuHeight+margin, Graphics.TOP|Graphics.LEFT); } 

4 座標變換

graphics.clipRect(0, head.menuHeight+margin, width, bodyHeight); graphics.translate(0, dir*currentPageIndex*bodyHeight); 

文本繪製完成後,將座標變換回來。

graphics.translate(0, -dir*currentPageIndex*bodyHeight); 

5 繪製捲軸

private void drawScrollBar() {  int barHeight = height-head.menuHeight-menu.menuHeight;    graphics.setColor(Color.menuFrame);  graphics.fillRect(width-3, head.menuHeight, 2, barHeight);  graphics.setColor(Color.selectBg);  graphics.fillRect(width-4, head.menuHeight+(currentPageIndex)*barHeight/page, 4, barHeight/page); } 

6 事件處理

當檢測到按鍵事件後,進行翻頁操作。

protected void keyPressed(int keyCode) {  //System.out.println(keycode);  switch(keyCode)  {   case KeyID.SOFT_RIGHT:   {    String flag = "0";    Object [] args = {flag,""};    controller.handleEvent(UIController.EventID.EVENT_MAIN_SCREEN,args);    break;   }   default:    ;  }     keyCode = getGameAction(keyCode);  //System.out.println(page);     switch(keyCode)  {      case UP:   {    dir = -1;               if(currentPageIndex>0)    {     currentPageIndex--;    }    else    {     //dir = 0;    }        show();    break;       }   case DOWN:   {    dir = -1;    if(currentPageIndex<page-1)    {     currentPageIndex++;    }    else    {      //dir = 0;    }         show();    break;   }  } } 

本例方法能自適應的檢測螢幕的寬度和長度,依據字型的大小,對文本進行分頁,滾動顯示,實現效果如圖1所示:

 

圖 滾動顯示效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.