Beispiel #1
0
  @Override
  protected synchronized void onDraw(Canvas canvas) {
    if ((mThumb != null) && (mDots.size() > 1)) {
      if (isSelected) {
        for (Dot dot : mDots) {
          if (dot.isSelected) {
            Rect bounds = mThumb.copyBounds();
            bounds.right = dot.mX;
            bounds.left = dot.mX;
            mThumb.setBounds(bounds);
            break;
          }
        }
      } else {
        int intervalWidth = mDots.get(1).mX - mDots.get(0).mX;
        Rect bounds = mThumb.copyBounds();
        // find nearest dot
        if ((mDots.get(mDots.size() - 1).mX - bounds.centerX()) < 0) {
          bounds.right = mDots.get(mDots.size() - 1).mX;
          bounds.left = mDots.get(mDots.size() - 1).mX;
          mThumb.setBounds(bounds);

          for (Dot dot : mDots) {
            dot.isSelected = false;
          }
          mDots.get(mDots.size() - 1).isSelected = true;
          handleClick(mDots.get(mDots.size() - 1));
        } else {
          for (int i = 0; i < mDots.size(); i++) {
            if (Math.abs(mDots.get(i).mX - bounds.centerX()) <= (intervalWidth / 2)) {
              bounds.right = mDots.get(i).mX;
              bounds.left = mDots.get(i).mX;
              mThumb.setBounds(bounds);
              mDots.get(i).isSelected = true;
              handleClick(mDots.get(i));
            } else {
              mDots.get(i).isSelected = false;
            }
          }
        }
      }
    }
    super.onDraw(canvas);
  }