如何在Android下的seekbar下显示分频器值?

时间:2015-10-26 11:04:00

标签: android android-seekbar

我在其中一项活动中添加了搜索栏。 它的最大值是5.现在,我想在搜索条下方显示分频器值(增量为1,如0,1,2,3,4和5)。我怎么能这样做?

有没有任何系统方法可以实现这一点,我无法动手?欢迎任何投入。

注意:我想以编程方式应用任何更改,而不是从xml应用。数字应以相等的间隔分开。我确实无法编辑它。

enter image description here

2 个答案:

答案 0 :(得分:2)

我想你想在图片中显示如下所示的视图。

enter image description here

如果是这种情况,你必须创建自己的customSeekbar,比如给代码。

<强> CustomSeekBar.java

public class CustomSeekBar extends SeekBar {

private Paint textPaint;

private Rect textBounds = new Rect();

private String text = "";

public CustomSeekBar(Context context) {
    super(context);
    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);

}

public CustomSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    textPaint = new Paint();
    textPaint.setTypeface(Typeface.SANS_SERIF);
    textPaint.setColor(Color.BLACK);

}

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);


    textPaint = new Paint();
    textPaint.setColor(Color.BLACK);
}

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


    int progress = getProgress();
    text = progress + "";

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

    // Set text size.
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setTextSize(40);
    // Get size of text.
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

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

    // Get start and end points of where text will be printed.
    float textXStart = position - textBounds.centerX();
    float textXEnd = position + textBounds.centerX();

    // Check does not start drawing outside seek bar.
    if (textXStart <= 1) textXStart = 20;

    if (textXEnd > width) {
        textXStart -= (textXEnd - width + 30);
    }
    // Calculate y text print position.
    float yPosition = height;

    canvas.drawText(text, textXStart, yPosition, textPaint);
}

public synchronized void setTextColor(int color) {
    super.drawableStateChanged();
    textPaint.setColor(color);
    drawableStateChanged();
}


}

在您的Xml文件中使用您的自定义文件,如下所示

 <com.waleedsarwar.customseekbar.CustomSeekBar
    android:id="@+id/seekbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="5"
    android:paddingBottom="16dp" />

答案 1 :(得分:0)

这是另一种方法。我正在扩展一个线性布局。我把搜索栏和另一个linearlayout(layout_5)包含6个文本视图和0-1-2-3-4-5。更好的选择是创建一个动态图像(从seekBar获取宽度),根据段数计算这些数字。

我强制搜索栏指示符停在特定点(在你的情况下为6点)。可以将seekBar的最大进度值设置为5.它可以工作,但不会提供良好的用户体验。

public class SegmentedSeekBar extends LinearLayout {

    private int[] preDefinedValues;
    private int currentProgressIndex;
    private SeekBar seekBar;
    private int segmentCount = 5:

    public SegmentedSeekBar(Context context) {
        this(context, null, 0);
    }

    public SegmentedSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.seekBarStyle);
    }

    public SegmentedSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.SegmentedSeekBar,
                0, 0);
        try {
           segmentCount =
           a.getInt(R.styleable.SegmentedSeekBar_segmentCount, -1);
        } finally {
           a.recycle();
        }
           init();
    }

    public void init() {
        //this values will be used when you need to set progress
        preDefinedValues = new int[segmentCount];
        for(int i = 0; i < preDefinedValues.length; i++) {
            preDefinedValues[i] = (100/(segmentCount-1)) * i;
        }
        //Get layout_5
        //which is linearlayout with 6 textviews  
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View sliderView = inflater.inflate(
                getSliderId(segmentCount), null);
        //seekbar already inside the linearlayout
        seekBar = (SeekBar)sliderView.findViewById(R.id.seek_bar);
        //linear layout is vertically align 
        //so add your 6 textview linearlayout
        addView(sliderView);
        seekBar.setOnTouchListener(seekBarTouchListener);
    }

    private int getSliderId(int size) {
        return R.layout.layout_5;
    }

    //this method sets progress which is seen in UI not actual progress
    //It uses the conversion that we did in beginning
    public synchronized void setProgress(int progress) {
        if(preDefinedValues != null && progress < preDefinedValues.length && progress >= 0) {
            seekBar.setProgress(preDefinedValues[progress]);
            currentProgressIndex = progress;
        }
    }

    //this listener make sure the right progress is seen in ui
    //take action when user finish with changing progress
    SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            int index = 0;
            for(int i = 0; i < preDefinedValues.length; i++) {
                //try to find closest preDefinedvalues by comparing with latest value
                if(Math.abs(seekBar.getProgress() - preDefinedValues[i]) < Math.abs(seekBar.getProgress() - preDefinedValues[index])) {
                    index = i;
                }
            }
            setProgress(index);
        }
    };
}