コード例 #1
0
  /**
   * Calculates control width and creates text layouts.
   *
   * @param widthSize the input layout width
   * @param mode the layout mode
   * @return the calculated control width
   */
  private int calculateLayoutWidth(int widthSize, int mode) {
    initResourcesIfNecessary();

    int width = widthSize;

    int maxLength = getMaxTextLength();
    if (maxLength > 0) {
      // 一个字符宽度
      float textWidth = (int) AbGraphicUtil.getStringWidth("0", labelPaint);
      // 不算lable的宽度
      itemsWidth = (int) (maxLength * textWidth);
    } else {
      itemsWidth = 0;
    }

    // 空隙宽度
    itemsWidth += LABEL_OFFSET;

    // label宽度的计算
    labelWidth = 0;
    if (label != null && label.length() > 0) {
      labelWidth = (int) AbGraphicUtil.getStringWidth(label, labelPaint);
      if (D) Log.d(TAG, "itemsWidth:" + itemsWidth + ",labelWidth:" + labelWidth);
    }

    boolean recalculate = false;
    if (mode == MeasureSpec.EXACTLY) {
      width = widthSize;
      recalculate = true;
    } else {
      width = itemsWidth + labelWidth + 2 * PADDING;
      if (labelWidth > 0) {
        width += LABEL_OFFSET;
      }

      // Check against our minimum width
      width = Math.max(width, getSuggestedMinimumWidth());

      if (mode == MeasureSpec.AT_MOST && widthSize < width) {
        width = widthSize;
        recalculate = true;
      }
    }

    if (recalculate) {
      // recalculate width
      int pureWidth = width - LABEL_OFFSET - 2 * PADDING;
      if (pureWidth <= 0) {
        itemsWidth = labelWidth = 0;
      }
      if (labelWidth > 0) {
        // 对半分后再调整下
        int newItemsWidth = pureWidth / 2;
        int newLabelWidth = pureWidth - itemsWidth;
        if (newItemsWidth < itemsWidth) {
          // 放不下了,看label有空余宽度没有
          itemsWidth = newItemsWidth + newLabelWidth - labelWidth;
        } else {
          labelWidth = newLabelWidth;
          itemsWidth = newItemsWidth;
        }

      } else {
        // no label
        itemsWidth = pureWidth + LABEL_OFFSET;
      }
    }

    if (itemsWidth > 0) {
      createLayouts(itemsWidth, labelWidth);
    }

    return width;
  }
コード例 #2
0
ファイル: GraphicalView.java プロジェクト: SkyLB2016/HbAndGap
  /**
   * 描述:TODO.
   *
   * @version v1.0
   * @param canvas the canvas
   * @see View#onDraw(Canvas)
   * @author: amsoft.cn
   * @date:2013-6-17 上午9:04:51
   */
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.getClipBounds(mRect);
    int top = mRect.top;
    int left = mRect.left;
    int width = mRect.width();
    int height = mRect.height();
    if (mRenderer.isInScroll()) {
      top = 0;
      left = 0;
      width = getMeasuredWidth();
      height = getMeasuredHeight();
    }
    mChart.draw(canvas, left, top, width, height, mPaint);

    if (mRenderer != null && mRenderer.isZoomEnabled() && mRenderer.isZoomButtonsVisible()) {
      mPaint.setColor(ZOOM_BUTTONS_COLOR);

      int bitmapWidth = zoomInImage.getWidth();
      int bitmapHeight = zoomInImage.getHeight();

      int rectMargin = 10;
      int topPadding = 15;
      int leftPadding = 20;
      int leftRect = width - bitmapWidth * 3 - rectMargin - 4 * leftPadding;
      int topRect = height - bitmapHeight - rectMargin - 2 * topPadding;
      int rightRect = width - rectMargin;
      int bottomRect = height - rectMargin;

      mZoomR.set(leftRect, topRect, rightRect, bottomRect);
      canvas.drawRoundRect(mZoomR, bitmapWidth / 2, bitmapWidth / 2, mPaint);
      // 画图标
      float buttonY = height - bitmapHeight - rectMargin - topPadding;
      canvas.drawBitmap(
          zoomInImage, width - rectMargin - bitmapWidth * 3 - 3 * leftPadding, buttonY, null);
      canvas.drawBitmap(
          zoomOutImage, width - rectMargin - bitmapWidth * 2 - 2 * leftPadding, buttonY, null);
      canvas.drawBitmap(
          fitZoomImage, width - rectMargin - bitmapWidth - leftPadding, buttonY, null);
    }

    if (mChart instanceof XYChart) {
      XYMultipleSeriesRenderer mXYMultipleSeriesRenderer = ((XYChart) mChart).getRenderer();

      if (mXYMultipleSeriesRenderer.isScaleLineEnabled()) {
        oldX = ((TouchHandler) mTouchHandler).getOldX();
        int scaleTopPadding = 50;
        // 从坐标轴往下
        int scaleBottomPadding = 20;
        int explainTextSize1 = mXYMultipleSeriesRenderer.getExplainTextSize1();
        int explainTextSize2 = mXYMultipleSeriesRenderer.getExplainTextSize2();
        int scaleCircleRadius = mXYMultipleSeriesRenderer.getScaleCircleRadius();
        // 按分辨率转换
        scaleTopPadding = AbViewUtil.scale(mContext, scaleTopPadding);
        scaleBottomPadding = AbViewUtil.scale(mContext, scaleBottomPadding);

        // Y轴位置
        int bottomY = 0;
        int topY = 0;
        if (mChart instanceof XYChart) {
          Rect mScreenR = ((XYChart) mChart).getScreenR();
          bottomY = mScreenR.bottom;
          topY = mScreenR.top;
          if (oldX == 0) {
            oldX = mScreenR.right;
          }
        } else {
        }

        // 标尺线与拖手
        mPaint.setColor(mXYMultipleSeriesRenderer.getScaleLineColor());
        canvas.drawLine(oldX, scaleTopPadding, oldX, bottomY + scaleBottomPadding, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(ZOOM_BUTTONS_COLOR);
        canvas.drawCircle(
            oldX, bottomY + scaleBottomPadding + scaleCircleRadius, scaleCircleRadius, mPaint);

        // 判断挂到哪个点上
        if (mChart instanceof XYChart) {
          List<Float> mPoints = null;
          List<Double> mValues = null;
          List<String> mExplains = null;
          float minValue = -1;
          int minIndex = -1;
          // 要显示的文本
          String showValue = "";
          String showExplain = "";
          Map<Integer, List<Float>> points = ((XYChart) mChart).getPoints();
          Map<Integer, List<Double>> values = ((XYChart) mChart).getValues();
          Map<Integer, List<String>> explains = ((XYChart) mChart).getExplains();
          for (Entry<Integer, List<Float>> value : points.entrySet()) {
            int index = value.getKey();
            mPoints = value.getValue();
            mValues = values.get(value.getKey());
            mExplains = explains.get(value.getKey());

            // 判断距离在2范围的点的索引
            minValue = 1000;
            minIndex = -1;

            for (int i = 0; i < mPoints.size(); i += 2) {
              Float f = mPoints.get(i);
              if (Math.abs(f - oldX) < minValue) {
                minValue = Math.abs(f - oldX);
                minIndex = i;
              }
            }
            if (index == 0) {
              showValue = String.valueOf((double) mValues.get(minIndex + 1)).replace(".0", "");
              String showExplainT = mExplains.get(minIndex / 2);
              if (showExplainT != null && !"".equals(showExplainT.trim())) {
                showExplain = mExplains.get(minIndex / 2);
              }
            } else {
              showValue =
                  showValue
                      + "/"
                      + String.valueOf((double) mValues.get(minIndex + 1)).replace(".0", "");
              String showExplainT = mExplains.get(minIndex / 2);
              if (showExplainT != null && !"".equals(showExplainT.trim())) {
                if (showExplain != null && !"".equals(showExplain.trim())) {
                  showExplain = showExplain + "/" + mExplains.get(minIndex / 2);
                } else {
                  showExplain = mExplains.get(minIndex / 2);
                }
              }
            }
          }
          if (minValue < 5) {

            boolean showRect = false;
            if (mXYMultipleSeriesRenderer.isDisplayValue0()) {
              showRect = true;
            } else {
              if ("0".equals(showValue) || "0/0".equals(showValue)) {
                showRect = false;
              } else {
                showRect = true;
              }
            }
            if (showRect) {
              // 画框框和点

              // 获取文本的高度
              TextPaint mTextPaint1 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
              mTextPaint1.setColor(Color.WHITE);
              mTextPaint1.setTypeface(Typeface.DEFAULT);
              mTextPaint1.setTextSize(explainTextSize1);

              TextPaint mTextPaint2 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
              mTextPaint2.setColor(Color.WHITE);
              mTextPaint2.setTypeface(Typeface.DEFAULT);
              mTextPaint2.setTextSize(explainTextSize2);

              // 行高
              float hSize1 = AbGraphicUtil.getDesiredHeight(mTextPaint1);

              // 行高
              float hSize2 = AbGraphicUtil.getDesiredHeight(mTextPaint2);

              int textPadding = 8;
              textPadding = AbViewUtil.scale(mContext, textPadding);

              // 设置个新的长方形
              // 判断文字是否超出设置的框框
              int row1 =
                  AbGraphicUtil.getDrawRowCount(
                      showValue, mXYMultipleSeriesRenderer.getScaleRectWidth() - 10, mTextPaint1);
              int row2 =
                  AbGraphicUtil.getDrawRowCount(
                      showExplain, mXYMultipleSeriesRenderer.getScaleRectWidth() - 10, mTextPaint2);
              RectF mRectF = null;
              int textHeight = (int) (row1 * hSize1 + row2 * hSize2 + textPadding);

              // 提示框的宽度和高度
              int realScaleRectWidth = mXYMultipleSeriesRenderer.getScaleRectWidth();
              int realScaleRectHeight = mXYMultipleSeriesRenderer.getScaleRectHeight();

              // 画圆角矩形  //充满
              mPaint.setStyle(Paint.Style.FILL);
              mPaint.setColor(mXYMultipleSeriesRenderer.getScaleRectColor());
              // 设置画笔的锯齿效果
              mPaint.setAntiAlias(true);

              int mRectLeft = (int) (double) mPoints.get(minIndex);
              int mRectTop = (int) (double) mPoints.get(minIndex + 1);
              int mRectRight = mRectLeft + realScaleRectWidth;
              int mRectBottom = mRectTop + realScaleRectHeight;

              if (textHeight > realScaleRectHeight) {
                realScaleRectHeight = textHeight;
                mRectBottom = mRectTop + realScaleRectHeight;
              }

              // 判断是否会超出屏幕
              if (mRectRight > width) {
                // 超出了
                mRectLeft =
                    (int) (double) mPoints.get(minIndex)
                        - 5
                        - mXYMultipleSeriesRenderer.getScaleRectWidth();
                mRectRight = (int) (double) mPoints.get(minIndex) - 5;
              }
              if (mRectBottom > bottomY) {
                // 超出了
                mRectTop = (int) (double) mPoints.get(minIndex + 1) - 5 - realScaleRectHeight;
                mRectBottom = (int) (double) mPoints.get(minIndex + 1) - 5;

                // 是否超出Y=0的位置的
                if (mRectTop < topY) {
                  mRectTop = mRectTop + realScaleRectHeight / 2;
                  mRectBottom = mRectBottom + realScaleRectHeight / 2;
                }
              }

              mRectF = new RectF(mRectLeft, mRectTop, mRectRight, mRectBottom);
              // 第二个参数是x半径,第三个参数是y半径
              canvas.drawRoundRect(mRectF, 5, 5, mPaint);

              AbGraphicUtil.drawText(
                  canvas,
                  showValue,
                  mXYMultipleSeriesRenderer.getScaleRectWidth(),
                  mTextPaint1,
                  mRectLeft + textPadding,
                  mRectTop + textPadding);
              AbGraphicUtil.drawText(
                  canvas,
                  showExplain,
                  mXYMultipleSeriesRenderer.getScaleRectWidth(),
                  mTextPaint2,
                  mRectLeft + textPadding,
                  (int) (mRectTop + textPadding + row1 * hSize1));
            }
          }
        }
      }
    }
  }