protected void onDrawItem(
      final Canvas canvas, final int zoomLevel, final Item item, final Point curScreenCoords) {
    MaskableOverlayItem maskableItem = item;
    if (maskableItem.isHidden()) {
      return;
    }

    final Station station = (Station) item.getRelatedObject();
    final boolean zoomLevelDetailled = OverlayZoomUtils.isDetailledZoomLevel(zoomLevel);
    final Drawable marker = getDefaultMarker(zoomLevelDetailled, station.isStarred());

    boundToHotspot(marker, HotspotPlace.BOTTOM_CENTER);

    Overlay.drawAt(canvas, marker, curScreenCoords.x, curScreenCoords.y, false, 0);
    if (zoomLevelDetailled) {
      mTitlePaint.setColor(
          getResourceProxy().getColor(ColorSelector.getColorForMap(station.getBikes())));
      canvas.drawText(
          station.getBikesAsString(),
          mCurScreenCoords.x - (8 * mScale),
          mCurScreenCoords.y - (26 * mScale),
          mTitlePaint);
      mTitlePaint.setColor(
          getResourceProxy().getColor(ColorSelector.getColorForMap(station.getAttachs())));
      canvas.drawText(
          station.getAttachsAsString(),
          mCurScreenCoords.x - (8 * mScale),
          mCurScreenCoords.y - (13 * mScale),
          mTitlePaint);
    }
  }
Beispiel #2
0
 @Override
 public void onDetach(final MapView mapView) {
   Log.d(TAG, "##### onDetach #### " + person);
   onPause();
   hideBubble(mapView);
   super.onDetach(mapView);
 }
  public void drawItem(final Canvas c, final MapView osmv, Item item) {
    Drawable markerFocusedBase = item.getMarker(OverlayItem.ITEM_STATE_FOCUSED_MASK);
    if (markerFocusedBase == null) {
      markerFocusedBase = this.mMarkerFocusedBase;
    }

    /* Calculate and set the bounds of the marker. */
    osmv.getProjection().toPixels(item.getPoint(), mFocusedScreenCoords);

    markerFocusedBase.copyBounds(mRect);
    mRect.offset(mFocusedScreenCoords.x, mFocusedScreenCoords.y);

    /* Strings of the OverlayItem, we need. */
    final String itemTitle = (item.getTitle() == null) ? UNKNOWN : item.getTitle();
    final String itemDescription = (item.getSnippet() == null) ? UNKNOWN : item.getSnippet();

    /*
     * Store the width needed for each char in the description to a float array. This is pretty
     * efficient.
     */
    final float[] widths = new float[itemDescription.length()];
    this.descriptionPaint.getTextWidths(itemDescription, widths);

    final StringBuilder sb = new StringBuilder();
    int maxWidth = 0;
    int curLineWidth = 0;
    int lastStop = 0;
    int i;
    int lastwhitespace = 0;
    /*
     * Loop through the charwidth array and harshly insert a linebreak, when the width gets
     * bigger than DESCRIPTION_MAXWIDTH.
     */
    for (i = 0; i < widths.length; i++) {
      if (!Character.isLetter(itemDescription.charAt(i))) {
        lastwhitespace = i;
      }

      final float charwidth = widths[i];

      if (curLineWidth + charwidth > DESCRIPTION_MAXWIDTH) {
        if (lastStop == lastwhitespace) {
          i--;
        } else {
          i = lastwhitespace;
        }

        sb.append(itemDescription.subSequence(lastStop, i));
        sb.append('\n');

        lastStop = i;
        maxWidth = Math.max(maxWidth, curLineWidth);
        curLineWidth = 0;
      }

      curLineWidth += charwidth;
    }
    /* Add the last line to the rest to the buffer. */
    if (i != lastStop) {
      final String rest = itemDescription.substring(lastStop, i);
      maxWidth = Math.max(maxWidth, (int) this.descriptionPaint.measureText(rest));
      sb.append(rest);
    }
    final String[] lines = sb.toString().split("\n");

    /*
     * The title also needs to be taken into consideration for the width calculation.
     */
    final int titleWidth = (int) this.descriptionPaint.measureText(itemTitle);

    maxWidth = Math.max(maxWidth, titleWidth);
    final int descWidth = Math.min(maxWidth, DESCRIPTION_MAXWIDTH);

    /* Calculate the bounds of the Description box that needs to be drawn. */
    final int descBoxLeft =
        mRect.left - descWidth / 2 - DESCRIPTION_BOX_PADDING + mRect.width() / 2;
    final int descBoxRight = descBoxLeft + descWidth + 2 * DESCRIPTION_BOX_PADDING;
    final int descBoxBottom = mRect.top;
    final int descBoxTop =
        descBoxBottom
            - DESCRIPTION_TITLE_EXTRA_LINE_HEIGHT
            - (lines.length + 1) * DESCRIPTION_LINE_HEIGHT /* +1 because of the title. */
            - 2 * DESCRIPTION_BOX_PADDING;

    /* Twice draw a RoundRect, once in black with 1px as a small border. */
    this.mMarkerBackgroundPaint.setColor(Color.BLACK);
    c.drawRoundRect(
        new RectF(descBoxLeft - 1, descBoxTop - 1, descBoxRight + 1, descBoxBottom + 1),
        DESCRIPTION_BOX_CORNERWIDTH,
        DESCRIPTION_BOX_CORNERWIDTH,
        this.descriptionPaint);
    this.mMarkerBackgroundPaint.setColor(this.mMarkerFocusedBackgroundColor);
    c.drawRoundRect(
        new RectF(descBoxLeft, descBoxTop, descBoxRight, descBoxBottom),
        DESCRIPTION_BOX_CORNERWIDTH,
        DESCRIPTION_BOX_CORNERWIDTH,
        this.mMarkerBackgroundPaint);

    final int descLeft = descBoxLeft + DESCRIPTION_BOX_PADDING;
    int descTextLineBottom = descBoxBottom - DESCRIPTION_BOX_PADDING;

    /* Draw all the lines of the description. */
    for (int j = lines.length - 1; j >= 0; j--) {
      c.drawText(lines[j].trim(), descLeft, descTextLineBottom, this.descriptionPaint);
      descTextLineBottom -= DESCRIPTION_LINE_HEIGHT;
    }
    /* Draw the title. */
    c.drawText(
        itemTitle,
        descLeft,
        descTextLineBottom - DESCRIPTION_TITLE_EXTRA_LINE_HEIGHT,
        this.titlePaint);
    c.drawLine(descBoxLeft, descTextLineBottom, descBoxRight, descTextLineBottom, descriptionPaint);

    /*
     * Finally draw the marker base. This is done in the end to make it look better.
     */
    Overlay.drawAt(
        c, markerFocusedBase, mFocusedScreenCoords.x, mFocusedScreenCoords.y, false, 0.0f);
  }