首页 > 学院 > 开发设计 > 正文

自定义SeekBar(ProgressBar实现SeekBar效果)去除两边间距

2019-11-08 00:34:31
字体:
来源:转载
供稿:网友

众所周知,SeekBar作为安卓的常用控件之一,用来进行显示进度,但是SeekBar本身两边会留出空隙,不管怎么设置,进度条本身都不会与父布局贴近,但是需求又偏偏要贴近贴近而且刻度图标又不能少呢?不妨用PRogressBar来做吧,记下来备用~~

public class MatchSeekBar extends ProgressBar{    protected Paint mPaint;    protected int mRealWidth;    private int w;    public MatchSeekBar(Context context, AttributeSet attrs) {        super(context, attrs);        mPaint=new Paint();    }    @Override    protected synchronized void onDraw(Canvas canvas) {        super.onDraw(canvas);        mPaint.setAntiAlias(true);                       //设置画笔为无锯齿        int oldRadius=getHeight()/2;        int radius=(int)(oldRadius-1.5);        w=mRealWidth-oldRadius*2;        float radio = getProgress() * 1.0f / getMax();        float progressPosX = (int) (w * radio);        canvas.translate(getPaddingLeft()+oldRadius, getHeight() / 2);        /**         * 先画一个白色的圆         */        Paint mPaint2=new Paint();        mPaint2.setColor(Color.WHITE);        canvas.drawCircle(progressPosX,0,radius,mPaint2);        /**         * 再画一个圆环         * 这里要注意的是圆与圆环的半径都被减去1.5,这是为啥呢,         * 因为这里半径用到控件高度的一半,而圆环的的宽度是3,         * 那么圆环就会有1.5被画到控件外面去         *         */        mPaint.setColor(Color.parseColor("#18BBB6"));                    //设置画笔颜色        mPaint.setStrokeWidth((float) 3.0);              //线宽        mPaint.setStyle(Paint.Style.STROKE);        canvas.drawCircle(progressPosX,0,(int)radius,mPaint);    }    protected void onSizeChanged(int w, int h, int oldw, int oldh)    {        super.onSizeChanged(w, h, oldw, oldh);        mRealWidth = w - getPaddingRight() - getPaddingLeft();        Log.d("dake.xuan", "onSizeChanged: "+mRealWidth);    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表