您的位置:首页 > 移动开发 > 微信开发

1.微信密码盒子之密文

2015-10-11 20:17 483 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">对于上一次的效果,其实我本人很不满意,今天回头看看觉得很不舒服,趁今天有时间就来重新做一做效果,废话不多说了,先上效果图吧</span>
下面是源码,具体实现细节我会用一个doc文档记录下来的,这个文档用于引导新手的,大神勿喷。
<pre name="code" class="java">public class PassBox extends View {

private Paint mPaint;//画笔
private int pwdLength = 6;//密码长度默认为6个
private ArrayList<Rect> mRects;//记录每一个矩形的对象信息
private int screenW, screenH;//屏幕宽高
private int boxW;//每个矩形的宽
private boolean onceLoad = false;//之所以使用这个标识,是为了使获取屏幕宽高的动作只执行一次

public PassBox(Context context) {
super(context);
init();
}

public PassBox(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public PassBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

void init() {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);//画笔设置空心填充方式
mPaint.setAntiAlias(true);
mRects = new ArrayList<>(pwdLength);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed && !onceLoad) {
screenW = getWidth();
screenH = getHeight();
onceLoad = true;
boxW = screenW / pwdLength;//获取每个矩形的宽度
for (int i = 0; i < pwdLength; i++) {
mRects.add(new Rect(i * boxW, 0, (1 + i) * boxW, screenH));//记录每个矩形的信息
}
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Rect box : mRects) {
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(box, mPaint);
mPaint.setStyle(Paint.Style.FILL);//画笔为填充模式
canvas.drawCircle(box.centerX(), box.centerY(), dip2px(getContext(), 8), mPaint);
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}

源码下载地址



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: