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();
}
}
网友评论