简单手机九宫格解锁

private void init(Context context) {
    this.context = context;
    //设置画笔
    mPaint = new Paint();
    mPaint2 = new Paint();
    mPaint3 = new Paint();
    //设置画圆的画笔
    path = new Path();
    //设置路线的画笔
    path2 = new Path();
    mPaint.setColor(Color.parseColor("#ff5f50"));
    mPaint.setAntiAlias(true);
    mPaint.setStrokeWidth(5);
    mPaint.setTextSize(30);
    mPaint.setTextAlign(Paint.Align.CENTER);
    mPaint2.setColor(Color.parseColor("#ff5f50"));
    mPaint2.setAntiAlias(true);
    mPaint2.setStrokeWidth(20);
    mPaint2.setTextSize(30);
    mPaint2.setStyle(Paint.Style.STROKE);
    mPaint2.setTextAlign(Paint.Align.CENTER);
    //设置圆的的数量
    for (int i = 1; i <= 9; i++) {
        list.add(i);
    }
    //s设置解锁密码
    list2.add(1);
    list2.add(4);
    list2.add(8);
    list2.add(9); }


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //圆半径
    circleRadius = 50;
    //间隔直径
    interval = (getRight() - circleRadius * 2) / 2;
    //圆的X轴
    circleX = 50;
    //圆的Y轴
    circleY = 50;

    //划线
    canvas.drawPath(path, mPaint2);
    //路径圆
    canvas.drawPath(path2, mPaint2);
    //绘制九个原点
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            canvas.drawCircle(circleX + interval * j, circleY + interval * i, circleRadius, mPaint);
        }
    }


}

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    float x = event.getX();
    float y = event.getY();
    int nam = 1;
    mPaint2.setColor(Color.parseColor("#ff5f50"));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            //判断是否在圆点的区间上
            if ((x > circleX - 50 + interval * j && x < circleX + 50 + interval * j) && (y > circleY - 50 + interval * i && y < circleY + 50 + interval * i)) {
               //判断是否是第一个线点
                if (lin == 2) {
                    //判断是否经过该圆点
                    if (list1.indexOf(nam) == -1) {
                        path2.addCircle(circleX + interval * j, circleY + interval * i, circleRadius, Path.Direction.CW);
                        path.lineTo(circleX + interval * j, circleY + interval * i);
                        path.moveTo(circleX + interval * j, circleY + interval * i);
                        //添加密码数
                        list1.add(nam);
                    }
                } else {
                    if (list1.indexOf(nam) == -1) {
                        path2.addCircle(circleX + interval * j, circleY + interval * i, circleRadius, Path.Direction.CW);
                        path.moveTo(circleX + interval * j, circleY + interval * i);
                        //添加密码数
                        list1.add(nam);
                        lin = 2;
                    }
                }

            }
            invalidate();
            nam++;
        }

    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        //判断俩个密码集合长度是否相等
        if (list1.size() == list2.size()) {
            boolean ischeck = true;
            for (int i = 0; i < list1.size(); i++) {
                Integer l1 = list1.get(i);
                Integer l2 = list2.get(i);
                //判断数字和顺序是否相等
                if (l1 != l2) {
                    ischeck = false;
                }
            }
            if (ischeck) {
                mPaint2.setColor(Color.parseColor("#00ff00"));
                Toast.makeText(context, "解锁成功", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        reset();
                        postInvalidate();
                    }
                }, 1000);
            } else {
                mPaint2.setColor(Color.parseColor("#ff0000"));
                Toast.makeText(context, "解锁失败", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        reset();
                        postInvalidate();
                    }
                }, 1000);
            }
        } else {
            mPaint2.setColor(Color.parseColor("#ff0000"));
            Toast.makeText(context, "解锁失败", Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    reset();
                    postInvalidate();
                }
            }, 1000);
        }

    }
    return true;
}

public void reset() {
    lin = 1;
    list1.clear();
    path.reset();
    path2.reset();
}

你可能感兴趣的:(简单手机九宫格解锁)