android自定义代码雨view,Android 自定义View 密码框实例代码

暴露您view中所有影响可见外观的属性或者行为。

•通过XML添加和设置样式

•通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器

效果图展示:

67c804922a0930762eb12696c546f41f.png

支持的样式

可以通过XML定义影响外边和行为的属性如下

边框圆角值,边框颜色,分割线颜色,边框宽度,密码长度,密码大小,密码颜色

同时支持原来EditText功能,可以获得数据值,数字键盘设置等

绘制逻辑的主要代码

protected void onDraw(Canvas canvas) {

int width = getWidth();

int height = getHeight();

// 外边框

RectF rect = new RectF(0, 0, width, height);

borderPaint.setColor(borderColor);

canvas.drawRoundRect(rect, borderRadius, borderRadius, borderPaint);

// 内容区

RectF rectIn = new RectF(rect.left + defaultContMargin, rect.top + defaultContMargin,

rect.right - defaultContMargin, rect.bottom - defaultContMargin);

borderPaint.setColor(Color.WHITE);

canvas.drawRoundRect(rectIn, borderRadius, borderRadius, borderPaint);

// 分割线

borderPaint.setColor(borderColor);

borderPaint.setStrokeWidth(defaultSplitLineWidth);

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

float x = width * i / passwordLength;

canvas.drawLine(x, 0, x, height, borderPaint);

}

// 密码

float cx, cy = height/ 2;

float half = width / passwordLength / 2;

for(int i = 0; i < textLength; i++) {

cx = width * i / passwordLength + half;

canvas.drawCircle(cx, cy, passwordWidth, passwordPaint);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值