JavaMe開發(fā)自適應(yīng)滾動(dòng)顯示
【問題描述】
我們??吹揭恍L動(dòng)顯示的實(shí)例,比如UC瀏覽器中,顯示網(wǎng)頁的內(nèi)容。當(dāng)內(nèi)容比較多時(shí),采用滾動(dòng)分頁顯示是合理的。在Canvas中繪圖中,多余的內(nèi)容被截?cái)嗔恕H绾螌?shí)現(xiàn)滾動(dòng)分頁顯示呢?
【原理】
JavaMe中有一個(gè)坐標(biāo)變換的功能。當(dāng)觸發(fā)相應(yīng)的按鍵事件時(shí),我們就讓其顯示相應(yīng)的頁,并且使?jié)L動(dòng)條滾動(dòng)到相應(yīng)的位置。
【代碼清單】
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 滾動(dòng)分頁顯示;\n"
+"2 滾動(dòng)分頁顯示;\n"
+"3 滾動(dòng)分頁顯示;\n"
+"4 滾動(dòng)分頁顯示;\n"
+"5 滾動(dòng)分頁顯示;\n"
+"6 滾動(dòng)分頁顯示;\n"
+"7 滾動(dòng)分頁顯示;\n"
+"8 滾動(dòng)分頁顯示;\n"
+"9 滾動(dòng)分頁顯示;\n"
+"10 滾動(dòng)分頁顯示;\n"
+"11 滾動(dòng)分頁顯示;\n"
+"12 滾動(dòng)分頁顯示;\n"
+"13 滾動(dòng)分頁顯示;\n"
+"14 滾動(dòng)分頁顯示;\n"
+"15 滾動(dòng)分頁顯示;\n"
+"16 滾動(dòng)分頁顯示;\n"
+"17 滾動(dòng)分頁顯示;\n"
+"18 滾動(dòng)分頁顯示;\n"
+"19 滾動(dòng)分頁顯示;\n"
+"20 滾動(dòng)分頁顯示;\n"
+"21 滾動(dòng)分頁顯示;\n"
+"22 滾動(dòng)分頁顯示;\n"
+"23 滾動(dòng)分頁顯示;\n"
+"24 滾動(dòng)分頁顯示;\n"
+"25 滾動(dòng)分頁顯示;\n"
+"26 滾動(dòng)分頁顯示;\n"
+"27 滾動(dòng)分頁顯示;\n"
+"28 滾動(dòng)分頁顯示;\n"
+"29 滾動(dòng)分頁顯示;\n"
+"30 滾動(dòng)分頁顯示;\n"
+"31 滾動(dòng)分頁顯示;\n"
+"32 滾動(dòng)分頁顯示;\n"
+"33 滾動(dòng)分頁顯示;\n"
+"34 滾動(dòng)分頁顯示;\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請(qǐng)參考JavaMe連載(3)-也說MVC設(shè)計(jì)模式,此處不再贅述。
【分析】
1 字符串拆分
String info_wrap1[] = StringDealMethod.format(info, width-15, ft);
具體請(qǐng)參考JavaMe連載(4)-繪制可自動(dòng)換行文本
2 避免字截?cái)?/strong>
如何在指定的區(qū)域內(nèi)繪制整行文本,而不會(huì)因?yàn)樽煮w或屏幕高度的改變使文本出現(xiàn)截?cái)嗟膯栴},使文本出現(xiàn)“半截字”的問題呢?
bodyHeight = ((int) (height-head.menuHeight-menu.menuHeight)/ft.getHeight())*ft.getHeight();
經(jīng)過上述處理后,滾動(dòng)區(qū)域的高度bodyHeight總會(huì)是字體高度的整數(shù)倍,這樣就不會(huì)出現(xiàn)上述字截?cái)嗟膯栴}了。
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 坐標(biāo)變換
graphics.clipRect(0, head.menuHeight+margin, width, bodyHeight); graphics.translate(0, dir*currentPageIndex*bodyHeight);
文本繪制完成后,將坐標(biāo)變換回來。
graphics.translate(0, -dir*currentPageIndex*bodyHeight);
5 繪制滾動(dòng)條
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 事件處理
當(dāng)檢測(cè)到按鍵事件后,進(jìn)行翻頁操作。
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;
}
}
}
本例方法能自適應(yīng)的檢測(cè)屏幕的寬度和長(zhǎng)度,依據(jù)字體的大小,對(duì)文本進(jìn)行分頁,滾動(dòng)顯示,實(shí)現(xiàn)效果如圖1所示:

圖 滾動(dòng)顯示效果
- Javascript實(shí)現(xiàn)的分頁函數(shù)
- Javascript實(shí)現(xiàn)的分頁函數(shù)
- javascript+xml技術(shù)實(shí)現(xiàn)分頁瀏覽
- javascript 支持頁碼格式的分頁類
- javascript 新聞標(biāo)題靜態(tài)分頁代碼 (無刷新)
- Jquery與JS兩種方法仿twitter/新浪微博 高度自適應(yīng)無縫滾動(dòng)實(shí)現(xiàn)代碼
- javascript分頁代碼(當(dāng)前頁碼居中)
- Java(基于Struts2) 分頁實(shí)現(xiàn)代碼
- java調(diào)用oracle分頁存儲(chǔ)過程示例
- Java web velocity分頁宏示例
- javascript實(shí)現(xiàn)簡(jiǎn)單的分頁特效
- 純javascript實(shí)現(xiàn)分頁(兩種方法)
- JavaMe開發(fā)繪制可自動(dòng)換行文本
- JavaMe開發(fā)繪制文本框TextEdit
相關(guān)文章
詳解OAuth2 Token 一定要放在請(qǐng)求頭中嗎
這篇文章主要介紹了詳解OAuth2 Token 一定要放在請(qǐng)求頭中嗎,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
解決sharding JDBC 不支持批量導(dǎo)入問題
這篇文章主要介紹了解決sharding JDBC 不支持批量導(dǎo)入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
JDBC+GUI實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JDBC+GUI實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
IDEA搭建多模塊的Maven項(xiàng)目方式(相互依賴)
這篇文章主要介紹了IDEA搭建多模塊的Maven項(xiàng)目方式(相互依賴),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交
基于SpringBoot框架來開發(fā)業(yè)務(wù)后臺(tái)項(xiàng)目時(shí),接口重復(fù)提交是一個(gè)常見的問題,本文主要介紹了SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
SpringBoot ApplicationEvent之事件發(fā)布與監(jiān)聽機(jī)制詳解
本文將深入探討SpringBoot的事件機(jī)制,介紹其核心概念、實(shí)現(xiàn)方法及最佳實(shí)踐,幫助開發(fā)者構(gòu)建更加靈活、可維護(hù)的應(yīng)用架構(gòu),希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
springboot配置內(nèi)存數(shù)據(jù)庫(kù)H2教程詳解
這篇文章主要介紹了springboot配置內(nèi)存數(shù)據(jù)庫(kù)H2的詳細(xì)教程,需要的朋友可以參考下2017-07-07
java實(shí)現(xiàn)識(shí)別二維碼圖片功能方法詳解與實(shí)例源碼
這篇文章主要介紹了java實(shí)現(xiàn)識(shí)別二維碼圖片,java無法識(shí)別二維碼情況下對(duì)二維碼圖片調(diào)優(yōu)功能方法與實(shí)例源碼,需要的朋友可以參考下2022-12-12

