java 自适应屏幕_JavaMe开发:自适应滚动显示

【问题描述】

我们常看到一些滚动显示的实例,比如UC浏览器中,显示网页的内容。当内容比较多时,采用滚动分页显示是合理的。在Canvas中绘图中,多余的内容被截断了。如何实现滚动分页显示呢?

【原理】

JavaMe中有一个坐标变换的功能。当触发相应的按键事件时,我们就让其显示相应的页,并且使滚动条滚动到相应的位置。

【代码清单】

ShowHelp.java

packagecom.token.view;

importjavax.microedition.lcdui.Font;

importjavax.microedition.lcdui.Graphics;

importjavax.microedition.lcdui.game.GameCanvas;

importcom.token.util.StringDealMethod;

importcom.token.util.UIController;

importcom.token.view.components.*;

publicclassShowHelpextendsGameCanvas

{

privateUIController controller;

privateGraphics graphics;

privateFont ft;

privateintwidth;

privateintheight;

privateMenu menu;

privateHead head;

privateBackGroud backGroud;

privateintpage =0;

privateintcurrentPageIndex =0;

privateintbodyHeight;

privateintdir =0;

publicShowHelp(UIController control)

{

super(false);

this.controller=control;

setFullScreenMode(true);

width = getWidth();

height = getHeight();

menu =newMenu(this);

head =newHead(this);

backGroud =newBackGroud(this);

}

publicvoidshow()

{

intmargin =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(inti=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());

}

privatevoiddrawScrollBar()

{

intbarHeight = 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);

}

protectedvoidkeyPressed(intkeyCode)

{

//System.out.println(keycode);

switch(keyCode)

{

caseKeyID.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)

{

caseUP:

{

dir = -1;

if(currentPageIndex>0)

{

currentPageIndex--;

}

else

{

//dir = 0;

}

show();

break;

}

caseDOWN:

{

dir = -1;

if(currentPageIndex

{

currentPageIndex++;

}

else

{

//dir = 0;

}

show();

break;

}

}

}

}

*UIController请参考

【分析】

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(inti=0; 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 绘制滚动条

privatevoiddrawScrollBar()

{

intbarHeight = 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 事件处理

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

protectedvoidkeyPressed(intkeyCode)

{

//System.out.println(keycode);

switch(keyCode)

{

caseKeyID.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)

{

caseUP:

{

dir = -1;

if(currentPageIndex>0)

{

currentPageIndex--;

}

else

{

//dir = 0;

}

show();

break;

}

caseDOWN:

{

dir = -1;

if(currentPageIndex

{

currentPageIndex++;

}

else

{

//dir = 0;

}

show();

break;

}

}

}

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

bbf50c31bfae72a12950e805351b2389.png

图1 滚动显示效果

【系列文章】

【责任编辑:小林 TEL:(010)68476606】

点赞 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值