-1

我想拥有 SeekBar 的功能,但是将线(它是垂直的)自定义为一个矩形,其中填充了我设置栏的最大值的每个段的颜色。这是我扩展 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;
    }

}

我想要一条粗垂直线(矩形),我可以为我设置的最大 120 的每个部分填充颜色。这是一张图片:

在此处输入图像描述

我目前只有一个带有拇指作为滑块的基本垂直 seekBar,它对应于列表视图中的位置。我需要 seekBar 与 listView 配对,并且取决于滑块的位置,我显示 listview 的那部分。

4

1 回答 1

0

我想出了我需要做什么。我的目标是使 seekBar 具有特定的宽度(或高度,因为它是横向的水平 seekBar)。我需要 progressDrawable 区域小于分配给 seekBar 本身的大小。我还需要找到一种方法,使可绘制对象填充适当的空间并在确定大小后对其应用渐变。这是我为完成所需而创建的代码片段:

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;
    }

这是结果的屏幕截图。

在此处输入图像描述

于 2014-03-29T12:32:19.700 回答