1.微信密码盒子之密文

<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);
    }
}

源码下载地址

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值