将seekbar thumb设置在seekbar线上方

时间:2016-07-22 03:05:58

标签: android android-layout seekbar

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

2 个答案:

答案 0 :(得分:1)

您可以查看Discrete Seekbar

enter image description here

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) {

    }
});

答案 1 :(得分:0)

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

SeekBar

MainClass:

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中使用(如果在活动中使用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);
    }