JavaMe開辟自順應轉動顯示。本站提示廣大學習愛好者:(JavaMe開辟自順應轉動顯示)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaMe開辟自順應轉動顯示正文
【成績描寫】
我們常看到一些轉動顯示的實例,好比UC閱讀器中,顯示網頁的內容。當內容比擬多時,采取轉動分頁顯示是公道的。在Canvas中畫圖中,過剩的內容被截斷了。若何完成轉動分頁顯示呢?
【道理】
JavaMe中有一個坐標變換的功效。當觸發響應的按鍵事宜時,我們就讓其顯示響應的頁,而且使轉動條轉動到響應的地位。
【代碼清單】
ShowHelp.java
package 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所示:
圖 轉動顯示後果