将搜索栏拇指设置在搜索栏线上方

Set seekbar thumb above seekbar line

在我的 Android 应用程序中,我必须自定义一个搜索栏,我想知道如何将搜索栏拇指设置在进度线上方而不是默认居中?

你可以看看Discrete Seekbar

seekBar.setMin(0);
seekBar.setMax(yourArray.length);

seekBar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
    int onProgressChanged =0;
    @Override
    public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
        onProgressChanged = value;

    }

    @Override
    public void onStartTrackingTouch(DiscreteSeekBar seekBar) {

        }

    @Override
    public void onStopTrackingTouch(DiscreteSeekBar seekBar) {

    }
});

我在许多帖子中查找了此信息并从那里和这里获得了一些想法,并创建了 SeekBayHint,它在箭头上方创建进度文本:

SeekBar

主类:

package your_package;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import your_package.R;

public class SeekBarHint extends android.support.v7.widget.AppCompatSeekBar {

    private Paint mTextPaint;
    private Rect mTextBounds = new Rect();
    private Bitmap mBitmapIconArrowDown;

    /**  for this class yPosition must be as minHeight in layoutFile for this seekBar*/
    private static int sTextYPositionIndent = 20;
    private float mTextSizeDecrease = 1.75f;

    public static void setTextYPositionIndent(int textYPositionIndent) {
        sTextYPositionIndent = textYPositionIndent;
    }

    public SeekBarHint(Context context) {
        super(context);
        mBitmapIconArrowDown = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.progress_seek_bar_arrow_down);
        mTextPaint = new Paint();
        mTextPaint.setColor(getResources().getColor(R.color.colorPrimary));

    }

    public SeekBarHint(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBitmapIconArrowDown = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.progress_seek_bar_arrow_down);
        mTextPaint = new Paint();
        mTextPaint.setColor(getResources().getColor(R.color.colorPrimary));

    }

    public SeekBarHint(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mBitmapIconArrowDown = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.progress_seek_bar_arrow_down);
        mTextPaint = new Paint();
        mTextPaint.setColor(getResources().getColor(R.color.colorPrimary));
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        //  first draw the regular progress bar, then custom draw our textString
        super.onDraw(canvas);

        //  now progress position and convert to textString.
        String textString = Integer.toString(getProgress()) + "%";

        //  now get size of seek bar.
        float width = getWidth();
        float height = getHeight();

        //  set textString size.
        mTextPaint.setTextSize(height / mTextSizeDecrease);

        //  get size of textString.
        mTextPaint.getTextBounds(textString, 0, textString.length(), mTextBounds);

        //  calculate where to start printing textString.
        float position = (width / getMax()) * getProgress();

        //  get start and end points of where textString will be printed.
        float textXStart = position - mTextBounds.centerX();
        float textXEnd = position + mTextBounds.centerX();

        //  check does not start drawing text outside seek bar.
        if (textXStart < 0)
            textXStart = 0;

        if (textXEnd > width)
            textXStart -= (textXEnd - width);

        //  calculate y textString print position.
        float yPosition = (height / 2) - mTextBounds.centerY();
        canvas.drawText(textString, textXStart, yPosition - sTextYPositionIndent, mTextPaint);

        //  arrow draw logic
        //  check does not start drawing arrow outside seek bar
        int seekBarAbsoluteWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        int thumbPos = (getPaddingLeft() / 2) + (seekBarAbsoluteWidth * getProgress() / getMax());

        //  set height and width for new bitmap
        int arrowHeight = Math.round(mTextBounds.height()/2f);
        int arrowWidth = mTextBounds.width()/3;

        Bitmap scaledBitmapIconArrowDown = Bitmap
                .createScaledBitmap(mBitmapIconArrowDown, arrowWidth, arrowHeight, true);

        canvas.drawBitmap(scaledBitmapIconArrowDown, thumbPos, yPosition, null);
    }
}

要支持不同维度,请在 OnCreateView(如果在片段中使用 seekBar)或在 OnCreate(如果在 activity 中使用 seekBar)中使用此代码:

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int dpi = metrics.densityDpi;

    if(dpi < 230){
        SeekBarHint.setTextYPositionIndent(5);
    } else if (dpi < 310){
        SeekBarHint.setTextYPositionIndent(15);
    } else if (dpi < 470){
        SeekBarHint.setTextYPositionIndent(10);
    }