简单手机九宫格解锁
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() {@Overridepublic void run() {reset();postInvalidate();}}, 1000);} else {mPaint2.setColor(Color.parseColor("#ff0000"));Toast.makeText(context, "解鎖失敗", Toast.LENGTH_SHORT).show();new Handler().postDelayed(new Runnable() {@Overridepublic void run() {reset();postInvalidate();}}, 1000);}} else {mPaint2.setColor(Color.parseColor("#ff0000"));Toast.makeText(context, "解鎖失敗", Toast.LENGTH_SHORT).show();new Handler().postDelayed(new Runnable() {@Overridepublic void run() {reset();postInvalidate();}}, 1000);}}return true;
}public void reset() {lin = 1;list1.clear();path.reset();path2.reset();
}
總結
- 上一篇: ABB变电站非自耦变压器
- 下一篇: Leetcode——安卓系统手势解锁(九