首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> J2ME >> 可选包 >> 正文
定位API开发指南——例子:移动应用开发的定位和定位检测(6)
作者:姜 译    文章来源:诺基亚官方文档    更新时间:2006-12-26 16:47:45

此文为开发视界翻译转载者请注明出处(开发视界 www.sf.org.cn)否则追究法律责任

4.4.8 CompassUI

1、创建CompassUI类文件。

2、引入必要的类。

package com.nokia.example.location.tourist.ui;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import javax.microedition.location.Orientation;

import com.nokia.example.location.tourist.TouristMIDlet;

import com.nokia.example.location.tourist.model.ConfigurationProvider;

3、创建CompassI类,并设置扩展Canvas。运行CommandListenerRunnable。定义这个类需要使用的常量和命令。

/**

* Viewer class representing a compass.

*/

public class CompassUI extends Canvas implements CommandListener, Runnable

{

/** Constant value for X coordinate. Used in arrays. */

private final int X = 0;

/** Constant value for Y coordinate. Used in arrays. */

private final int Y = 1;

/** Compass center X point */

private int centerX;

/** Compass center Y point */

private int centerY;

/** Constant value representing one degree. */

private final float degree = (float) (2 * Math.PI / 360.0);

/** Current compass azimuth. */

private float azimuth = 0.0f;

/**

* Is orientation relative to the magnetic field of the Earth or true north

* and gravity.

*/

private boolean isMagnetic;

/** Flag telling is the compass update thread active */

private boolean threadActive = false;

/** Sleep 1000ms during each compass update */

private final long SLEEPTIME = 1000;

/** A reference to Route UI */

private Displayable route = null;

/** A reference to Pitch and Roll UI */

private Displayable pitchrollUI = null;

/** Command that swithes current displayable to Route UI */

private Command routeCmd = new Command("Route", Command.BACK, 1);

/** Command that swithes current displayable to Pitch and Roll UI */

private Command prCmd = new Command("Pitch and Roll", Command.OK, 1);

/** Compss background image. */

private Image compassImage = null;

4、为类创建结构。

/**

* Construct instance of this displayable.

*

* @param route

* is a reference to Route UI.

*/

public CompassUI(Displayable route)

{

this.route = route;

pitchrollUI = new PitchRollUI(this);

loadCompassImage();

centerX = getWidth() / 2;

centerY = getHeight() / 2;

addCommand(routeCmd);

addCommand(prCmd);

setCommandListener(this);

}

5、为Compass创建方法,加载背景图像。

/**

* Load compass backgound image from JAR file.

*/

private void loadCompassImage()

{

String imageName = "/compass_small.gif";

if (getWidth() > 160)

{

imageName = "/compass_large.gif";

}

try

{

compassImage = Image.createImage(getClass().getResourceAsStream(

imageName));

}

catch (IOException e)

{

e.printStackTrace();

}

}

6、创建以下两个方法,根据Canvas的可见性,VM选择调用。

/**

* VM calls this method immediately prior to this Canvas being made visible

* on the display.

*/

protected void showNotify()

{

// Actives compass update thread.

threadActive = true;

new Thread(this).start();

}

/**

* VM calls this method shortly after the Canvas has been removed from the

* display.

*/

protected void hideNotify()

{

// Stops the thread.

threadActive = false;

}

7、创建方法,引导canvas

/**

* Renders the canvas.

*

* @param g -

* the Graphics object to be used for rendering the Canvas

*/

protected void paint(Graphics g)

{

// clean up canvas

g.setColor(255, 255, 255);

g.fillRect(0, 0, getWidth(), getHeight());

int spikeLen = 5;

int len = (compassImage.getWidth() / 2) - spikeLen;

// draw compass background

g.drawImage(compassImage, (getWidth() - compassImage.getWidth()) / 2,

centerY, Graphics.LEFT | Graphics.VCENTER);

// draw compass arrow

g.setColor(0, 0, 255);

drawArrow(g, degree * azimuth, len, spikeLen);

// draw orientation type

g.setColor(0, 0, 255);

String otext = "True North";

if (!isMagnetic)

{

otext = "Magnetic field";

}

g.drawString("Orientation: " + otext, 0, getHeight(), Graphics.BOTTOM

| Graphics.LEFT);

}

8、创建方法,描述compass arrow

/**

* Draw a compass arrow rotated to a certain angle.

*

* @param g

* is a reference to Graphics object.

* @param angle

* in degrees [0.0,360.0)

* @param len

* is arrows length.

* @param spikeLen

* is length of arrows spike.

*/

private void drawArrow(Graphics g, float angle, int len, int spikeLen)

{

int a[] = rotate(angle, 0, -(len - spikeLen));

int b[] = rotate(angle, -spikeLen, -(len - spikeLen));

int c[] = rotate(angle, 0, -len);

int d[] = rotate(angle, spikeLen, -(len - spikeLen));

int e[] = rotate(angle + (degree * 180.0), 0, -len);

// use red foreground color

g.setColor(255, 0, 0);

g.drawLine(centerX, centerY, centerX + a[X], centerY + a[Y]);

g.drawLine(centerX + b[X], centerY + b[Y], centerX + d[X], centerY

+ d[Y]);

g.drawLine(centerX + b[X], centerY + b[Y], centerX + c[X], centerY

+ c[Y]);

g.drawLine(centerX + c[X], centerY + c[Y], centerX + d[X], centerY

+ d[Y]);

// use black foreground color

g.setColor(0, 0, 0);

g.drawLine(centerX, centerY, centerX + e[X], centerY + e[Y]);

}

9、创建方法,旋转箭头方向。

/**

* Rotate point (x,y) by degrees that angle parameter defines. The new

* coordinate calculation is performed with a 2x2 rotate matrix.

*

* @param angle

* to be rotated

* @param x

* coordinate

* @param y

* coordinate

* @return new coordinate pair in int array format [x,y]

*/

private int[] rotate(double angle, int x, int y)

{

int rotated[] = new int[2];

rotated[X] = (int) (Math.cos(angle) * x + Math.sin(angle) * y);

rotated[Y] = (int) (-Math.sin(angle) * x + Math.cos(angle) * y);

return rotated;

}

10、通过Runnable创建一个运行的方法。目的是更新角度、高度和长度,覆盖canvasisOrientationMagnetic方法将返回一个布尔值,用来说明Orientation方法是否与地球的地磁连接或者使用的是真实的北极点。

/**

* run method from Runnable interface. Updates azimuth, pitch and roll

* values and repaints the canvas.

*

* If Orientation is supported on the terminal, compass sensor is either 2D

* or 3D. If the terminals compass sensor providers only compass azimuth,

* pitch and roll values are Float.NaN.

*

* @see HideNotify() method.

*/

public void run()

{

// Keep the thread running until another displayable is set visible.

// See also hideNotify() method.

while (threadActive)

{

Orientation orientation = ConfigurationProvider.getInstance()

.getOrientation();

if (orientation != null)

{

isMagnetic = orientation.isOrientationMagnetic();

azimuth = orientation.getCompassAzimuth();

}

repaint();

try

{

// Pause this thread for a secord before next update.

Thread.sleep(SLEEPTIME);

}

catch (InterruptedException e)

{

}

}

}

11、创建方法,检测命令按键的输入。

/**

* Event indicating when a command button is pressed.

*

* @see javax.microedition.lcdui.CommandListener#commandAction

(javax.microedition.lcdui.Command,

* javax.microedition.lcdui.Displayable)

*/

public void commandAction(Command command, Displayable d)

{

if (command == routeCmd)

{

TouristMIDlet.getDisplay().setCurrent(route);

}

else if (command == prCmd)

{

TouristMIDlet.getDisplay().setCurrent(pitchrollUI);

}

}

}
相关文章:
没有相关文章
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号