/** Draws the widget on the given canvas. */ @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); // draw seek bar background line final RectF rect = new RectF( padding, 0.5f * (getHeight() - lineHeight), getWidth() - padding, 0.5f * (getHeight() + lineHeight)); /*paint.setStyle(Style.FILL); paint.setColor(getResources().getColor(R.color.notification_baf_color)); paint.setAntiAlias(true); canvas.drawRect(rect, paint);*/ // draw seek bar active range line rect.left = normalizedToScreen(normalizedMinValue); rect.right = normalizedToScreen(normalizedMaxValue); // orange color paint.setColor(getResources().getColor(R.color.white)); canvas.drawRect(rect, paint); // draw minimum thumb drawThumb(normalizedToScreen(normalizedMinValue), Thumb.MIN.equals(pressedThumb), canvas); // draw maximum thumb drawThumb(normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas); }
/** Handles thumb selection and movement. Notifies listener callback on certain events. */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: pressedThumb = evalPressedThumb(event.getX()); invalidate(); break; case MotionEvent.ACTION_MOVE: if (pressedThumb != null) { if (Thumb.MIN.equals(pressedThumb)) { setNormalizedMinValue(screenToNormalized(event.getX())); } else if (Thumb.MAX.equals(pressedThumb)) { setNormalizedMaxValue(screenToNormalized(event.getX())); } if (notifyWhileDragging && listener != null) { listener.rangeSeekBarValuesChanged(getSelectedMinValue(), getSelectedMaxValue()); } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: pressedThumb = null; invalidate(); if (listener != null) { listener.rangeSeekBarValuesChanged(getSelectedMinValue(), getSelectedMaxValue()); } break; } return true; }
private void trackTouchEvent(MotionEvent event) { final int pointerIndex = event.findPointerIndex(mActivePointerId); final float x = event.getX(pointerIndex); if (Thumb.MIN.equals(pressedThumb) && !mSingleThumb) { setNormalizedMinValue(screenToNormalized(x)); } else if (Thumb.MAX.equals(pressedThumb)) { setNormalizedMaxValue(screenToNormalized(x)); } }
/** * Draws the "normal" resp. "pressed" thumb image on specified x-coordinate. * * @param screenCordX The x-coordinate in screen space where to draw the image. * @param pressed Is the thumb currently in "pressed" state? * @param canvas The canvas to draw upon. */ private void drawThumb(Canvas canvas, boolean areSelectedValuesDefault, boolean isMinThumb) { boolean pressed = isMinThumb ? Thumb.MIN.equals(pressedThumb) : Thumb.MAX.equals(pressedThumb); Bitmap buttonToDraw; if (areSelectedValuesDefault) { buttonToDraw = thumbDisabledImage; } else { if (isMinThumb) { buttonToDraw = pressed ? thumbPressedImage : thumbImage; } else { buttonToDraw = pressed ? maxThumbPressedImage : maxThumbImage; } } float drawScreenCordX = calculateScreenCordX(isMinThumb); float drawScreenCordY = calculateScreenCordY(isMinThumb); canvas.drawBitmap(buttonToDraw, drawScreenCordX, drawScreenCordY, paint); }
/** Draws the widget on the given canvas. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // draw seek bar background line RectF rect = new RectF(padding, 0, getWidth() - padding, lineHeight); paint.setStyle(Style.FILL); paint.setColor(Color.GRAY); canvas.drawRect(rect, paint); // draw seek bar active range line rect.left = normalizedToScreen(normalizedMinValue); rect.right = normalizedToScreen(normalizedMaxValue); // orange color paint.setColor(Color.rgb(255, 255, 255)); canvas.drawRect(rect, paint); // draw minimum thumb drawThumb(normalizedToScreen(normalizedMinValue), Thumb.MIN.equals(pressedThumb), canvas); // draw maximum thumb drawThumb(normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas); }
/** Draws the widget on the given canvas. */ @Override protected synchronized void onDraw(@NonNull Canvas canvas) { super.onDraw(canvas); paint.setTextSize(mTextSize); paint.setStyle(Style.FILL); paint.setColor(mDefaultColor); paint.setAntiAlias(true); float minMaxLabelSize = 0; if (mShowLabels) { // draw min and max labels String minLabel = getContext().getString(R.string.demo_min_label); String maxLabel = getContext().getString(R.string.demo_max_label); minMaxLabelSize = Math.max(paint.measureText(minLabel), paint.measureText(maxLabel)); float minMaxHeight = mTextOffset + mThumbHalfHeight + mTextSize / 3; canvas.drawText(minLabel, 0, minMaxHeight, paint); canvas.drawText(maxLabel, getWidth() - minMaxLabelSize, minMaxHeight, paint); } padding = mInternalPad + minMaxLabelSize + mThumbHalfWidth; // draw seek bar background line mRect.left = padding; mRect.right = getWidth() - padding; canvas.drawRoundRect(mRect, mRoundedCorners, mRoundedCorners, paint); boolean selectedValuesAreDefault = (getSelectedMinValue().equals(getAbsoluteMinValue()) && getSelectedMaxValue().equals(getAbsoluteMaxValue())); int colorToUseForButtonsAndHighlightedLine = !mAlwaysActive && selectedValuesAreDefault ? mDefaultColor : // default values mActiveColor; // non default, filter is active // draw seek bar active range line mRect.left = normalizedToScreen(normalizedMinValue); mRect.right = normalizedToScreen(normalizedMaxValue); paint.setColor(colorToUseForButtonsAndHighlightedLine); canvas.drawRoundRect(mRect, mRoundedCorners, mRoundedCorners, paint); // draw minimum thumb if not a single thumb control if (!mSingleThumb) { drawThumb( normalizedToScreen(normalizedMinValue), Thumb.MIN.equals(pressedThumb), canvas, selectedValuesAreDefault); } // draw maximum thumb drawThumb( normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas, selectedValuesAreDefault); // draw the text if sliders have moved from default edges if (mShowTextAboveThumbs) { paint.setTextSize(mTextSize); paint.setColor(mTextAboveThumbsColor); // give text a bit more space here so it doesn't get cut off int offset = PixelUtil.dpToPx(getContext(), TEXT_LATERAL_PADDING_IN_DP); String minText = String.valueOf(getSelectedMinValue()); String maxText = String.valueOf(getSelectedMaxValue()); if (mValueProcessor != null) { minText = mValueProcessor.getProcessedValue(getSelectedMinValue()); maxText = mValueProcessor.getProcessedValue(getSelectedMaxValue()); } float minTextWidth = paint.measureText(minText) + offset; float maxTextWidth = paint.measureText(maxText) + offset; if (!mSingleThumb) { canvas.drawText( minText, normalizedToScreen(normalizedMinValue) - minTextWidth * 0.5f, mDistanceToTop + mTextSize, paint); } canvas.drawText( maxText, normalizedToScreen(normalizedMaxValue) - maxTextWidth * 0.5f, mDistanceToTop + mTextSize, paint); } }