拇指图像略位于拇指左侧

时间:2018-06-24 16:59:43

标签: android rangeseekbar

enter image description here我在CrysalRangeBar中进行了一些编辑,以在拇指上显示文本,虽然我找到了一些成功,但是我遇到了一个问题,即ThumbImage在原始拇指的左侧是slighlty如果必须滑动拇指,则必须将其从拇指的右侧空白区域拖动。总的来说,我想说图像拇指没有正确放置在拇指上。如果我删除所有编辑内容,则缩略图位于正确的位置。

下面是CrystalRangeBar.Java的代码

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // prevent render is in edit mode
        if (isInEditMode()) return;

        // setup bar
        setupBar(canvas, _paint, _rect);

        // setup seek bar active range line
        setupHighlightBar(canvas, _paint, _rect);

        // draw left thumb
        //  setupLeftThumb(canvas, _paint, _rect);
        // getSelectedMinValue(), getSelectedMaxValue()
        String minText = String.valueOf(getSelectedMinValue());
        String maxText = String.valueOf(getSelectedMaxValue());
        boolean selectedValuesAreDefaultMax = (maxText.equals(String.valueOf((int) absoluteMaxValue)));
        boolean selectedValuesAreDefaultMin = (minText.equals(String.valueOf((int) absoluteMinValue)));

        leftThumbColor = (Thumb.MIN.equals(pressedThumb)) ? leftThumbColorPressed : leftThumbColorNormal;
        rightThumbColor = (Thumb.MAX.equals(pressedThumb)) ? rightThumbColorPressed : rightThumbColorNormal;
        _paint.setColor(leftThumbColor);
        _paint.setStyle(Paint.Style.FILL);
        _paint.setTextSize(mTextSize);
        _paint.setAntiAlias(true);
        float minMaxLabelSize = 0;

        padding = mInternalPad + minMaxLabelSize + mThumbHalfHeight;

        // draw seek bar background line
        mRect.left = padding;
        mRect.right = getWidth() - padding;
        canvas.drawRect(mRect, _paint);
/*
        mRect.left = normalizedToScreen(normalizedMinValue);
        mRect.right = normalizedToScreen(normalizedMaxValue);*/

        int offset = PixelUtil.dpToPx(getContext(), TEXT_LATERAL_PADDING_IN_DP);


        if (!selectedValuesAreDefaultMax) {

            float maxTextWidth = _paint.measureText(maxText) + offset;
            float maxPosition = Math.min(getWidth() - maxTextWidth, normalizedToScreen(normalizedMaxValue) - maxTextWidth * 0.5f);
            canvas.drawText(maxText,
                    maxPosition,
                    mDistanceToTop + mTextSize,
                    _paint);


        }


        if (!selectedValuesAreDefaultMin) {

            float minTextWidth = _paint.measureText(minText) + offset;
            float minPosition = Math.max(0f, normalizedToScreen(normalizedMinValue) - minTextWidth * 0.5f);

            canvas.drawText(minText,
                    minPosition,
                    mDistanceToTop + mTextSize,
                    _paint);

        }


        rectLeftThumb.left = normalizedToScreen(normalizedMinValue);
        rectLeftThumb.right = Math.min(rectLeftThumb.left + mThumbHalfWidth + barPadding, getWidth());
        rectLeftThumb.bottom = thumbHeight;

        rectRightThumb.left = normalizedToScreen(normalizedMaxValue);
        rectRightThumb.right = Math.min(rectRightThumb.left + mThumbHalfWidth + barPadding, getWidth());
        rectRightThumb.bottom = thumbHeight;

        if (leftThumb != null) {
            drawLeftThumb(normalizedToScreen(normalizedMinValue), Thumb.MIN.equals(pressedThumb), canvas, selectedValuesAreDefaultMin);
        } else {
            drawLeftThumbWithColor(canvas, _paint, rectLeftThumb);
        }

        if (rightThumb != null) {
            drawRightThumb(normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas, selectedValuesAreDefaultMax);
        } else {
            drawRightThumbWithColor(canvas, _paint, rectRightThumb);
        }
        // draw right thumb
        //setupRightThumb(canvas, _paint, _rect);

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // setMeasuredDimension(getMeasureSpecWith(widthMeasureSpec), getMeasureSpecHeight(heightMeasureSpec));
        int width = 200;
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        int height = rightThumb.getHeight()
                + PixelUtil.dpToPx(getContext(), HEIGHT_IN_DP);
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)) {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }
        setMeasuredDimension(width, height);
    }

    /**
     * Handles thumb selection and movement. Notifies listener callback on certain events.
     */
    @Override
    public synchronized boolean onTouchEvent(MotionEvent event) {

        if (!isEnabled())
            return false;


        final int action = event.getAction();

        switch (action & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mActivePointerId = event.getPointerId(event.getPointerCount() - 1);
                pointerIndex = event.findPointerIndex(mActivePointerId);
                float mDownMotionX = event.getX(pointerIndex);

                pressedThumb = evalPressedThumb(mDownMotionX);

                if (pressedThumb == null) return super.onTouchEvent(event);

                touchDown(event.getX(pointerIndex), event.getY(pointerIndex));
                setPressed(true);
                invalidate();
                onStartTrackingTouch();
                trackTouchEvent(event);
                attemptClaimDrag();

                break;

            case MotionEvent.ACTION_MOVE:
                if (pressedThumb != null) {

                    if (mIsDragging) {
                        touchMove(event.getX(pointerIndex), event.getY(pointerIndex));
                        trackTouchEvent(event);
                    }

                    if (onRangeSeekbarChangeListener != null) {
                        onRangeSeekbarChangeListener.valueChanged(getSelectedMinValue(), getSelectedMaxValue());
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if (mIsDragging) {
                    trackTouchEvent(event);
                    onStopTrackingTouch();
                    setPressed(false);
                    touchUp(event.getX(pointerIndex), event.getY(pointerIndex));
                    if (onRangeSeekbarFinalValueListener != null) {
                        onRangeSeekbarFinalValueListener.finalValue(getSelectedMinValue(), getSelectedMaxValue());
                    }
                } else {
                    // Touch up when we never crossed the touch slop threshold
                    // should be interpreted as a tap-seek to that location.
                    onStartTrackingTouch();
                    trackTouchEvent(event);
                    onStopTrackingTouch();
                }

                pressedThumb = null;
                invalidate();
                if (onRangeSeekbarChangeListener != null) {
                    onRangeSeekbarChangeListener.valueChanged(getSelectedMinValue(), getSelectedMaxValue());
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN: {
                //final int index = event.getPointerCount() - 1;
                // final int index = ev.getActionIndex();
                /*mDownMotionX = event.getX(index);
                mActivePointerId = event.getPointerId(index);
                invalidate();*/
                break;
            }
            case MotionEvent.ACTION_POINTER_UP:
                /*onSecondaryPointerUp(event);*/
                invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
                if (mIsDragging) {
                    onStopTrackingTouch();
                    setPressed(false);
                    touchUp(event.getX(pointerIndex), event.getY(pointerIndex));
                }
                invalidate(); // see above explanation
                break;
        }

        return true;

    }
}

1 个答案:

答案 0 :(得分:1)

请尝试以下代码

LEFT JOIN