2009-10-20 31 views

回答

1

alt text http://img689.imageshack.us/img689/2886/maplinkicon.jpg

首先实现ButtonField字段的extention将:

  • 样子上点击图标
  • 将打开浏览器使用预定义的链接
  • 将使用上下文菜单

这样的控制代码:

class MapLinkIcon extends ButtonField implements FieldChangeListener { 
Bitmap mNormal; 
Bitmap mFocused; 

String mLink; 
String mDescription; 

int mWidth; 
int mHeight; 

public MapLinkIcon(Bitmap normal, Bitmap focused, String link, 
    String description) { 
    super(CONSUME_CLICK); 
    mNormal = normal; 
    mFocused = focused; 
    mLink = link; 
    mDescription = description; 

    mWidth = mNormal.getWidth(); 
    mHeight = mNormal.getHeight(); 
    setMargin(0, 0, 0, 0); 
    setPadding(0, 0, 0, 0); 
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0))); 
    setBorder(VISUAL_STATE_ACTIVE, BorderFactory 
    .createSimpleBorder(new XYEdges(0, 0, 0, 0))); 

    this.setChangeListener(this); 
} 

protected void paint(Graphics graphics) { 
    Bitmap bitmap = null; 
    switch (getVisualState()) { 
    case VISUAL_STATE_NORMAL: 
    bitmap = mNormal; 
    break; 
    case VISUAL_STATE_FOCUS: 
    bitmap = mFocused; 
    break; 
    case VISUAL_STATE_ACTIVE: 
    bitmap = mFocused; 
    break; 
    default: 
    bitmap = mNormal; 
    } 
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), 
    bitmap, 0, 0); 
} 

public int getPreferredWidth() { 
    return mWidth; 
} 

public int getPreferredHeight() { 
    return mHeight; 
} 

protected void layout(int width, int height) { 
    setExtent(mWidth, mHeight); 
} 

protected void applyTheme(Graphics arg0, boolean arg1) { 

} 

public void fieldChanged(Field field, int context) { 
    openLink(mLink); 
} 

MenuItem mMenuItem = new MenuItem("Go To Link", 0, 0) { 
    public void run() { 
    openLink(mLink); 
    } 
}; 

protected void makeContextMenu(ContextMenu contextMenu) { 
    super.makeContextMenu(contextMenu); 
    contextMenu.addItem(mMenuItem); 
} 

private static void openLink(String link) { 
    Browser.getDefaultSession().displayPage(link); 
} 
} 

现在我们可以使用与MapField可结合该按钮控制,覆盖sublayout放置按钮,在地图上:

class CustomMapField extends VerticalFieldManager { 

MapField mMapField; 
MapLinkIcon mButton; 

public CustomMapField() { 
    add(mMapField = new MapField()); 
} 

public int getPreferredHeight() { 
    return getScreen().getHeight(); 
} 

public int getPreferredWidth() { 
    return getScreen().getWidth(); 
} 

public void moveTo(Coordinates coordinates, Bitmap icoNorm, Bitmap icoAct, 
    String link, String description) { 

    mMapField.moveTo(coordinates); 

    add(mButton = new MapLinkIcon(icoNorm, icoAct, link, description)); 
} 

protected void sublayout(int maxWidth, int maxHeight) { 
    int width = getPreferredWidth(); 
    int height = getPreferredHeight(); 
    layoutChild(mMapField, width, height); 
    setPositionChild(mMapField, 0, 0); 

    layoutChild(mButton, mButton.mWidth, mButton.mHeight); 
    XYPoint fieldOut = new XYPoint(); 
    mMapField.convertWorldToField(mMapField.getCoordinates(), fieldOut); 
    int xPos = fieldOut.x - mButton.mWidth/2; 
    int yPos = fieldOut.y - mButton.mHeight; 
    setPositionChild(mButton, xPos, yPos); 

    setExtent(width, height); 
} 
} 

使用示例:

​​

另请参见:
How to show our own icon in BlackBerry Map?
How to show more than one location in Blackberry MapField?

相关问题