コード例 #1
0
 private void updateProgressMessage(int value) {
   if (!isInEditMode()) {
     if (mNumericTransformer.useStringTransform()) {
       mIndicator.setValue(mNumericTransformer.transformToString(value));
     } else {
       mIndicator.setValue(convertValueToMessage(mNumericTransformer.transform(value)));
     }
   }
 }
コード例 #2
0
 /**
  * Sets the current {@link NumericTransformer}
  *
  * @param transformer the transformer
  * @see #getNumericTransformer()
  */
 public void setNumericTransformer(@Nullable NumericTransformer transformer) {
   mNumericTransformer = transformer != null ? transformer : new DefaultNumericTransformer();
   // We need to refresh the PopupIndicator view
   if (!isInEditMode()) {
     if (mNumericTransformer.useStringTransform()) {
       mIndicator.updateSizes(mNumericTransformer.transformToString(mMax));
     } else {
       mIndicator.updateSizes(convertValueToMessage(mNumericTransformer.transform(mMax)));
     }
   }
   updateProgressMessage(mValue);
 }
コード例 #3
0
 private void hideFloater() {
   removeCallbacks(mShowIndicatorRunnable);
   if (!isInEditMode()) {
     mIndicator.dismiss();
     notifyBubble(false);
   }
 }
コード例 #4
0
 private void showFloater() {
   if (!isInEditMode()) {
     mThumb.animateToPressed();
     mIndicator.showIndicator(this, mThumb.getBounds());
     notifyBubble(true);
   }
 }
コード例 #5
0
  private void updateThumbPos(int posX) {
    int thumbWidth = mThumb.getIntrinsicWidth();
    int halfThumb = thumbWidth / 2;
    int start;
    if (isRtl()) {
      start = getWidth() - getPaddingRight() - mAddedTouchBounds;
      posX = start - posX - thumbWidth;
    } else {
      start = getPaddingLeft() + mAddedTouchBounds;
      posX = start + posX;
    }
    mThumb.copyBounds(mInvalidateRect);
    mThumb.setBounds(posX, mInvalidateRect.top, posX + thumbWidth, mInvalidateRect.bottom);
    if (isRtl()) {
      mScrubber.getBounds().right = start - halfThumb;
      mScrubber.getBounds().left = posX + halfThumb;
    } else {
      mScrubber.getBounds().left = start + halfThumb;
      mScrubber.getBounds().right = posX + halfThumb;
    }
    final Rect finalBounds = mTempRect;
    mThumb.copyBounds(finalBounds);
    if (!isInEditMode()) {
      mIndicator.move(finalBounds.centerX());
    }

    mInvalidateRect.inset(-mAddedTouchBounds, -mAddedTouchBounds);
    finalBounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
    mInvalidateRect.union(finalBounds);
    SeekBarCompat.setHotspotBounds(
        mRipple, finalBounds.left, finalBounds.top, finalBounds.right, finalBounds.bottom);
    invalidate(mInvalidateRect);
  }
コード例 #6
0
 @Override
 protected void onDetachedFromWindow() {
   super.onDetachedFromWindow();
   removeCallbacks(mShowIndicatorRunnable);
   if (!isInEditMode()) {
     mIndicator.dismissComplete();
   }
 }
コード例 #7
0
 /**
  * Sets the color of the seek thumb, as well as the color of the popup indicator.
  *
  * @param thumbColorStateList The ColorStateList the seek thumb will be changed to
  * @param indicatorColor The color the popup indicator will be changed to The indicator will
  *     animate from thumbColorStateList(pressed state) to indicatorColor when opening
  */
 public void setThumbColor(@NonNull ColorStateList thumbColorStateList, int indicatorColor) {
   mThumb.setColorStateList(thumbColorStateList);
   // we use the "pressed" color to morph the indicator from it to its own color
   int thumbColor =
       thumbColorStateList.getColorForState(
           new int[] {PRESSED_STATE}, thumbColorStateList.getDefaultColor());
   mIndicator.setColors(indicatorColor, thumbColor);
 }
コード例 #8
0
 @Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
   super.onLayout(changed, left, top, right, bottom);
   if (changed) {
     removeCallbacks(mShowIndicatorRunnable);
     if (!isInEditMode()) {
       mIndicator.dismissComplete();
     }
     updateFromDrawableState();
   }
 }
コード例 #9
0
 /**
  * Sets the color of the seek thumb, as well as the color of the popup indicator.
  *
  * @param thumbColor The color the seek thumb will be changed to
  * @param indicatorColor The color the popup indicator will be changed to The indicator will
  *     animate from thumbColor to indicatorColor when opening
  */
 public void setThumbColor(int thumbColor, int indicatorColor) {
   mThumb.setColorStateList(ColorStateList.valueOf(thumbColor));
   mIndicator.setColors(indicatorColor, thumbColor);
 }
コード例 #10
0
  public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setFocusable(true);
    setWillNotDraw(false);

    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    float density = context.getResources().getDisplayMetrics().density;
    mTrackHeight = (int) (1 * density);
    mScrubberHeight = (int) (4 * density);
    int thumbSize = (int) (density * ThumbDrawable.DEFAULT_SIZE_DP);

    // Extra pixels for a touch area of 48dp
    int touchBounds = (int) (density * 32);
    mAddedTouchBounds = (touchBounds - thumbSize) / 2;

    TypedArray a =
        context.obtainStyledAttributes(
            attrs, R.styleable.DiscreteSeekBar, defStyleAttr, R.style.Widget_DiscreteSeekBar);

    int max = 100;
    int min = 0;
    int value = 0;
    mMirrorForRtl = a.getBoolean(R.styleable.DiscreteSeekBar_dsb_mirrorForRtl, mMirrorForRtl);
    mAllowTrackClick =
        a.getBoolean(R.styleable.DiscreteSeekBar_dsb_allowTrackClickToDrag, mAllowTrackClick);
    mIndicatorPopupEnabled =
        a.getBoolean(R.styleable.DiscreteSeekBar_dsb_indicatorPopupEnabled, mIndicatorPopupEnabled);
    int indexMax = R.styleable.DiscreteSeekBar_dsb_max;
    int indexMin = R.styleable.DiscreteSeekBar_dsb_min;
    int indexValue = R.styleable.DiscreteSeekBar_dsb_value;
    final TypedValue out = new TypedValue();
    // Not sure why, but we wanted to be able to use dimensions here...
    if (a.getValue(indexMax, out)) {
      if (out.type == TypedValue.TYPE_DIMENSION) {
        max = a.getDimensionPixelSize(indexMax, max);
      } else {
        max = a.getInteger(indexMax, max);
      }
    }
    if (a.getValue(indexMin, out)) {
      if (out.type == TypedValue.TYPE_DIMENSION) {
        min = a.getDimensionPixelSize(indexMin, min);
      } else {
        min = a.getInteger(indexMin, min);
      }
    }
    if (a.getValue(indexValue, out)) {
      if (out.type == TypedValue.TYPE_DIMENSION) {
        value = a.getDimensionPixelSize(indexValue, value);
      } else {
        value = a.getInteger(indexValue, value);
      }
    }

    mMin = min;
    mMax = Math.max(min + 1, max);
    mValue = Math.max(min, Math.min(max, value));
    updateKeyboardRange();

    mIndicatorFormatter = a.getString(R.styleable.DiscreteSeekBar_dsb_indicatorFormatter);

    ColorStateList trackColor = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_trackColor);
    ColorStateList progressColor =
        a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_progressColor);
    ColorStateList rippleColor = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_rippleColor);
    boolean editMode = isInEditMode();
    if (editMode || rippleColor == null) {
      rippleColor = new ColorStateList(new int[][] {new int[] {}}, new int[] {Color.DKGRAY});
    }
    if (editMode || trackColor == null) {
      trackColor = new ColorStateList(new int[][] {new int[] {}}, new int[] {Color.GRAY});
    }
    if (editMode || progressColor == null) {
      progressColor =
          new ColorStateList(new int[][] {new int[] {}}, new int[] {DEFAULT_THUMB_COLOR});
    }
    mRipple = SeekBarCompat.getRipple(rippleColor);
    if (isLollipopOrGreater) {
      SeekBarCompat.setBackground(this, mRipple);
    } else {
      mRipple.setCallback(this);
    }

    TrackRectDrawable shapeDrawable = new TrackRectDrawable(trackColor);
    mTrack = shapeDrawable;
    mTrack.setCallback(this);

    shapeDrawable = new TrackRectDrawable(progressColor);
    mScrubber = shapeDrawable;
    mScrubber.setCallback(this);

    mThumb = new ThumbDrawable(progressColor, thumbSize);
    mThumb.setCallback(this);
    mThumb.setBounds(0, 0, mThumb.getIntrinsicWidth(), mThumb.getIntrinsicHeight());

    if (!editMode) {
      mIndicator = new PopupIndicator(context, attrs, defStyleAttr, convertValueToMessage(mMax));
      mIndicator.setListener(mFloaterListener);
    }
    a.recycle();

    setNumericTransformer(new DefaultNumericTransformer());
  }