2015-01-27 105 views
1

嘿傢伙我目前正在使用我自己的Android SeekBar,並且我想將進度顏色從欄的中心繪製到拇指。目前,我的搜索欄類看起來是這樣的:Android:從中心繪製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

回答

0

所以這就是現在整個類。 getRekt方法得到SeekBar的大小,並且您在onDraw方法中看到的其他內容幾乎可以自我解釋。後臺可繪製是一個由顏色值0x00000000組成的可繪製對象,因此它不包含「新」進度背景。

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