美文网首页
自定义电池view

自定义电池view

作者: 小婷婷tt | 来源:发表于2019-05-27 16:27 被阅读0次
    效果图

    public class BatteryViewextends View {

    private ContextmContext;

    private PaintelectricPaint;

    private int wholenum;

    private int choosenum;

    public BatteryView(Context context) {

    super(context);

    }

    public BatteryView(Context context, AttributeSet attrs) {

    super(context, attrs);

    init(context);

    }

    private void init(Context context) {

    this.mContext = context;

    electricPaint =new Paint();

    electricPaint.setAntiAlias(true);

    electricPaint.setColor(getResources().getColor(R.color.white));

    electricPaint.setStyle(Paint.Style.FILL);

    }

    @Override

        protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    int height = getHeight();

    int width = getWidth();

    float strokeWidth = (width - ViewUtils.dip2px(mContext,2f) *2 - ViewUtils.dip2px(mContext,1f) *wholenum) /wholenum;

    electricPaint.setColor(getResources().getColor(R.color.white));

    electricPaint.setStrokeWidth(strokeWidth);

    for (int i =1; i <=choosenum; i++) {

    float x = (strokeWidth + ViewUtils.dip2px(mContext,1f)) * i - ViewUtils.dip2px(mContext,3f);

    canvas.drawLine(x, ViewUtils.dip2px(mContext,3f), x, height - ViewUtils.dip2px(mContext,3f),electricPaint);

    }

    }

    public void setWholeNum(int num) {

    this.wholenum = num;

    }

    public void setChooseNum(int num) {

    this.choosenum = num;

    postInvalidate();

    }

    }

    相关文章

      网友评论

          本文标题:自定义电池view

          本文链接:https://www.haomeiwen.com/subject/vrbzbqtx.html