BlackBerry 触摸屏设备编程

作者: 王志刚

 

 

  触摸 计需 注意 地方

 

开发用 带触 屏的   Blac k Ber r y ®   设备的应用 ,您应 虑这 设备 以下   U I 功能:

    触摸屏

    屏幕方位的 要性

    触摸屏键盘

    加速度计( 些带     Bla c kB e rr y   设备上)

 

关于这 项目 具体 细节, 以参 blackbe r r y 的编程 皮书 BlackBe r r y Ja v a A pp l ic a ti o n

Tra n si t io n in g t o T o uc h S cre e n D ev e lo p me n t .

 

 

  触摸 具体 程和 计的

 

随着触 屏幕 机的 Bla c kB e rr y 也加入了相应 API 的支持:

net . ri m .d e vi c e. a pi . ui . T o uch s cr e e n 这个类 作用 是系 判别当 手机 否是 uti l it y 类别,所以一个 较通用的 jav a 程序的 就是在 序入 中加 的逻辑 断:

if (Touchscreen.isSupported ())

{


 

}

else

{


/* Touch screen logic   */

 

 

 

UiApplication.getUiApplication ().invokeLater( new   Runnable()

{


public     void     run()

{


 

 

}

});

}


Dialog.alert ( "This application requires a touch screen device." );

System.exit (0);


JDE5.0 的示 程序中, 有一个 touchdemo 有关于 个工 类的使 用方法 可以 考相 代理例 在做 个通 bl ac kb er ry 客户端 序。

 

触屏手 的引 ,可 对屏幕 方向 变化 在原有 全键 手机 有的, 以在 摸屏幕 手机开 应用 程中 的响 的逻辑 . 有两种 式可 用来 的方位 行控 和调 一种方 screen su bl ay ou t 方法中。

 

public void sublayout(int width, int height)

{

//update scrren layout based on orientation

if(Display.getOrientation()== Display.ORIENTATION_LANDSCAPE)

{

invalidate();

}

else if(Display.getOrientation()== Display.ORIENTATION_PORTRAIT)

{

invalidate();

}

supe r.sublayout(width, height);

}

 


还有一 方式 在应 式调用 幕的 位信 后做出 整:

switch(Display.getOrientation())

{

case Display.ORIENTATION_LANDSCAPE:

Dialog.alert("Screen orientation is landscape"); break;

case Display.ORIENTATION_PORTRAIT:

Dialog.alert("Screen orientation is portrait"); break;

case Dis play.ORIENTATION_SQUARE:

Dialog.alert("Screen orientation is square"); break;

default:

Dialog.alert("Screen orientation is not known"); break;

}

 

除了对 幕方 也就 切换的 理, 触摸 程中, 要考 UI 事件的处 理和全 盘手 的不 同,关 这部 的具 ,可以 考下 部分 以及后 的内

 

  一个触 屏幕应用界 的例子

 

本章以 个定 的黑莓 UI 示程序 例, 明黑 中需要 意的 个方 过一个 以定 的个性 化的 toolbar 现以及黑 莓应用 背景 切换 明触摸 幕编 和普 盘手机 制程 的区

别。

 

黑莓标 UI 里面是没 toolbar 的, 里要设计 toolbar 以考虑标 UI 组件 扩展 里我们让 toolbar 继承自 H or iz on ta lF ie ld Ma na ge r . 基本的 toolbar 特性包括 排列的 toolbar 的高 宽等等 及组 的排 。这些 是可 配置 的,如 要做 一个 活的设 ,这 我们 死。

 

//public class ToolBarField extends HorizontalFieldManager public class ToolBarField extends HorizontalFieldManager

{

//private static final int DefaultButtonHeight = 55;

//private static final int DefaultButtonWidth = 55; private static final int DefaultButtonHeight = 129; private static final int DefaultButtonWidth = 129;

 

private Vector leftJustifiedButtons = new Vector(); private Vector rightJustifiedButtons = new Vector(); private int preferredHeight = DefaultButtonHeight; private int sideMargin = 3;

private int buttonSpacing = 2;

private int preferredWidth = Display.getWidth();

private Bitmap bg = null;

 

……

}

 

toolbarfield 中,核 的部 在于

private Vector leftJustifiedButtons = new Vector();

private Vector rightJustifiedButtons = new Vector(); 这两个 为了 置用 扩种的具体 field 里使用 是标 vector 元素, 以添 也可 除组件 这里 们简 ,只实 添加 接口 addbutton

 

public void addButton(ToolBarButtonField button, boolean leftJustified)

{

super.add(button);

 

if (button.getPreferredHeight() > preferredHeight)

preferredHeight = button.getPreferredHeight();

 

if (leftJustified)


 

{

 

}

else

{

 

}

}


 

leftJustifiedButtons.addElement(button);

 

 

 

rightJustifiedButtons.addElement(button);


 

为了实 更加 供定 效果, 以扩 Horizont almanager subpaint 法,添 部分 景处理

的能力:

 

protected void subpaint(Graphics graphics)

{

if (bg != null)

{

for (int x = 0; x < Display.getWidth();)

{


 

 

}

}

else

{


graphics.drawBitmap(x, 0, getPreferredWidth(), bg.getHeight(), bg, 0, 0);

x += bg.getWidth();


graphics.setColor(Color.BLACK);

graphics.drawRect(0, 0, getPreferredWidth(), getPrefe rredHeight());

}

 

super.subpaint(graphics);

}

 

如果所 的功 扩充 ,我们 设计 toolbar 只是一 可供 键盘使 用的版 ,这 我们加

入一个 touchevent 加入 触摸 这个 法, 是一个 门针对

touchscreen 的控件 版本:

 

public boolean touchEvent(TouchEvent event)

{

int eventID = event.getEvent();

if (eventID == TouchEvent.DOWN || eventID == TouchEvent.UP)

{

boolean hit = false; int x = event.getX(1); int y = event.getY(1);

 

for (int f = 0; f < getFieldCount(); f++)

{

ToolBarButtonField field = (ToolBarButtonField)getField(f);

XYRect ext = field.getExtent();

if (ext.contains(x, y))

{

hit = true;

 

 

if (eventID == TouchEvent.UP)

{


 

 

}

else

{

 

 

}


field.setActive(false);

this.setFocus();

 

 

 

field.setFocus();

field.setActive(true);


 

invalidate();

break;


 

}

}

 

if (!hit && eventID == TouchEvent.UP)

{

this.setFocus();

invalidate();

}

}

 

return true;

}

 

 

基本上 UI 展现 如果把如 上的部 做完 ,有本的 UI 的事 的处 之后也就 可以了 系统 自动对

UI 的布局 一些 整,但是 作为一 标准 应用 需要考 的是 UI 的展 现在任 情况 都能达 到比较 想的 果, 需要对 屏手 的屏 方位切 事件 行相 理和考 了。

 

protected void sublayout(int maxWidth, int maxHeight)

{

this.setExtent(maxWidth, getPreferredHeight());

 

Enumeration iter = leftJustifiedButtons.elements();

 

if (maxHeight > Display.getHeight())

maxHeight = Display.getHeight();

 

int y = 0;

int curX = sideMargin;

 

//

// Layout the left justified buttons

//

while (iter.hasMoreElements())

{

ToolBarButtonField button = (ToolBarButtonField)iter.nextElement();

this.layoutChild(button, button.getPreferredWidth(), button.getPreferredWidth());

 

this.setPositionChild(button, curX, y);

curX = curX + button.getWidth();

}

 

int minX = curX + buttonSpacing;

int totalButtonsWidth = 0;

//

// Layout the right justified buttons

//

iter = rightJustifiedButtons.elements();

 

while (iter.hasMoreElements())

{

ToolBarButtonField button = (ToolBarButtonField)iter.nextElement(); this.layoutChild(button, button.getPreferredWidth(), button.getPreferredWidth()); totalButtonsWidth += button.getWidth() + buttonSpacing;

}

totalButtonsWidth -= buttonSpacing;

 

if ((totalButtonsWidth+minX) > maxWidth)

{

preferredWidth = totalButtonsWidth+minX;

}

curX = maxWidth;

iter = rightJustifiedButtons.elements();

while (iter.hasMoreElements())

{

ToolBarButtonField button = (ToolBarButtonField)iter.nextElement();

this.layoutChild(button, button.getPreferredWidth(), button.getPreferredWidth());


 

curX = curX - button.getWidth(); this.setPositionChild(button, curX, y); curX -= buttonSpacing;

}

}

 

 

 

 

我们这 以一 定制的 to o lb ar 组件为 ,说 BlackBerry 的触 上响应 处理 幕的

和翻转 做到 applicatio n 对屏幕 自适 能力 Bl ac kb er ry 标准的 幕切 事件处 中, 推荐 sublayout 中对 件的布 进行 应的 上面的 法中 也实 分这样 功能 但是 有用到 屏编 中需 的方位 题的 测, 不完整。

 

 

 

所以下 我们 考虑 子中的 个比 好的 ,对于 幕背 的切 blackberry api 中,提 screen mainmanager ba ck br ou nd 的背景 置的 api ,但 是由于 种原 是有些 问题, 里我 考虑 法,可 绕过 个问

 

DecorTestScreen screen = new DecorTestScreen(); VerticalFieldManager horizontalFieldManager = new

VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH | VerticalFieldManager.USE_ALL_HEIGHT){

//Override the paint method to draw the background image. public void paint(Graphics graphics)

{

graphics.clear();

switch(Display.getOrientation())

{

case Display.ORIENTATION_LANDSCAPE:

graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(), backgroundBitmap, 0, 0);

break;

case Display.ORIENTATION_PORTRAIT:

graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(), backgroundBitmap2, 0, 0);

break;

default:

break;

}

super.paint(graphics);

}

};

screen.setTitle(toolBar);

screen.add(horizontalFieldManager);

 

这里面使 用的 法实 是天面 到的 应用 程序的 位, 后重 屏幕, 不过 们这里 重新绘 的是 幕背 具体 ui 件, 果是 ui 组件, 好的 法是 sublayout 去通过对 layoutmanag er 的设置。

 

在完成 本的 幕和 to ol b ar 组件的 计之 ,剩下的 就是测 和验 这个 我们设 一个 toolbar 的添 button 的类 ,然后把 button 添加到我 们设计的 toolbar 中,代 码如下:

public class ToolBarButtonField extends ImageButtonField

//class T

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值