JavaMe连载(6)-自适应滚动显示 (四)

2014-11-24 07:56:20 · 作者: · 浏览: 1
],5, i * ft.getHeight()+head.menuHeight+margin, Graphics.TOP|Graphics.LEFT);
}
for(int i=0; i {
graphics.drawString(info_wrap1[i],5, i * ft.getHeight()+head.menuHeight+margin, Graphics.TOP|Graphics.LEFT);
}

4 坐标变换

[html] graphics.clipRect(0, head.menuHeight+margin, width, bodyHeight);
graphics.translate(0, dir*currentPageIndex*bodyHeight);
graphics.clipRect(0, head.menuHeight+margin, width, bodyHeight);
graphics.translate(0, dir*currentPageIndex*bodyHeight);
文本绘制完成后,将坐标变换回来。

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

5 绘制滚动条

[html] 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);
}
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 事件处理

当检测到按键事件后,进行翻页操作。

[html] 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 {
currentPageIndex++;
}
else
{
//dir = 0;
}

show();
break;
}
}
}
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 {
currentPageIndex++;
}
else
{
//dir = 0;
}

show();
break;
}
}
}

本例方法能自适应的检测屏幕的宽度和长度,依据字体的大小,对文本进行分页,滚动显示,实现效果如图1所示:

\

图1 滚动显示效果



摘自 tandesir的专栏