Skip to content

一个添加银行卡操作步骤自定义VIew,很简单

License

Notifications You must be signed in to change notification settings

wenzhimin/BootStepView

Repository files navigation

一个添加银行卡操作步骤自定义View,很简单




继承View 实现自定义属性(不知道自定义属性的百度)

public BootStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 加载自定义属性集合BootStepView
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BootStepView);
// 解析集合中的属性属性
// 将解析的属性传入到画圆的画笔颜色变量当中(本质上是自定义画圆画笔的颜色)
// 第二个参数是默认设置颜色(即无指定情况下使用)
circularColor = typedArray.getColor(R.styleable.BootStepView_circular_color, Color.RED);
circularSize=typedArray.getDimensionPixelSize(R.styleable.BootStepView_circular_size,16);
circularTextSize=typedArray.getDimensionPixelSize(R.styleable.BootStepView_circular_text_size,60);
circularTextColor= typedArray.getColor(R.styleable.BootStepView_circular_text_color, Color.WHITE);
....
}

在onMeasure()对View的宽高进行测量

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获取宽-测量规则的模式和大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
// 获取高-测量规则的模式和大小
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width=480;
int height=240;
if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(width, height);
} else if (widthMode == MeasureSpec.AT_MOST ) {
setMeasuredDimension(width, heightSize);
}else if(heightMode== MeasureSpec.AT_MOST){
setMeasuredDimension(widthSize, height);
}
}
   ## 在onDraw()实现
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取传入的padding值
final int paddingLeft = getPaddingLeft();
final int paddingRight = getPaddingRight();
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
int width=getWidth();
//开始绘制第一个圆
int oneX=paddingLeft+circularSize*2;
canvas.drawCircle(oneX,paddingTop,circularSize,paint);
//开始绘制数字1
paint.setColor(circularTextColor);
paint.setTextSize(circularTextSize);
canvas.drawText("1",oneX-circularTextSize/3,paddingTop+circularTextSize/3,paint);
//开始绘制第一个线条
paint.setColor(isTwoColor?circularColor:defaultColor);
canvas.drawLine(oneX+circularSize,paddingTop,width/2-circularSize,paddingTop,paint);
...
}

About

一个添加银行卡操作步骤自定义VIew,很简单

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages