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

2014-11-24 07:56:20 · 作者: · 浏览: 2
");
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 {
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 {
currentPageIndex++;
}
else
{
//dir = 0;
}

show();
break;
}
}
}

}

*UIController请参考JavaMe连载(3)-也说MVC设计模式,此处不再赘述。

【分析】

1 字符串拆分

[html] String info_wrap1[] = StringDealMethod.format(info, width-15, ft);
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 绘制文本

[html] for(int i=0; i {
graphics.drawString(info_wrap1[i