Example #1
0
  /** Do all of the recalculations needed when the data array changes. */
  private void onDataChanged() {
    int angle = 360 / mData.size();
    // This is the extra degrees that cannot be equally divided by the number of sides
    int extra = 360 % mData.size();

    // When the data changes, we have to recalculate
    // all of the angles.
    int currentAngle = 0;
    for (Item it : mData) {

      // Adds the extra degrees to the last slice to ensue no gaps
      if (mData.get(mData.size() - 1).mLabel.equals(it.mLabel)) {
        it.mEndAngle = currentAngle + angle + extra;
      } else {

        it.mEndAngle = currentAngle + angle;
      }
      it.mStartAngle = currentAngle;
      currentAngle = it.mEndAngle;

      // Recalculate the gradient shaders. There are
      // three values in this gradient, even though only
      // two are necessary, in order to work around
      // a bug in certain versions of the graphics engine
      // that expects at least three values if the
      // positions array is non-null.
      //
      it.mItemShader =
          new SweepGradient(
              mPieBounds.width() / 2.0f,
              mPieBounds.height() / 2.0f,
              new int[] {
                it.mItemHighlight, it.mItemHighlight, it.mItemColor, it.mItemColor,
              },
              new float[] {
                0,
                (float) (360 - it.mEndAngle) / 360.0f,
                (float) (360 - it.mStartAngle) / 360.0f,
                1.0f
              });

      it.cSliceShader =
          new SweepGradient(
              mPieBounds.width() / 2.0f,
              mPieBounds.height() / 2.0f,
              new int[] {
                it.cSliceHighlight, it.cSliceHighlight, it.cSliceColor, it.cSliceColor,
              },
              new float[] {
                0,
                (float) (360 - it.mEndAngle) / 360.0f,
                (float) (360 - it.mStartAngle) / 360.0f,
                1.0f
              });
    }
    calcCurrentItem();
    onScrollFinished();
  }