你好,游客 登录 注册 搜索
背景:
阅读新闻

Android之九宫格解锁的实现

[日期:2013-05-14] 来源:Linux社区  作者:weidi1989 [字体:大 中 小]

下面是最重要的那个LocusPassWordView:

  1. /**
  2. *
  3. * 九宫格解锁
  4. *
  5. * @author way
  6. *
  7. */
  8. publicclass LocusPassWordView extends View {
  9. privatefloat w = 0;
  10. privatefloat h = 0;
  11. //
  12. privateboolean isCache = false;
  13. //
  14. private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  15. //
  16. private Point[][] mPoints = new Point[3][3];
  17. // 圆的半径
  18. privatefloat r = 0;
  19. // 选中的点
  20. private List<Point> sPoints = new ArrayList<Point>();
  21. privateboolean checking = false;
  22. private Bitmap locus_round_original;// 圆点初始状态时的图片
  23. private Bitmap locus_round_click;// 圆点点击时的图片
  24. private Bitmap locus_round_click_error;// 出错时圆点的图片
  25. private Bitmap locus_line;// 正常状态下线的图片
  26. private Bitmap locus_line_semicircle;
  27. private Bitmap locus_line_semicircle_error;
  28. private Bitmap locus_arrow;// 线的移动方向
  29. private Bitmap locus_line_error;// 错误状态下的线的图片
  30. privatelong CLEAR_TIME = 0;// 清除痕迹的时间
  31. privateint passwordMinLength = 5;// 密码最小长度
  32. privateboolean isTouch = true; // 是否可操作
  33. private Matrix mMatrix = new Matrix();
  34. privateint lineAlpha = 50;// 连线的透明度
  35. public LocusPassWordView(Context context, AttributeSet attrs, int defStyle) {
  36. super(context, attrs, defStyle);
  37. }
  38. public LocusPassWordView(Context context, AttributeSet attrs) {
  39. super(context, attrs);
  40. }
  41. public LocusPassWordView(Context context) {
  42. super(context);
  43. }
  44. @Override
  45. publicvoid onDraw(Canvas canvas) {
  46. if (!isCache) {
  47. initCache();
  48. }
  49. drawToCanvas(canvas);
  50. }
  51. privatevoid drawToCanvas(Canvas canvas) {
  52. // mPaint.setColor(Color.RED);
  53. // Point p1 = mPoints[1][1];
  54. // Rect r1 = new Rect(p1.x - r,p1.y - r,p1.x +
  55. // locus_round_click.getWidth() - r,p1.y+locus_round_click.getHeight()-
  56. // r);
  57. // canvas.drawRect(r1, mPaint);
  58. // 画所有点
  59. for (int i = 0; i < mPoints.length; i++) {
  60. for (int j = 0; j < mPoints[i].length; j++) {
  61. Point p = mPoints[i][j];
  62. if (p.state == Point.STATE_CHECK) {
  63. canvas.drawBitmap(locus_round_click, p.x - r, p.y - r,
  64. mPaint);
  65. } elseif (p.state == Point.STATE_CHECK_ERROR) {
  66. canvas.drawBitmap(locus_round_click_error, p.x - r,
  67. p.y - r, mPaint);
  68. } else {
  69. canvas.drawBitmap(locus_round_original, p.x - r, p.y - r,
  70. mPaint);
  71. }
  72. }
  73. }
  74. // mPaint.setColor(Color.BLUE);
  75. // canvas.drawLine(r1.left+r1.width()/2, r1.top, r1.left+r1.width()/2,
  76. // r1.bottom, mPaint);
  77. // canvas.drawLine(r1.left, r1.top+r1.height()/2, r1.right,
  78. // r1.bottom-r1.height()/2, mPaint);
  79. // 画连线
  80. if (sPoints.size() > 0) {
  81. int tmpAlpha = mPaint.getAlpha();
  82. mPaint.setAlpha(lineAlpha);
  83. Point tp = sPoints.get(0);
  84. for (int i = 1; i < sPoints.size(); i++) {
  85. Point p = sPoints.get(i);
  86. drawLine(canvas, tp, p);
  87. tp = p;
  88. }
  89. if (this.movingNoPoint) {
  90. drawLine(canvas, tp, new Point((int) moveingX, (int) moveingY));
  91. }
  92. mPaint.setAlpha(tmpAlpha);
  93. lineAlpha = mPaint.getAlpha();
  94. }
  95. }
  96. /**
  97. * 初始化Cache信息
  98. *
  99. * @param canvas
  100. */
  101. privatevoid initCache() {
  102. w = this.getWidth();
  103. h = this.getHeight();
  104. float x = 0;
  105. float y = 0;
  106. // 以最小的为准
  107. // 纵屏
  108. if (w > h) {
  109. x = (w - h) / 2;
  110. w = h;
  111. }
  112. // 横屏
  113. else {
  114. y = (h - w) / 2;
  115. h = w;
  116. }
  117. locus_round_original = BitmapFactory.decodeResource(
  118. this.getResources(), R.drawable.locus_round_original);
  119. locus_round_click = BitmapFactory.decodeResource(this.getResources(),
  120. R.drawable.locus_round_click);
  121. locus_round_click_error = BitmapFactory.decodeResource(
  122. this.getResources(), R.drawable.locus_round_click_error);
  123. locus_line = BitmapFactory.decodeResource(this.getResources(),
  124. R.drawable.locus_line);
  125. locus_line_semicircle = BitmapFactory.decodeResource(
  126. this.getResources(), R.drawable.locus_line_semicircle);
  127. locus_line_error = BitmapFactory.decodeResource(this.getResources(),
  128. R.drawable.locus_line_error);
  129. locus_line_semicircle_error = BitmapFactory.decodeResource(
  130. this.getResources(), R.drawable.locus_line_semicircle_error);
  131. locus_arrow = BitmapFactory.decodeResource(this.getResources(),
  132. R.drawable.locus_arrow);
  133. // Log.d("Canvas w h :", "w:" + w + " h:" + h);
  134. // 计算圆圈图片的大小
  135. float canvasMinW = w;
  136. if (w > h) {
  137. canvasMinW = h;
  138. }
  139. float roundMinW = canvasMinW / 8.0f * 2;
  140. float roundW = roundMinW / 2.f;
  141. //
  142. float deviation = canvasMinW % (8 * 2) / 2;
  143. x += deviation;
  144. x += deviation;
  145. if (locus_round_original.getWidth() > roundMinW) {
  146. float sf = roundMinW * 1.0f / locus_round_original.getWidth(); // 取得缩放比例,将所有的图片进行缩放
  147. locus_round_original = BitmapUtil.zoom(locus_round_original, sf);
  148. locus_round_click = BitmapUtil.zoom(locus_round_click, sf);
  149. locus_round_click_error = BitmapUtil.zoom(locus_round_click_error,
  150. sf);
  151. locus_line = BitmapUtil.zoom(locus_line, sf);
  152. locus_line_semicircle = BitmapUtil.zoom(locus_line_semicircle, sf);
  153. locus_line_error = BitmapUtil.zoom(locus_line_error, sf);
  154. locus_line_semicircle_error = BitmapUtil.zoom(
  155. locus_line_semicircle_error, sf);
  156. locus_arrow = BitmapUtil.zoom(locus_arrow, sf);
  157. roundW = locus_round_original.getWidth() / 2;
  158. }
  159. mPoints[0][0] = new Point(x + 0 + roundW, y + 0 + roundW);
  160. mPoints[0][1] = new Point(x + w / 2, y + 0 + roundW);
  161. mPoints[0][2] = new Point(x + w - roundW, y + 0 + roundW);
  162. mPoints[1][0] = new Point(x + 0 + roundW, y + h / 2);
  163. mPoints[1][1] = new Point(x + w / 2, y + h / 2);
  164. mPoints[1][2] = new Point(x + w - roundW, y + h / 2);
  165. mPoints[2][0] = new Point(x + 0 + roundW, y + h - roundW);
  166. mPoints[2][1] = new Point(x + w / 2, y + h - roundW);
  167. mPoints[2][2] = new Point(x + w - roundW, y + h - roundW);
  168. int k = 0;
  169. for (Point[] ps : mPoints) {
  170. for (Point p : ps) {
  171. p.index = k;
  172. k++;
  173. }
  174. }
  175. r = locus_round_original.getHeight() / 2;// roundW;
  176. isCache = true;
  177. }
  178. /**
  179. * 画两点的连接
  180. *
  181. * @param canvas
  182. * @param a
  183. * @param b
  184. */
  185. privatevoid drawLine(Canvas canvas, Point a, Point b) {
  186. float ah = (float) MathUtil.distance(a.x, a.y, b.x, b.y);
  187. float degrees = getDegrees(a, b);
  188. // Log.d("=============x===========", "rotate:" + degrees);
  189. canvas.rotate(degrees, a.x, a.y);
  190. if (a.state == Point.STATE_CHECK_ERROR) {
  191. mMatrix.setScale((ah - locus_line_semicircle_error.getWidth())
  192. / locus_line_error.getWidth(), 1);
  193. mMatrix.postTranslate(a.x, a.y - locus_line_error.getHeight()
  194. / 2.0f);
  195. canvas.drawBitmap(locus_line_error, mMatrix, mPaint);
  196. canvas.drawBitmap(locus_line_semicircle_error, a.x
  197. + locus_line_error.getWidth(),
  198. a.y - locus_line_error.getHeight() / 2.0f, mPaint);
  199. } else {
  200. mMatrix.setScale((ah - locus_line_semicircle.getWidth())
  201. / locus_line.getWidth(), 1);
  202. mMatrix.postTranslate(a.x, a.y - locus_line.getHeight() / 2.0f);
  203. canvas.drawBitmap(locus_line, mMatrix, mPaint);
  204. canvas.drawBitmap(locus_line_semicircle, a.x + ah
  205. - locus_line_semicircle.getWidth(),
  206. a.y - locus_line.getHeight() / 2.0f, mPaint);
  207. }
  208. canvas.drawBitmap(locus_arrow, a.x, a.y - locus_arrow.getHeight()
  209. / 2.0f, mPaint);
  210. canvas.rotate(-degrees, a.x, a.y);
  211. }
  212. publicfloat getDegrees(Point a, Point b) {
  213. float ax = a.x;// a.index % 3;
  214. float ay = a.y;// a.index / 3;
  215. float bx = b.x;// b.index % 3;
  216. float by = b.y;// b.index / 3;
  217. float degrees = 0;
  218. if (bx == ax) // y轴相等 90度或270
  219. {
  220. if (by > ay) // 在y轴的下边 90
  221. {
  222. degrees = 90;
  223. } elseif (by < ay) // 在y轴的上边 270
  224. {
  225. degrees = 270;
  226. }
  227. } elseif (by == ay) // y轴相等 0度或180
  228. {
  229. if (bx > ax) // 在y轴的下边 90
  230. {
  231. degrees = 0;
  232. } elseif (bx < ax) // 在y轴的上边 270
  233. {
  234. degrees = 180;
  235. }
  236. } else {
  237. if (bx > ax) // 在y轴的右边 270~90
  238. {
  239. if (by > ay) // 在y轴的下边 0 - 90
  240. {
  241. degrees = 0;
  242. degrees = degrees
  243. + switchDegrees(Math.abs(by - ay),
  244. Math.abs(bx - ax));
  245. } elseif (by < ay) // 在y轴的上边 270~0
  246. {
  247. degrees = 360;
  248. degrees = degrees
  249. - switchDegrees(Math.abs(by - ay),
  250. Math.abs(bx - ax));
  251. }
  252. } elseif (bx < ax) // 在y轴的左边 90~270
  253. {
  254. if (by > ay) // 在y轴的下边 180 ~ 270
  255. {
  256. degrees = 90;
  257. degrees = degrees
  258. + switchDegrees(Math.abs(bx - ax),
  259. Math.abs(by - ay));
  260. } elseif (by < ay) // 在y轴的上边 90 ~ 180
  261. {
  262. degrees = 270;
  263. degrees = degrees
  264. - switchDegrees(Math.abs(bx - ax),
  265. Math.abs(by - ay));
  266. }
  267. }
  268. }
  269. return degrees;
  270. }
  271. /**
  272. * 1=30度 2=45度 4=60度
  273. *
  274. * @param tan
  275. * @return
  276. */
  277. privatefloat switchDegrees(float x, float y) {
  278. return (float) MathUtil.pointTotoDegrees(x, y);
  279. }
  280. /**
  281. * 取得数组下标
  282. *
  283. * @param index
  284. * @return
  285. */
  286. publicint[] getArrayIndex(int index) {
  287. int[] ai = newint[2];
  288. ai[0] = index / 3;
  289. ai[1] = index % 3;
  290. return ai;
  291. }
  292. /**
  293. *
  294. * 检查
  295. *
  296. * @param x
  297. * @param y
  298. * @return
  299. */
  300. private Point checkSelectPoint(float x, float y) {
  301. for (int i = 0; i < mPoints.length; i++) {
  302. for (int j = 0; j < mPoints[i].length; j++) {
  303. Point p = mPoints[i][j];
  304. if (RoundUtil.checkInRound(p.x, p.y, r, (int) x, (int) y)) {
  305. return p;
  306. }
  307. }
  308. }
  309. returnnull;
  310. }
  311. /**
  312. * 重置
  313. */
  314. privatevoid reset() {
  315. for (Point p : sPoints) {
  316. p.state = Point.STATE_NORMAL;
  317. }
  318. sPoints.clear();
  319. this.enableTouch();
  320. }
  321. /**
  322. * 判断点是否有交叉 返回 0,新点 ,1 与上一点重叠 2,与非最后一点重叠
  323. *
  324. * @param p
  325. * @return
  326. */
  327. privateint crossPoint(Point p) {
  328. // 重叠的不最后一个则 reset
  329. if (sPoints.contains(p)) {
  330. if (sPoints.size() > 2) {
  331. // 与非最后一点重叠
  332. if (sPoints.get(sPoints.size() - 1).index != p.index) {
  333. return2;
  334. }
  335. }
  336. return1; // 与最后一点重叠
  337. } else {
  338. return0; // 新点
  339. }
  340. }
  341. /**
  342. * 添加一个点
  343. *
  344. * @param point
  345. */
  346. privatevoid addPoint(Point point) {
  347. this.sPoints.add(point);
  348. }
  349. /**
  350. * 转换为String
  351. *
  352. * @param points
  353. * @return
  354. */
  355. private String toPointString() {
  356. if (sPoints.size() > passwordMinLength) {
  357. StringBuffer sf = new StringBuffer();
  358. for (Point p : sPoints) {
  359. sf.append(",");
  360. sf.append(p.index);
  361. }
  362. return sf.deleteCharAt(0).toString();
  363. } else {
  364. return"";
  365. }
  366. }
  367. boolean movingNoPoint = false;
  368. float moveingX, moveingY;
  369. @Override
  370. publicboolean onTouchEvent(MotionEvent event) {
  371. // 不可操作
  372. if (!isTouch) {
  373. returnfalse;
  374. }
  375. movingNoPoint = false;
  376. float ex = event.getX();
  377. float ey = event.getY();
  378. boolean isFinish = false;
  379. boolean redraw = false;
  380. Point p = null;
  381. switch (event.getAction()) {
  382. case MotionEvent.ACTION_DOWN: // 点下
  383. // 如果正在清除密码,则取消
  384. if (task != null) {
  385. task.cancel();
  386. task = null;
  387. Log.d("task", "touch cancel()");
  388. }
  389. // 删除之前的点
  390. reset();
  391. p = checkSelectPoint(ex, ey);
  392. if (p != null) {
  393. checking = true;
  394. }
  395. break;
  396. case MotionEvent.ACTION_MOVE: // 移动
  397. if (checking) {
  398. p = checkSelectPoint(ex, ey);
  399. if (p == null) {
  400. movingNoPoint = true;
  401. moveingX = ex;
  402. moveingY = ey;
  403. }
  404. }
  405. break;
  406. case MotionEvent.ACTION_UP: // 提起
  407. p = checkSelectPoint(ex, ey);
  408. checking = false;
  409. isFinish = true;
  410. break;
  411. }
  412. if (!isFinish && checking && p != null) {
  413. int rk = crossPoint(p);
  414. if (rk == 2) // 与非最后一重叠
  415. {
  416. // reset();
  417. // checking = false;
  418. movingNoPoint = true;
  419. moveingX = ex;
  420. moveingY = ey;
  421. redraw = true;
  422. } elseif (rk == 0) // 一个新点
  423. {
  424. p.state = Point.STATE_CHECK;
  425. addPoint(p);
  426. redraw = true;
  427. }
  428. // rk == 1 不处理
  429. }
  430. // 是否重画
  431. if (redraw) {
  432. }
  433. if (isFinish) {
  434. if (this.sPoints.size() == 1) {
  435. this.reset();
  436. } elseif (this.sPoints.size() < passwordMinLength
  437. && this.sPoints.size() > 0) {
  438. // mCompleteListener.onPasswordTooMin(sPoints.size());
  439. error();
  440. clearPassword();
  441. Toast.makeText(this.getContext(), "密码太短,请重新输入!",
  442. Toast.LENGTH_SHORT).show();
  443. } elseif (mCompleteListener != null) {
  444. if (this.sPoints.size() >= passwordMinLength) {
  445. this.disableTouch();
  446. mCompleteListener.onComplete(toPointString());
  447. }
  448. }
  449. }
  450. this.postInvalidate();
  451. returntrue;
  452. }
  453. /**
  454. * 设置已经选中的为错误
  455. */
  456. privatevoid error() {
  457. for (Point p : sPoints) {
  458. p.state = Point.STATE_CHECK_ERROR;
  459. }
  460. }
  461. /**
  462. * 设置为输入错误
  463. */
  464. publicvoid markError() {
  465. markError(CLEAR_TIME);
  466. }
  467. /**
  468. * 设置为输入错误
  469. */
  470. publicvoid markError(finallong time) {
  471. for (Point p : sPoints) {
  472. p.state = Point.STATE_CHECK_ERROR;
  473. }
  474. this.clearPassword(time);
  475. }
  476. /**
  477. * 设置为可操作
  478. */
  479. publicvoid enableTouch() {
  480. isTouch = true;
  481. }
  482. /**
  483. * 设置为不可操作
  484. */
  485. publicvoid disableTouch() {
  486. isTouch = false;
  487. }
  488. private Timer timer = new Timer();
  489. private TimerTask task = null;
  490. /**
  491. * 清除密码
  492. */
  493. publicvoid clearPassword() {
  494. clearPassword(CLEAR_TIME);
  495. }
  496. /**
  497. * 清除密码
  498. */
  499. publicvoid clearPassword(finallong time) {
  500. if (time > 1) {
  501. if (task != null) {
  502. task.cancel();
  503. Log.d("task", "clearPassword cancel()");
  504. }
  505. lineAlpha = 130;
  506. postInvalidate();
  507. task = new TimerTask() {
  508. publicvoid run() {
  509. reset();
  510. postInvalidate();
  511. }
  512. };
  513. Log.d("task", "clearPassword schedule(" + time + ")");
  514. timer.schedule(task, time);
  515. } else {
  516. reset();
  517. postInvalidate();
  518. }
  519. }
  520. //
  521. private OnCompleteListener mCompleteListener;
  522. /**
  523. * @param mCompleteListener
  524. */
  525. publicvoid setOnCompleteListener(OnCompleteListener mCompleteListener) {
  526. this.mCompleteListener = mCompleteListener;
  527. }
  528. /**
  529. * 取得密码
  530. *
  531. * @return
  532. */
  533. private String getPassword() {
  534. SharedPreferences settings = this.getContext().getSharedPreferences(
  535. this.getClass().getName(), 0);
  536. return settings.getString("password", ""); // , "0,1,2,3,4,5,6,7,8"
  537. }
  538. /**
  539. * 密码是否为空
  540. *
  541. * @return
  542. */
  543. publicboolean isPasswordEmpty() {
  544. return StringUtil.isEmpty(getPassword());
  545. }
  546. publicboolean verifyPassword(String password) {
  547. boolean verify = false;
  548. if (com.way.util.StringUtil.isNotEmpty(password)) {
  549. // 或者是超级密码
  550. if (password.equals(getPassword())) {
  551. verify = true;
  552. }
  553. }
  554. return verify;
  555. }
  556. /**
  557. * 设置密码
  558. *
  559. * @param password
  560. */
  561. publicvoid resetPassWord(String password) {
  562. SharedPreferences settings = this.getContext().getSharedPreferences(
  563. this.getClass().getName(), 0);
  564. Editor editor = settings.edit();
  565. editor.putString("password", password);
  566. editor.commit();
  567. }
  568. publicint getPasswordMinLength() {
  569. return passwordMinLength;
  570. }
  571. publicvoid setPasswordMinLength(int passwordMinLength) {
  572. this.passwordMinLength = passwordMinLength;
  573. }
  574. /**
  575. * 轨迹球画完成事件
  576. *
  577. * @author way
  578. */
  579. publicinterface OnCompleteListener {
  580. /**
  581. * 画完了
  582. *
  583. * @param str
  584. */
  585. publicvoid onComplete(String password);
  586. }
  587. }

这里只贴出了最重要的那部分代码,有兴趣的朋友可以下载源码看看

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android九宫格解锁  九宫格解锁 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/5/23 15:11:09
为啥下载不下来啊