Ejemplo n.º 1
0
  private void stroke(Seria s, Canvas canvas) {
    mPlotPaint.setStyle(Paint.Style.STROKE);
    // draw line
    if (s.mWidth > 0) {
      // create the path
      mPath.reset();
      mPath.moveTo(calcX(s.x[0]), calcY(s.y[0]));
      for (int i = 1; i < s.sz(); i++) {
        mPath.lineTo(calcX(s.x[i]), calcY(s.y[i]));
      }
      // draw the path
      mPlotPaint.setColor(s.mColor);
      mPlotPaint.setStrokeWidth(s.mWidth);
      mPlotPaint.setPathEffect(s.mEffect);
      canvas.drawPath(mPath, mPlotPaint);
    }

    // draw images seria
    if (s.bms != null) {
      for (int i = 0; i < s.sz(); i++) {
        Bitmap bm;
        if (s.bms.length > 1) {
          bm = s.bms[i];
        } else {
          bm = s.bms[0];
        }
        float left = calcX(s.x[i]);
        float top = calcY(s.y[i]);
        if (s.hAlign == HALIGN.CENTER) {
          left -= bm.getWidth() / 2;
        } else if (s.hAlign == HALIGN.RIGHT) {
          left -= bm.getWidth();
        }
        if (s.vAlign == VALIGN.CENTER) {
          top -= bm.getHeight() / 2;
        } else if (s.vAlign == VALIGN.BOTTOM) {
          top -= bm.getHeight();
        }
        canvas.drawBitmap(s.bms[i], left, top, mPlotPaint);
      }
    }
    if (s.texts != null) {
      Align align = Align.CENTER;
      switch (s.hAlign) {
        case LEFT:
          align = Align.RIGHT;
        case RIGHT:
          align = Align.LEFT;
      }

      mPlotPaint.setTextAlign(align);
      mPlotPaint.setStrokeWidth(0);
      mPlotPaint.setColor(s.mColor);
      for (int i = 0; i < s.sz(); i++) {
        float x = calcX(s.x[i]);
        float y = calcY(s.y[i]);
        switch (s.vAlign) {
          case TOP:
            y -= mPlotPaint.getTextSize();
          case CENTER:
            y -= 0.5 * mPlotPaint.getTextSize();
        }
        if (s.deg != 0) {
          canvas.save();
          canvas.rotate(s.deg, x, y);
        }
        canvas.drawText(s.texts[i], x, y, mPlotPaint);
        if (s.deg != 0) {
          canvas.restore();
        }
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Initialize the control. This code is in a separate method so that it can be called from both
   * constructors.
   */
  private void init() {
    // Force the background to software rendering because otherwise the Blur
    // filter won't work.
    setLayerToSW(this);

    // Set up the paint for the label text
    mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setColor(mTextColor);
    if (mTextHeight == 0) {
      mTextHeight = mTextPaint.getTextSize();
    } else {
      mTextPaint.setTextSize(mTextHeight);
    }

    // Set up the paint for the pie slices
    mPiePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPiePaint.setStyle(Paint.Style.FILL);
    mPiePaint.setTextSize(mTextHeight);

    cSlicePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    cSlicePaint.setStyle(Paint.Style.FILL);

    // Set up the paint for the shadow
    mShadowPaint = new Paint(0);
    mShadowPaint.setColor(0xff101010);
    mShadowPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));

    // Add a child view to draw the pie. Putting this in a child view
    // makes it possible to draw it on a separate hardware layer that rotates
    // independently
    mPieView = new PieView(getContext());
    addView(mPieView);
    mPieView.rotateTo(mPieRotation);

    // The pointer doesn't need hardware acceleration, but in order to show up
    // in front of the pie it also needs to be on a separate view.
    mPointerView = new PointerView(getContext());
    addView(mPointerView);

    // Set up an animator to animate the PieRotation property. This is used to
    // correct the pie's orientation after the user lets go of it.
    if (Build.VERSION.SDK_INT >= 11) {
      mAutoCenterAnimator = ObjectAnimator.ofInt(PieChart.this, "PieRotation", 0);

      // Add a listener to hook the onAnimationEnd event so that we can do
      // some cleanup when the pie stops moving.
      mAutoCenterAnimator.addListener(
          new Animator.AnimatorListener() {
            public void onAnimationStart(Animator animator) {}

            public void onAnimationEnd(Animator animator) {
              mPieView.decelerate();
            }

            public void onAnimationCancel(Animator animator) {}

            public void onAnimationRepeat(Animator animator) {}
          });
    }

    // Create a Scroller to handle the fling gesture.
    if (Build.VERSION.SDK_INT < 11) {
      mScroller = new Scroller(getContext());
    } else {
      mScroller = new Scroller(getContext(), null, true);
    }
    // The scroller doesn't have any built-in animation functions--it just supplies
    // values when we ask it to. So we have to have a way to call it every frame
    // until the fling ends. This code (ab)uses a ValueAnimator object to generate
    // a callback on every animation frame. We don't use the animated value at all.
    if (Build.VERSION.SDK_INT >= 11) {
      mScrollAnimator = ValueAnimator.ofFloat(0, 1);
      mScrollAnimator.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
              tickScrollAnimation();
            }
          });
    }

    // Create a gesture detector to handle onTouch messages
    mDetector = new GestureDetector(PieChart.this.getContext(), new GestureListener());

    // Turn off long press--this control doesn't use it, and if long press is enabled,
    // you can't scroll for a bit, pause, then scroll some more (the pause is interpreted
    // as a long press, apparently)
    mDetector.setIsLongpressEnabled(false);
  }