1

我的客户希望有一个看起来像这样的搜索栏: 在此处输入链接描述 我已经将进度、次要进度和拇指设置为希望的样子,但我不知道如何在进度上应用点。我希望这对可绘制对象是可行的,并且我不必应用 5 张带有填充或类似内容的图像。

提前致谢。

4

1 回答 1

5

终于解决了这个问题。也许其他人可能需要这个。我继承了原始类并更改了 onDraw:

public class SeverityBar extends SeekBar {
private Paint selected, unselected;
private int halfSize = 15;
private RectF position;

public SeverityBar(Context context) {
    super(context);
    selected = new Paint(Paint.ANTI_ALIAS_FLAG);
    selected.setColor(getContext().getResources().getColor(R.color.TextColor));
    selected.setStyle(Style.FILL);
    unselected = new Paint(Paint.ANTI_ALIAS_FLAG);
    unselected.setColor(getContext().getResources().getColor(android.R.color.darker_gray));
    selected.setStyle(Style.FILL);
    position = new RectF();
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    float margin = (canvas.getWidth()-(paddingLeft+getPaddingRight()))/4;
    float halfHeight = (canvas.getHeight()+paddingTop)*.5f;
    for (int i = 0; i < 5; i++) {
        position.set(
                paddingLeft+(i*margin)-halfSize, 
                halfHeight-halfSize, 
                paddingLeft+(i*margin)+halfSize,
                halfHeight+halfSize);
        canvas.drawOval(position, (i<getProgress())?selected:unselected);
    }
    super.onDraw(canvas);
}

public int getDotsSize() {
    return halfSize*2;
}

public void setDotsSize(int dotsSize) {
    this.halfSize = dotsSize/2;
}

}

查询和边距的值应根据进度的最小/最大值进行更改。

于 2013-06-07T10:08:13.273 回答