本文源码(utf-8编码):http://download.csdn.net/detail/weidi1989/6628211

ps:请不要再问我,为什么导入之后会乱码了。

其实,代码基本上都是从原生系统中提取的:LockPatternView、加密工具类,以及解锁逻辑等,我只是稍作修改,大家都知道,原生系统界面比较丑陋,因此,我特意把QQ的apk解压了,从中拿了几张图案解锁的图片,一个简单的例子就这样诞生了。

好了,废话不多说,我们来看看效果(最后两张是最新4.4系统,炫一下,呵呵):

      

1.最关健的就是那个自定义九宫格View,代码来自framework下:LockPatternView,原生系统用的图片资源比较多,好像有7、8张吧,而且绘制的比较复杂,我找寻半天,眼睛都找瞎了,发现解压的QQ里面就3张图片,一个圈圈,两个点,没办法,只能修改代码了,在修改的过程中,才发现,其实可以把原生的LockPatternView给简化,绘制更少的图片,达到更好的效果。总共优化有:①去掉了连线的箭头,②原生的连线只有白色一种,改成根据不同状态显示黄色和红色两张色,③.原生view是先画点再画线,使得线覆盖在点的上面,影响美观,改成先画连线再画点。

关健部分代码onDraw函数:

[java] view plain copy  
  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. final ArrayList<Cell> pattern = mPattern;
  4. final int count = pattern.size();
  5. final boolean[][] drawLookup = mPatternDrawLookup;
  6. if (mPatternDisplayMode == DisplayMode.Animate) {
  7. // figure out which circles to draw
  8. // + 1 so we pause on complete pattern
  9. final int oneCycle = (count + 1) * MILLIS_PER_CIRCLE_ANIMATING;
  10. final int spotInCycle = (int) (SystemClock.elapsedRealtime() - mAnimatingPeriodStart)
  11. % oneCycle;
  12. final int numCircles = spotInCycle / MILLIS_PER_CIRCLE_ANIMATING;
  13. clearPatternDrawLookup();
  14. for (int i = 0; i < numCircles; i++) {
  15. final Cell cell = pattern.get(i);
  16. drawLookup[cell.getRow()][cell.getColumn()] = true;
  17. }
  18. // figure out in progress portion of ghosting line
  19. final boolean needToUpdateInProgressPoint = numCircles > 0
  20. && numCircles < count;
  21. if (needToUpdateInProgressPoint) {
  22. final float percentageOfNextCircle = ((float) (spotInCycle % MILLIS_PER_CIRCLE_ANIMATING))
  23. / MILLIS_PER_CIRCLE_ANIMATING;
  24. final Cell currentCell = pattern.get(numCircles - 1);
  25. final float centerX = getCenterXForColumn(currentCell.column);
  26. final float centerY = getCenterYForRow(currentCell.row);
  27. final Cell nextCell = pattern.get(numCircles);
  28. final float dx = percentageOfNextCircle
  29. * (getCenterXForColumn(nextCell.column) - centerX);
  30. final float dy = percentageOfNextCircle
  31. * (getCenterYForRow(nextCell.row) - centerY);
  32. mInProgressX = centerX + dx;
  33. mInProgressY = centerY + dy;
  34. }
  35. // TODO: Infinite loop here...
  36. invalidate();
  37. }
  38. final float squareWidth = mSquareWidth;
  39. final float squareHeight = mSquareHeight;
  40. float radius = (squareWidth * mDiameterFactor * 0.5f);
  41. mPathPaint.setStrokeWidth(radius);
  42. final Path currentPath = mCurrentPath;
  43. currentPath.rewind();
  44. // TODO: the path should be created and cached every time we hit-detect
  45. // a cell
  46. // only the last segment of the path should be computed here
  47. // draw the path of the pattern (unless the user is in progress, and
  48. // we are in stealth mode)
  49. final boolean drawPath = (!mInStealthMode || mPatternDisplayMode == DisplayMode.Wrong);
  50. // draw the arrows associated with the path (unless the user is in
  51. // progress, and
  52. // we are in stealth mode)
  53. boolean oldFlag = (mPaint.getFlags() & Paint.FILTER_BITMAP_FLAG) != 0;
  54. mPaint.setFilterBitmap(true); // draw with higher quality since we
  55. // render with transforms
  56. // draw the lines
  57. if (drawPath) {
  58. boolean anyCircles = false;
  59. for (int i = 0; i < count; i++) {
  60. Cell cell = pattern.get(i);
  61. // only draw the part of the pattern stored in
  62. // the lookup table (this is only different in the case
  63. // of animation).
  64. if (!drawLookup[cell.row][cell.column]) {
  65. break;
  66. }
  67. anyCircles = true;
  68. float centerX = getCenterXForColumn(cell.column);
  69. float centerY = getCenterYForRow(cell.row);
  70. if (i == 0) {
  71. currentPath.moveTo(centerX, centerY);
  72. } else {
  73. currentPath.lineTo(centerX, centerY);
  74. }
  75. }
  76. // add last in progress section
  77. if ((mPatternInProgress || mPatternDisplayMode == DisplayMode.Animate)
  78. && anyCircles) {
  79. currentPath.lineTo(mInProgressX, mInProgressY);
  80. }
  81. // chang the line color in different DisplayMode
  82. if (mPatternDisplayMode == DisplayMode.Wrong)
  83. mPathPaint.setColor(Color.RED);
  84. else
  85. mPathPaint.setColor(Color.YELLOW);
  86. canvas.drawPath(currentPath, mPathPaint);
  87. }
  88. // draw the circles
  89. final int paddingTop = getPaddingTop();
  90. final int paddingLeft = getPaddingLeft();
  91. for (int i = 0; i < 3; i++) {
  92. float topY = paddingTop + i * squareHeight;
  93. // float centerY = mPaddingTop + i * mSquareHeight + (mSquareHeight
  94. // / 2);
  95. for (int j = 0; j < 3; j++) {
  96. float leftX = paddingLeft + j * squareWidth;
  97. drawCircle(canvas, (int) leftX, (int) topY, drawLookup[i][j]);
  98. }
  99. }
  100. mPaint.setFilterBitmap(oldFlag); // restore default flag
  101. }

2.第二个值得学习的地方是(代码来自设置应用中):在创建解锁图案时的枚举使用,原生代码中使用了很多枚举,将绘制图案时的状态、底部两个按钮状态、顶部一个TextView显示的提示文字都紧密的联系起来。因此,只用监听LockPatternView动态变化,对应改变底部Button和顶部TextView的状态即可实现联动,简单的方法可以实现很多代码才能实现的逻辑,个人很喜欢。

①全局的状态:

[java] view plain copy  
  1. /**
  2. * Keep track internally of where the user is in choosing a pattern.
  3. */
  4. protected enum Stage {
  5. // 初始状态
  6. Introduction(R.string.lockpattern_recording_intro_header,
  7. LeftButtonMode.Cancel, RightButtonMode.ContinueDisabled,
  8. ID_EMPTY_MESSAGE, true),
  9. // 帮助状态
  10. HelpScreen(R.string.lockpattern_settings_help_how_to_record,
  11. LeftButtonMode.Gone, RightButtonMode.Ok, ID_EMPTY_MESSAGE,
  12. false),
  13. // 绘制过短
  14. ChoiceTooShort(R.string.lockpattern_recording_incorrect_too_short,
  15. LeftButtonMode.Retry, RightButtonMode.ContinueDisabled,
  16. ID_EMPTY_MESSAGE, true),
  17. // 第一次绘制图案
  18. FirstChoiceValid(R.string.lockpattern_pattern_entered_header,
  19. LeftButtonMode.Retry, RightButtonMode.Continue,
  20. ID_EMPTY_MESSAGE, false),
  21. // 需要再次绘制确认
  22. NeedToConfirm(R.string.lockpattern_need_to_confirm,
  23. LeftButtonMode.Cancel, RightButtonMode.ConfirmDisabled,
  24. ID_EMPTY_MESSAGE, true),
  25. // 确认出错
  26. ConfirmWrong(R.string.lockpattern_need_to_unlock_wrong,
  27. LeftButtonMode.Cancel, RightButtonMode.ConfirmDisabled,
  28. ID_EMPTY_MESSAGE, true),
  29. // 选择确认
  30. ChoiceConfirmed(R.string.lockpattern_pattern_confirmed_header,
  31. LeftButtonMode.Cancel, RightButtonMode.Confirm,
  32. ID_EMPTY_MESSAGE, false);
  33. /**
  34. * @param headerMessage
  35. *            The message displayed at the top.
  36. * @param leftMode
  37. *            The mode of the left button.
  38. * @param rightMode
  39. *            The mode of the right button.
  40. * @param footerMessage
  41. *            The footer message.
  42. * @param patternEnabled
  43. *            Whether the pattern widget is enabled.
  44. */
  45. Stage(int headerMessage, LeftButtonMode leftMode,
  46. RightButtonMode rightMode, int footerMessage,
  47. boolean patternEnabled) {
  48. this.headerMessage = headerMessage;
  49. this.leftMode = leftMode;
  50. this.rightMode = rightMode;
  51. this.footerMessage = footerMessage;
  52. this.patternEnabled = patternEnabled;
  53. }
  54. final int headerMessage;
  55. final LeftButtonMode leftMode;
  56. final RightButtonMode rightMode;
  57. final int footerMessage;
  58. final boolean patternEnabled;
  59. }

②.底部两个按钮的状态枚举:

[java] view plain copy  
  1. /**
  2. * The states of the left footer button.
  3. */
  4. enum LeftButtonMode {
  5. // 取消
  6. Cancel(android.R.string.cancel, true),
  7. // 取消时禁用
  8. CancelDisabled(android.R.string.cancel, false),
  9. // 重试
  10. Retry(R.string.lockpattern_retry_button_text, true),
  11. // 重试时禁用
  12. RetryDisabled(R.string.lockpattern_retry_button_text, false),
  13. // 消失
  14. Gone(ID_EMPTY_MESSAGE, false);
  15. /**
  16. * @param text
  17. *            The displayed text for this mode.
  18. * @param enabled
  19. *            Whether the button should be enabled.
  20. */
  21. LeftButtonMode(int text, boolean enabled) {
  22. this.text = text;
  23. this.enabled = enabled;
  24. }
  25. final int text;
  26. final boolean enabled;
  27. }
  28. /**
  29. * The states of the right button.
  30. */
  31. enum RightButtonMode {
  32. // 继续
  33. Continue(R.string.lockpattern_continue_button_text, true),
  34. //继续时禁用
  35. ContinueDisabled(R.string.lockpattern_continue_button_text, false),
  36. //确认
  37. Confirm(R.string.lockpattern_confirm_button_text, true),
  38. //确认是禁用
  39. ConfirmDisabled(R.string.lockpattern_confirm_button_text, false),
  40. //OK
  41. Ok(android.R.string.ok, true);
  42. /**
  43. * @param text
  44. *            The displayed text for this mode.
  45. * @param enabled
  46. *            Whether the button should be enabled.
  47. */
  48. RightButtonMode(int text, boolean enabled) {
  49. this.text = text;
  50. this.enabled = enabled;
  51. }
  52. final int text;
  53. final boolean enabled;
  54. }

就这样,只要LockPatternView的状态一发生改变,就会动态改变底部两个Button的文字和状态。很简洁,逻辑性很强。

3.第三个个人觉得比较有用的就是加密这一块了,为了以后方便使用,我把图案加密和字符加密分成两个工具类:LockPatternUtils和LockPasswordUtils两个文件,本文使用到的是LockPatternUtils。其实所谓的图案加密也是将其通过SHA-1加密转化成二进制数再保存到文件中(原生系统保存在/system/目录下,我这里没有权限,就保存到本应用目录下),解密时,也是将获取到用户的输入通过同样的方法加密,再与保存到文件中的对比,相同则密码正确,不同则密码错误。关健代码就是以下4个函数:

[java] view plain copy  
  1. /**
  2. * Serialize a pattern. 加密
  3. *
  4. * @param pattern
  5. *            The pattern.
  6. * @return The pattern in string form.
  7. */
  8. public static String patternToString(List<LockPatternView.Cell> pattern) {
  9. if (pattern == null) {
  10. return "";
  11. }
  12. final int patternSize = pattern.size();
  13. byte[] res = new byte[patternSize];
  14. for (int i = 0; i < patternSize; i++) {
  15. LockPatternView.Cell cell = pattern.get(i);
  16. res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
  17. }
  18. return new String(res);
  19. }
  20. /**
  21. * Save a lock pattern.
  22. *
  23. * @param pattern
  24. *            The new pattern to save.
  25. * @param isFallback
  26. *            Specifies if this is a fallback to biometric weak
  27. */
  28. public void saveLockPattern(List<LockPatternView.Cell> pattern) {
  29. // Compute the hash
  30. final byte[] hash = LockPatternUtils.patternToHash(pattern);
  31. try {
  32. // Write the hash to file
  33. RandomAccessFile raf = new RandomAccessFile(sLockPatternFilename,
  34. "rwd");
  35. // Truncate the file if pattern is null, to clear the lock
  36. if (pattern == null) {
  37. raf.setLength(0);
  38. } else {
  39. raf.write(hash, 0, hash.length);
  40. }
  41. raf.close();
  42. } catch (FileNotFoundException fnfe) {
  43. // Cant do much, unless we want to fail over to using the settings
  44. // provider
  45. Log.e(TAG, "Unable to save lock pattern to " + sLockPatternFilename);
  46. } catch (IOException ioe) {
  47. // Cant do much
  48. Log.e(TAG, "Unable to save lock pattern to " + sLockPatternFilename);
  49. }
  50. }
  51. /*
  52. * Generate an SHA-1 hash for the pattern. Not the most secure, but it is at
  53. * least a second level of protection. First level is that the file is in a
  54. * location only readable by the system process.
  55. *
  56. * @param pattern the gesture pattern.
  57. *
  58. * @return the hash of the pattern in a byte array.
  59. */
  60. private static byte[] patternToHash(List<LockPatternView.Cell> pattern) {
  61. if (pattern == null) {
  62. return null;
  63. }
  64. final int patternSize = pattern.size();
  65. byte[] res = new byte[patternSize];
  66. for (int i = 0; i < patternSize; i++) {
  67. LockPatternView.Cell cell = pattern.get(i);
  68. res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
  69. }
  70. try {
  71. MessageDigest md = MessageDigest.getInstance("SHA-1");
  72. byte[] hash = md.digest(res);
  73. return hash;
  74. } catch (NoSuchAlgorithmException nsa) {
  75. return res;
  76. }
  77. }
  78. /**
  79. * Check to see if a pattern matches the saved pattern. If no pattern
  80. * exists, always returns true.
  81. *
  82. * @param pattern
  83. *            The pattern to check.
  84. * @return Whether the pattern matches the stored one.
  85. */
  86. public boolean checkPattern(List<LockPatternView.Cell> pattern) {
  87. try {
  88. // Read all the bytes from the file
  89. RandomAccessFile raf = new RandomAccessFile(sLockPatternFilename,
  90. "r");
  91. final byte[] stored = new byte[(int) raf.length()];
  92. int got = raf.read(stored, 0, stored.length);
  93. raf.close();
  94. if (got <= 0) {
  95. return true;
  96. }
  97. // Compare the hash from the file with the entered pattern's hash
  98. return Arrays.equals(stored,
  99. LockPatternUtils.patternToHash(pattern));
  100. } catch (FileNotFoundException fnfe) {
  101. return true;
  102. } catch (IOException ioe) {
  103. return true;
  104. }
  105. }

Android之高仿手机QQ图案解锁相关推荐

  1. Android之高仿手机QQ聊天

    源代码下载 转载请注明出处,谢谢! 最终版已上传.优化下拉刷新.增加来消息声音提示.主界面改成ViewPager,实现左右滑动.新增群组.最近会话显示条数,开始上班了,不再修改了.谢谢! 国庆这几天, ...

  2. 高仿手机QQ音乐之——Android带进度条的开关

    最新版的手机QQ音乐体验确实不错,发现首页播放按钮可以显示歌曲当前进度条,觉得挺有新意!效果如下: 自己琢磨了下,可以用自定义组件来实现,试着做了一下,效果如下: 整理了下思路,大概设计流程是这样的: ...

  3. 九宫锁屏图案有多少种c语言编程,手机九宫格图案解锁问题,编程高手戳进来!...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 数学吧里看到的一个有趣问题,题目描述很简单: 求手机九宫格图案解锁一共有多少种答案.规则大家应该都知道,至少连四个点,最多连九个点,一条直线上的三个点只有 ...

  4. 仿 手机QQ 登录、注册、找回密码、好友列表、QQ状态等功能的实现

    仿 手机QQ 登录.注册.找回密码.好友列表.QQ状态等功能的实现 全文 图 + 代码 .... 福利!!!(QQ登录背景,过度页面背景) 1.加载过程中的背景 2.登录页面 ==1. 登录页面 布局 ...

  5. Android之高仿QQ6.6.0侧滑效果(背景动画、透明+沉浸式状态栏、渐变效果)

    根据需求实现类似QQ侧滑效果,之前看到过很多实现方式通过SlidingMenu,但是既然官方推出了自己的专属控件,那么使用DrawerLayout就是不二选择.且看下文. 一.先来看看官方文档解释 D ...

  6. Android之高仿墨迹天气桌面组件(AppWidgetProvider) .

    Android之高仿墨迹天气桌面组件(AppWidgetProvider) . 点击:382 发布时间:2012-10-03 更多0 相信墨迹天气,大家都见过,他在时间显示和天气界面上,很吸引人,今天 ...

  7. android布局管理器模仿qq登录效果,Android程序开发仿新版QQ锁屏下弹窗功能

    新版的qq,可以在锁屏下弹窗显示qq消息,正好目前在做的项目也需要这一功能.经过各种试验和资料查找,终于实现,过程不难,但是却有一些地方需要注意. 下面是实现过程. 1.使用Activity,而不是V ...

  8. 一周新闻纵览:房产行业成侵害个人信息高发领域;小心高仿手机APP;

    我荒废了时间,时间便把我给废了 -- 莎士比亚 房产行业成侵害个人信息高发领域 国家市场监管总局执法稽查局局长杨红灿近日说,从案件查办看,房产租售.装饰装修.教育培训成为侵害消费者个人信息违法行为的三 ...

  9. 仿手机QQ网络状态条的显示与消失,没网络时显示,有网络时自动消失 ,点击网络条设置网络

    关注finddreams,一起分享,一起进步: http://blog.csdn.net/finddreams/article/details/44647843 我们都知道手机QQ上有这样一个功能,就 ...

最新文章

  1. python神经结构二层_《python深度学习》笔记---8.3、神经风格迁移
  2. Windows下通过VNC远程访问Linux服务器,并实现可视化
  3. 查询前几条记录SQL在不同数据库中的用法
  4. textmate开发一个blog
  5. Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
  6. jprofiler_windows-x64_9_1注册码
  7. 2019-06-02 Java学习日记之多线程上
  8. 拓端tecdat|R语言多元动态条件相关DCC-MVGARCH、常相关CCC-MVGARCH模型进行多变量股市波动率预测
  9. html返回到处乱跑,电脑鼠标乱动不受控制(鼠标到处乱跑不听使唤)
  10. ADS1115使用之电路与程序
  11. web 前端入坑第一篇:web前端到底是什么?有前途吗
  12. 【附案例】UI交互设计不会做?设计大神带你开启动效灵感之路
  13. 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,求该数
  14. 质量小议19 -- 熵
  15. linux的原理和运用,Linux操作系统原理与应用_内存寻址
  16. java 黑马头条 day4 自媒体文章发布 自媒体文章列表查询 频道列表展示 自媒体文章-发布、修改、保存草稿 自媒体文章-根据id查询 自媒体文章-删除
  17. 阿里JAVA实习生面试总结(2019年春招)
  18. ernel 3.10内核源码分析--KVM相关--虚拟机运行
  19. 移远EC20在linux下驱动移植
  20. 看看您电脑里哪些端口会被入侵?

热门文章

  1. 后台配置商品规格sku(vue+element)
  2. 使用sql语句实现 报表的小计合计
  3. html中根号怎么写,HTML特殊符号对应代码
  4. git 命令行配置及配置文件 解决clone报错 LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
  5. java计算机毕业设计驾校管理系统MyBatis+系统+LW文档+源码+调试部署
  6. UESTC 2014 Summer Training #7 Div.2
  7. HSSFWorkbook 创建Excel文件
  8. 当存储遇到开源 可以先从冷数据着手
  9. 写在离职前,我在富士康的这两年
  10. vue项目通过url链接引入其他系统页面