@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int offsetY = getPaddingTop(); int startX = (int) (getPaddingLeft() + 10 + getMaxRadius() + 0.5f); if (isDrawTransit()) startX = startX + getMaxTransitWidth(); if (points.size() > 1) { for (int i = 0; i < points.size(); i++) { RouteViewPoint p = points.get(i); if (i == 0) offsetY += p.getRadius(); if (i > 0) offsetY += p.getDistance(); p.setBasicX(startX); p.setBasicY(offsetY); if (i == points.size() - 1) offsetY += p.getRadius(); } } else if (points.size() == 1) { RouteViewPoint p = points.get(0); offsetY += p.getRadius(); p.setBasicY(offsetY); offsetY += p.getRadius(); } offsetY += getPaddingBottom(); heightMeasureSpec = MeasureSpec.makeMeasureSpec(offsetY, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
/** * draw label * * @param canvas * @param p * @param marginLeft left margin from the circle background of index * @param textPaint */ private void drawLabel(Canvas canvas, RouteViewPoint p, int marginLeft, TextPaint textPaint) { if (p == null) return; String label = p.getLabel(); if (label == null || label.length() == 0) return; textPaint.setColor(p.getLabelColor()); textPaint.setTextSize(p.getLabelSize()); textPaint.setTypeface(Typeface.defaultFromStyle(p.isLabelBold() ? Typeface.BOLD : Typeface.NORMAL)); textPaint.getTextBounds(label, 0, label.length(), textBoundRect); Paint.FontMetrics fontMetrics = textPaint.getFontMetrics(); int w = textBoundRect.right - textBoundRect.left; int h = textBoundRect.bottom - textBoundRect.top; float xStart = p.getBasicX() + getMaxRadius() + marginLeft; float baseLine = p.getBasicY() - h / 2.0f + (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top; canvas.drawText(label, xStart, baseLine, textPaint); }
int w = textBoundRect.right - textBoundRect.left; int h = textBoundRect.bottom - textBoundRect.top; float xStart = p.getBasicX() + getMaxRadius() + marginLeft; float newBasicY = p.getBasicY() + p.getRadius() + h / 2.0f; float baseLine = newBasicY - h / 2.0f + (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;