1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
public class NineGridView extends View {
private Paint paint; public static final int raduis = 60; public static final int raduisS = 30; private float curr_x, curr_y; private boolean isOver = false; private List<Point> points = new ArrayList<>(); private List<Integer> mList = new ArrayList<>();
public NineGridView(Context context) { super(context); init(); }
public NineGridView(Context context, AttributeSet attrs) { super(context, attrs); init(); }
public void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setDither(true); paint.setStrokeWidth(10); paint.setColor(Color.rgb(0x5a, 0xa9, 0xfe)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (getWidth() != 0 && getHeight() != 0) { int width = getWidth() / 4; int height = getHeight() / 4; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Point point = new Point((j + 1) * width, (i + 1) * height); points.add(point); } } } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mList.size() > 0) { Point point = points.get(mList.get(mList.size() - 1)); canvas.drawLine(point.x, point.y, curr_x, curr_y, paint); } if (mList.size() > 1) { for (int i = 0; i < mList.size() - 1; i++) { Point start = points.get(mList.get(i)); Point end = points.get(mList.get(i + 1)); canvas.drawLine(start.x, start.y, end.x, end.y, paint); } } paint.setColor(Color.WHITE); for (Point point : points) { canvas.drawCircle(point.x, point.y, raduis, paint); } paint.setColor(Color.rgb(0x5a, 0xa9, 0xfe)); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(2); for (Point point : points) { canvas.drawCircle(point.x, point.y, raduis, paint); } paint.setStyle(Paint.Style.FILL_AND_STROKE); for (Integer i : mList) { Point point = points.get(i); canvas.drawCircle(point.x, point.y, raduisS, paint); } }
@Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY();
switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isOver = false; int index = isOnClircle(x, y); if (index != -1) { mList.add(index); } curr_x = x; curr_y = y; break; case MotionEvent.ACTION_MOVE: if (!isOver) { curr_x = x; curr_y = y; int i = isOnClircle(x, y); if (i != -1 && !mList.contains(i)) { mList.add(i); } if (mList.size() == 9) { returnData(); mList.clear(); isOver = true; } } break; case MotionEvent.ACTION_UP: if (mList.size() > 4) { returnData(); } mList.clear(); break; default: break; } invalidate(); return true; }
public void returnData() { if (onPasswordFinishListener != null) { String password = ""; for (int i = 0; i < mList.size(); i++) { password += mList.get(i) + 1; } onPasswordFinishListener.onPasswrodFinish(password); } }
public int isOnClircle(float x, float y) { for (int i = 0; i < points.size(); i++) { Point point = points.get(i); if ((x - point.x) * (x - point.x) + (y - point.y) * (y - point.y) < raduis * raduis) { return i; } } return -1; }
private OnPasswordFinishListener onPasswordFinishListener;
public void setOnPasswordFinishListener(OnPasswordFinishListener onPasswordFinishListener) { this.onPasswordFinishListener = onPasswordFinishListener; }
public interface OnPasswordFinishListener { void onPasswrodFinish(String password); } }
|