/** 设置选中状态下按钮的背景颜色 */
  public void setSelectedBackgroundColors(int backgroundColors) {
    mSelectedBackgroundColors = backgroundColors;

    if (mBackgroundDrawable != null) {
      mBackgroundDrawable.setStrokeColor(backgroundColors);
    }

    if (mSelectedDrawable != null) {
      mSelectedDrawable.setColor(backgroundColors);
    }

    mPaint.setColor(backgroundColors);

    invalidate();
  }
  public void setCornerRadius(int cornerRadius) {
    mCornerRadius = cornerRadius;

    if (mBackgroundDrawable != null) {
      mBackgroundDrawable.setRadius(cornerRadius);
    }

    invalidate();
  }
  public SegmentControl(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SegmentControl);

    String textArray = ta.getString(R.styleable.SegmentControl_texts);

    if (textArray != null) {
      mTexts = textArray.split("\\|");
    }

    mTextSize =
        ta.getDimensionPixelSize(
            R.styleable.SegmentControl_android_textSize,
            (int)
                TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_SP, 14, context.getResources().getDisplayMetrics()));
    mSelectedBackgroundColors =
        ta.getColor(R.styleable.SegmentControl_selectedBackgroundColors, 0xffffffff);
    mSelectedTextColors = ta.getColor(R.styleable.SegmentControl_selectedTextColors, 0xffffffff);
    mCornerRadius =
        ta.getDimensionPixelSize(
            R.styleable.SegmentControl_cornerRadius,
            (int)
                TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 5, context.getResources().getDisplayMetrics()));
    mDirection = Direction.values()[ta.getInt(R.styleable.SegmentControl_direction, 0)];

    mHorizonGap = ta.getDimensionPixelSize(R.styleable.SegmentControl_horizonGap, 0);
    mVerticalGap = ta.getDimensionPixelSize(R.styleable.SegmentControl_verticalGap, 0);

    mCurrentIndex = ta.getInt(R.styleable.SegmentControl_selectedIndex, 0); // 默认选中第一个
    if (mTexts != null && mTexts.length > 0 && mCurrentIndex > mTexts.length - 1) {
      mCurrentIndex = mTexts.length - 1;
    }

    int gap =
        ta.getDimensionPixelSize(
            R.styleable.SegmentControl_gaps,
            (int)
                TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 2, context.getResources().getDisplayMetrics()));

    if (mHorizonGap == 0) mHorizonGap = gap;
    if (mVerticalGap == 0) mVerticalGap = gap;

    ta.recycle();

    mBackgroundDrawable = new RadiusDrawable(mCornerRadius, true, 0);
    mBackgroundDrawable.setStrokeWidth(2);
    mBackgroundDrawable.setStrokeColor(mSelectedBackgroundColors);

    if (Build.VERSION.SDK_INT < 16) {
      setBackgroundDrawable(mBackgroundDrawable);
    } else {
      setBackground(mBackgroundDrawable);
    }

    mSelectedDrawable = new RadiusDrawable(mCornerRadius, false, mSelectedBackgroundColors);

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setTextSize(mTextSize);
    mPaint.setColor(mSelectedBackgroundColors);

    // here's the tricky thing, when you doing a click detect on a capacitive touch screen,
    // sometimes the touch points of touchDown and touchUp are different(it's call slop) even when
    // you didn't actually move your finger,
    // so we set a distance limit for the distance of this two touch points to create a better user
    // experience;
    int touchSlop = 0;

    if (context == null) {
      touchSlop = ViewConfiguration.getTouchSlop();
    } else {
      final ViewConfiguration config = ViewConfiguration.get(context);
      touchSlop = config.getScaledTouchSlop();
    }

    mTouchSlop = touchSlop * touchSlop;
    inTapRegion = false;
  }
  @Override
  public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mTexts != null && mTexts.length > 0) {
      for (int i = 0; i < mTexts.length; i++) {

        // draw separate lines
        if (i < mTexts.length - 1) {
          mPaint.setColor(mSelectedBackgroundColors);
          mPaint.setStrokeWidth(2);
          if (mDirection == Direction.HORIZON) {
            canvas.drawLine(mCacheBounds[i].right, 0, mCacheBounds[i].right, getHeight(), mPaint);
          } else {
            canvas.drawLine(
                mCacheBounds[i].left,
                mSingleChildHeight * (i + 1),
                mCacheBounds[i].right,
                mSingleChildHeight * (i + 1),
                mPaint);
          }
        }

        // draw selected drawable
        if (i == mCurrentIndex && mSelectedDrawable != null) {
          int topLeftRadius = 0;
          int topRightRadius = 0;
          int bottomLeftRadius = 0;
          int bottomRightRadius = 0;

          if (mDirection == Direction.HORIZON) {
            if (i == 0) {
              topLeftRadius = mCornerRadius;
              bottomLeftRadius = mCornerRadius;
            }
            if (i == mTexts.length - 1) {
              topRightRadius = mCornerRadius;
              bottomRightRadius = mCornerRadius;
            }
          } else {
            if (i == 0) {
              topLeftRadius = mCornerRadius;
              topRightRadius = mCornerRadius;
            }
            if (i == mTexts.length - 1) {
              bottomLeftRadius = mCornerRadius;
              bottomRightRadius = mCornerRadius;
            }
          }

          mSelectedDrawable.setRadiuses(
              topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
          mSelectedDrawable.setBounds(mCacheBounds[i]);
          mSelectedDrawable.draw(canvas);

          mPaint.setColor(mSelectedTextColors);
        } else {
          mPaint.setColor(mSelectedBackgroundColors);
        }

        // draw texts
        canvas.drawText(
            mTexts[i],
            mCacheBounds[i].left + (mSingleChildWidth - mTextBounds[i].width()) / 2,
            mCacheBounds[i].top + (mSingleChildHeight - mPaint.ascent() - mPaint.descent()) / 2,
            mPaint);
      }
    }
  }