-1

I want to have the functionality of a SeekBar, but customize the line (it's vertical) to be a rectangle with the colors filled per segment of the max I set the bar to. Here is my class which I extend the SeekBar:

public class VerticalSeekBar extends SeekBar {

    private static final int NUMBER_OF_MINUTES = 120;
    private Paint mPaint = new Paint();

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMax(NUMBER_OF_MINUTES - 1);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas canvas) {
        canvas.rotate(90);
        canvas.translate(0, -getWidth());

        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                int i=0;
                i=getMax() - (int) (getMax() * event.getY() / getHeight());
                setProgress(getMax()-i);
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }

}

I want to have a thick vertical line (rectangle) which I can fill the color for each segment of the 120 max I set. Here is an image:

enter image description here

I currently just have a basic vertical seekBar with a thumb as the slider and it corresponds to the position in a listview. I need the seekBar for it pairs with a listView and dependent on where the slider is, I show that portion of the listview.

taraloca
  • 9,077
  • 9
  • 44
  • 77
  • Why the down vote? If I don't know what the problem is with my question...I don't know how to correct it. – taraloca Mar 26 '14 at 15:07

1 Answers1

0

I figured out what I needed to do. My goal was to make the seekBar a particular width (or height for it is a horizontal seekBar turned sideways). I needed the progressDrawable area to be smaller than the size allotted for the seekBar itself. I also needed to find a way that the drawable filled the appropriate space and applied the gradient to it after the size was determined. Here is a snippet of the code I created to accomplish what I needed:

mVerticalSeekBar = (VerticalSeekBar) getView().findViewById(R.id.seekBar1);

LayerDrawable layers = setDrawablesWithGradientAndPadding();

mVerticalSeekBar.setProgressDrawable(layers);

protected LayerDrawable setDrawablesWithGradientAndPadding() {
        Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
        ClipDrawable clipDrawable = new ClipDrawable(transparentDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        GradientDrawable gradientDrawable = new GradientDrawable(Orientation.LEFT_RIGHT, getColors(jsonResults));
        /**
         * The padding can be confusing, applying to top and bottom, because it is actually applying to a horizontal
         * seekBar which I then flip in the onDraw() method in VerticalSeekBar.java
         */
        InsetDrawable insetDrawable = new InsetDrawable(gradientDrawable, 0, 40, 0, 40);

        LayerDrawable layers = new LayerDrawable(new Drawable[] { insetDrawable, clipDrawable });
        return layers;
    }

Here is a screenshot of the result.

enter image description here

taraloca
  • 9,077
  • 9
  • 44
  • 77