Search code examples
androidandroid-custom-viewseekbar

android seekbar custom drawable


I want to have a seek bar that I can show range of acceptable values like below. I know the method of setting a custom drawable to seek bar. My question is how I can build this kind of drawable with partial green part in xml.Thanks in advanced.

enter image description here


Solution

  • use this Drawable, call it with SeekBar.setProgressDrawable()

    public class PartialDrawable extends Drawable {
        private float mStart;
        private float mEnd;
        private Paint mPaint;
    
        public PartialDrawable(float start, float end) {
            mStart = start;
            mEnd = end;
            mPaint = new Paint();
        }
    
        @Override
        public void draw(Canvas canvas) {
            Rect b = getBounds();
            mPaint.setColor(0xff888888);
            canvas.drawRect(b,mPaint);
            mPaint.setColor(0xff00aa00);
            canvas.drawRect(b.width() * mStart, b.top, b.width() * mEnd, b.bottom, mPaint);
        }
    
        @Override
        public void setAlpha(int alpha) {
        }
    
        @Override
        public void setColorFilter(ColorFilter cf) {
        }
    
        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }
    }