- 浏览: 298362 次
- 性别:
- 来自: 上海
最新评论
-
再_见孙悟空:
写的不错
Dialog -
a549262189:
支持下,正好在学习这块的内容!
Android Gesture Detector -
dcsff:
受教了
LayoutInflater -
庆_啊:
真厉害。
LayoutInflater -
ghj234:
学习了,哈哈
LayoutInflater
参考文档:
http://www.iteye.com/topic/479217
MenuMidlet类:是主类;Menu类:Menu的绘制;MenuScreen类:Menu事件处理。
1. 菜单绘制
(1)菜单有两种状态:激活状态和非激活状态。
在激活状态下,显示菜单项,并且可以接受用户的上下选择事件;在非激活状态下,隐藏菜单项,不接受用户的上下选择事件。
(2)菜单的绘制包括激活状态下的菜单绘制和非激活状态下的菜单绘制
非激活状态下的菜单绘制包括:左菜单(options)和右菜单(exit)绘制;激活状态下的菜单绘制包括:左菜单(options)和右菜单(cancel)绘制,左菜单项(新建,设定,记录,帮助,关于)绘制。
2.菜单事件处理
Canvas提供了keyPressed(int key)来响应用户的按键;Canvas中的gameAction是将手机键盘映射成为游戏动作的机制。
3.代码
public class Menu { private String leftOption; private String rightOption; private String cancelOption = "Cancel"; private String[] menuOptions; private int padding = 5; // 颜色设置 private static final int defaultFontColor = 0x000000; // 默认字体颜色 private static final int selectedFontColor = 0xfff000; // 选中后字体颜色 private static final int bgColor = 0xCCCCCC;// 菜单背景色 private static final int highLight = 0x0000ff;// 选中的菜单高亮显示颜色 public Menu(String leftOption, String rightOption, String[] menuOptions) { this.leftOption = leftOption; this.rightOption = rightOption; this.menuOptions = menuOptions; } // end constructor public void drawInactiveMenu(GameCanvas canvas, Graphics g) { // create inactive menu font Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM); int fontHeight = font.getHeight(); // clear inactive menu background int width = canvas.getWidth(); int height = canvas.getHeight(); g.setColor(bgColor); // grey color g.fillRect(0, height - fontHeight - 2 * padding, width, height); // draw left and right menu options g.setFont(font); g.setColor(defaultFontColor); // black g.drawString(leftOption, padding, height - padding, Graphics.LEFT | Graphics.BOTTOM); g.drawString(rightOption, width - padding, height - padding, Graphics.RIGHT | Graphics.BOTTOM); canvas.flushGraphics(); } // end drawInactiveMenu public void drawActiveMenu(GameCanvas canvas, Graphics g, int selectedOptionIndex) { Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM); int fontHeight = font.getHeight(); int width = canvas.getWidth(); int height = canvas.getHeight(); g.setColor(bgColor); g.fillRect(0, height - fontHeight - 2 * padding, width, height); // draw default menu bar options g.setFont(font); g.setColor(0x000000); // black g.drawString(leftOption, padding, height - padding, Graphics.LEFT | Graphics.BOTTOM); // draw "Cancel" option g.drawString(cancelOption, width - padding, height - padding, Graphics.RIGHT | Graphics.BOTTOM); canvas.flushGraphics(); // draw menu options if (menuOptions != null) { // 确定菜单的最大宽度、最大高度 int menuMaxWidth = 0; int menuMaxHeight = 0; int currentWidth = 0; for (int i = 0; i < menuOptions.length; i++) { currentWidth = font.stringWidth(menuOptions[i]); if (currentWidth > menuMaxWidth) { menuMaxWidth = currentWidth; } menuMaxHeight += fontHeight + padding; } menuMaxWidth += 6 * padding; g.setColor(bgColor); g.fillRect(0, // x height - fontHeight - 2 * padding - menuMaxHeight, // y menuMaxWidth, menuMaxHeight); g.setFont(font); int menuOptionX = padding; int menuOptionY = height - fontHeight - 2 * padding - menuMaxHeight + padding; for (int i = 0; i <= menuOptions.length; i++) { g.setColor(highLight); int lineOptionY = height - fontHeight - 2 * padding - menuMaxHeight + i * (fontHeight + padding); g.drawLine(0, lineOptionY, menuMaxWidth - 1, lineOptionY); } for (int i = 0; i < menuOptions.length; i++) { if (i != selectedOptionIndex) { // draw unselected menu option g.setColor(defaultFontColor); // black g.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT | Graphics.TOP); } else { // draw selected menu option g.setColor(highLight); int highOptionY = height - fontHeight - 2 * padding - menuMaxHeight + i * (fontHeight + padding); g.fillRect(0, highOptionY, menuMaxWidth, fontHeight + padding); g.setColor(selectedFontColor); g.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT | Graphics.TOP); } menuOptionY += padding + fontHeight; } canvas.flushGraphics(); } } }
public class MenuScreen extends GameCanvas { static int width; // screen width static int height; // screen height private int LEFT_SOFTKEY_CODE = -6; private int RIGHT_SOFTKEY_CODE = -7; // Menu Item Labels static final String[] menuOptions = { "新建", "设置", "记录", "帮助", "关于" }; static int menuIdx;// 记录是在第几个菜单处按下的确定键。 private int currentlySelectedIndex = 0; private boolean menuIsActive = false; private String leftOption; private String rightOption; private Graphics g; private Menu menu; // Constructor public MenuScreen(MenuMidlet midlet) { super(false); // Get Width and Height of Canvas width = getWidth(); height = getHeight(); setFullScreenMode(true); menuIdx = 0; g = getGraphics(); leftOption = "Options"; rightOption = "Exit"; menu = new Menu(leftOption, rightOption, menuOptions); start(); midlet.display.setCurrent(this); } public void start() { clearScreen(); menu.drawInactiveMenu(this, g); } public void clearScreen() { g.setColor(0xffffff); // white g.fillRect(0, 0, width, height); flushGraphics(); } protected void keyPressed(int keyCode) { if (menuIsActive) { if (keyCode == RIGHT_SOFTKEY_CODE) { // draw inactive menu again clearScreen(); menu.drawInactiveMenu(this, g); menuIsActive = false; } keyCode = getGameAction(keyCode); if (keyCode == UP) { currentlySelectedIndex--; if (currentlySelectedIndex < 0) { currentlySelectedIndex = 0; // stay within limits } menu.drawActiveMenu(this, g, currentlySelectedIndex); } else if (keyCode == DOWN) { currentlySelectedIndex++; if (currentlySelectedIndex >= menuOptions.length) { currentlySelectedIndex = menuOptions.length - 1; } menu.drawActiveMenu(this, g, currentlySelectedIndex); } else if (keyCode == FIRE) { clearScreen(); g.setColor(0x000000); g.drawString("[" + menuOptions[currentlySelectedIndex] + "] was selected", 10, 15, Graphics.LEFT | Graphics.TOP); menu.drawInactiveMenu(this, g); menuIsActive = false; } } else { if (keyCode == LEFT_SOFTKEY_CODE) { // "Options" pressed menu.drawActiveMenu(this, g, currentlySelectedIndex); menuIsActive = true; } else if (keyCode == RIGHT_SOFTKEY_CODE) { } } } }
public class MenuMidlet extends MIDlet { private MenuScreen menuScreen; Display display; public MenuMidlet() { display= Display.getDisplay(this); menuScreen = new MenuScreen(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { } }
4.http://www.iteye.com/topic/31909
http://www.iteye.com/topic/35427
本博文未完,误发布了。把自己过去半年来自己写的customized ui总结上来。
发表评论
-
线程过多and高效MIDP编程
2010-05-03 17:42 0见附件 -
XML解析
2010-02-02 23:25 1275下面代码用到了jdom和dom4j两个包 public cl ... -
解压缩
2010-02-02 23:25 1153/* * 非面向对象语言要实现保存工作数据(如一个绘图程序 ... -
回车换行符
2010-01-21 13:52 135811 换行符:另起一行,'\n'10,仅仅把一个长行截断成两 ... -
Thread Pool
2010-01-14 15:27 975public class WorkQueue { p ... -
FileConnection
2010-01-12 15:39 15341. 引言 FileConnection API 是一 ... -
自定义repeat()方法
2009-09-23 17:42 1355j2me的Canvas中有一个repeat()回调方法。大部分 ... -
J2ME移植-键值
2009-09-23 15:07 16511. J2ME标准api中Canvas中定义的键值有17(方向 ... -
kxml doc
2009-09-22 14:55 18931. KXmlParser java.lang.Ob ... -
Http线程阻塞分析
2009-08-23 16:48 4373先做一个总结:Http连接线程发生阻塞,会影响其他线程也出现阻 ... -
jar和jad解释
2009-08-20 23:22 1252整理电脑,从F盘挖坟挖出的股东,也贴出来吧 如果是K- ... -
图片在程序内存中的占有量
2009-08-20 17:58 27181. 图片占内存容量计算公式为: 图片所占内存大小 ... -
如何从网络中获取png图片
2009-08-03 19:54 20421. J2ME // 方法1 Connector conn ... -
cmnet和cmwap的访问
2009-07-30 11:06 2364CMWAP(通过WAP网关代理,由WAP网关连接服务器,然后将 ... -
JSR75 FileConnection Optional Package 使用方法
2009-07-29 17:09 1637package forrest.fcop; imp ... -
mobile日志
2009-07-29 16:05 9851.项目中的实际应用 这个demo用到了JSR75 - ... -
DoubleBuffer 实现
2009-07-29 10:22 1291绘制地图的时候会出现图片断裂,屏幕闪烁等绘图效率问题 ... -
System.getProperty(String key)和MIDlet.getAppProperty(String key)
2009-07-13 17:53 20021. System.getProperty(String ... -
WTK的安全特性
2009-07-01 17:28 10851. MIDlet套件签名的背景 ... -
纳米星开发遇到的一些bug
2009-06-30 17:01 10571.Stream和Connection使用后要关闭 ...
相关推荐
Linux Device Drivers Development: Develop customized drivers for embedded Linux AZW3版(kindle格式),可以用开源软件calibre-ebook(https://calibre-ebook.com/)打开,里面的图片可以用该软件还原到原本的大...
### LabVIEW Courseware Customization for Pre-College Learners #### Introduction Traditionally, teaching programming languages at the pre-college level has emphasized the use of graphical flow ...
CodeProject A Customized Window for MFC Applications_ Free source code and programming help.mht
《Linux Device Drivers Development: Develop customized drivers for embedded Linux》是一本专注于教授如何为嵌入式Linux系统开发定制化设备驱动程序的专业书籍。本书由John Madieu编写,于2017年首次出版。作者...
适合瑞昱网卡的R8168的普通PC机安装ESXI6.7系统。经过本人验证,可以在普通电脑上安装使用,重启后可以正常进入系统,不需要按 shift+O输入代码,合成的离线安装包是ESXi670-201912001.zip版本。...
根据给定的文件信息,我们可以提炼出如下知识点: 1. Linux设备驱动开发:本书名为《Linux设备驱动开发》,这本书专注于讲解如何为嵌入式Linux系统开发定制的设备驱动程序。在Linux系统中,设备驱动程序是操作系统...
Data Science with Java: Practical Methods for Scientists and Engineers by PhD Michael R....Get up and running with MapReduce, using customized components suitable for data science algorithms
自封装ESXi-6.7.0-20201004001-standard-customized.iso
标题"android_customized_component"暗示我们将讨论如何在Android平台上创建一个定制的组件,这个组件可以动态地展示资源加载的百分比。描述中的"小例子,实现动态显示资源加载百分比"进一步明确了我们的目标,即...
It is perfect for creating a quick and dynamic menu to use for your site. It comes with 8 different looks to choose from. It has a clean and modern design. It is very flexible and easy to use. It ...
I based the development of this control on Vista start menu, but as you can see in the image above, it can be easily customized to support other styles, themes, etc. See demo project for more details...
ESXi-6.7.0-20190504001-standard-customized-8168网卡
ESXi-6.7.0-20191204001-standard-customized.iso
网卡驱动封装之离线封装,r8168网卡驱动为例net55-r8168驱动支持网卡型号(Realtek RTL8111B / RTL8168B / RTL8111/RTL8168 / RTL8111C / RTL8111CP / RTL8111D(L) / RTL8168C / RTL8111DP / RTL8111E / RTL8168E / ...
ESXi-6.7.0-20191204001-standard-customized-8111f.iso 补入驱动 net55-r8168 支持以下网卡 10ec:8168, 1186:4300 (Realtek RTL8111B / RTL8168B / RTL8111/RTL8168 / RTL8111C / RTL8111CP / RTL8111D(L) / RTL...
sip related resource, simple and short, with Avaya's solutions
1. **文件I/O操作**:工具首先需要读取JAR文件,这涉及到Java的`java.util.jar`包,其中的`JarFile`类可以用来打开并访问JAR文件,而`JarEntry`类则表示JAR内的单个条目,通常是类文件。 2. **文件流处理**:对JAR...
examples can be directly adapted for customized solutions in diverse areas such as XML processing, automated software testing, and program analysis tools. Readers will also find underlying rationales ...
在这个案例中,"customized-music-menu-" 的标题和描述表明我们正在讨论一个关于如何使用HTML来创建个性化音乐播放功能的项目。 一个自定义音乐菜单通常包含以下关键元素: 1. **播放器结构**:HTML中的`<audio>`...