Android:从中心绘制搜索栏进度

时间:2015-01-27 00:44:35

标签: android android-seekbar

嘿伙计们,我目前正在使用自己的Android SeekBar,我希望将进度颜色从条形图的中心绘制到拇指。目前,我的SeekBar类看起来像这样:

public class VerticalSeekBar extends SeekBar {

public VerticalSeekBar(Context context) {
    super(context);

}

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

}

public VerticalSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    setThumb(getResources().getDrawable(R.drawable.slidera));
    setProgressDrawable(getResources().getDrawable(R.drawable.sl_bg));
}

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

    c.rotate(-90);
    c.translate(-getHeight(), 0);

    super.onDraw(c);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
   [...]

我现在的问题是,我不知道为了改变ProgressDrawable绘图的行为我需要覆盖哪种方法,我相信一定有某种方法。

提前致谢

current state vs. wanted version

1 个答案:

答案 0 :(得分:0)

所以这就是全班。 getRekt方法获得 SeekBar 的大小,你在onDraw方法中看到的其他东西几乎是自我解释的。背景drawable是一个drawable,由颜色值0x00000000组成,因此它不会覆盖" new"进展背景。

public class VerticalSeekBar extends SeekBar {

public VerticalSeekBar(Context context) {
    super(context);

}

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

}

public VerticalSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    setThumb(getResources().getDrawable(R.drawable.slidera));
    setThumbOffset(0);
    setProgressDrawable(getResources().getDrawable(R.drawable.sl_bg));

    setPadding(0,0,0,0);
} 

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

    c.rotate(-90);
    c.translate(-getHeight(), 0);
    Paint paint = new Paint();
    paint.setColor(0xffff7700);
    paint.setStrokeWidth(11);


    BlurMaskFilter blurMaskFilter=new BlurMaskFilter(15.0f,BlurMaskFilter.Blur.OUTER);
    c.drawLine(
            getRectX()+(getProgress()-50)*getRHeight()/100, getRectY(),getRectX(), getRectY(), paint);
    paint.setMaskFilter(blurMaskFilter);

    c.drawLine(
            getRectX()+(getProgress()-50)*getRHeight()/100, getRectY(),getRectX(), getRectY(), paint);


drawMeter(c);

     super.onDraw(c);

}

public int getRectX(){
Rect MLG=   getProgressDrawable().getBounds();
return MLG.centerX();
}
public int getRectY(){
Rect MLG=   getProgressDrawable().getBounds();
 return MLG.centerY();
}

public int getRHeight(){
Rect MLG=   getProgressDrawable().getBounds();
return MLG.width();
}

public void drawMeter(Canvas c){
Paint paint = new Paint();
paint.setColor(0xffff7700);

 paint.setStrokeWidth(2);      
c.drawLine(
        0,100,0, 0, paint);
c.drawLine(
        getRHeight()/2,100,getRHeight()/2, 0, paint);
c.drawLine(
        getRHeight(),100,getRHeight(), 0, paint);


}

@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:
            setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
            onSizeChanged(getWidth(), getHeight(), 0, 0);

            break;

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

这应该是这样的:

faders