Android之高仿手机QQ图案解锁

来源:互联网 发布:最新版学越南语软件 编辑:程序博客网 时间:2024/05/01 09:44






http://blog.csdn.net/way_ping_li/article/details/17015153

http://blog.csdn.net/way_ping_li/article/details/17015153

http://blog.csdn.net/way_ping_li/article/details/17015153

http://blog.csdn.net/way_ping_li/article/details/17015153

http://blog.csdn.net/way_ping_li/article/details/17015153


Android之高仿手机QQ图案解锁

分类: Android 4757人阅读 评论(19) 收藏 举报

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

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

①全局的状态:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  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.         /** 
  35.          * @param headerMessage 
  36.          *            The message displayed at the top. 
  37.          * @param leftMode 
  38.          *            The mode of the left button. 
  39.          * @param rightMode 
  40.          *            The mode of the right button. 
  41.          * @param footerMessage 
  42.          *            The footer message. 
  43.          * @param patternEnabled 
  44.          *            Whether the pattern widget is enabled. 
  45.          */  
  46.         Stage(int headerMessage, LeftButtonMode leftMode,  
  47.                 RightButtonMode rightMode, int footerMessage,  
  48.                 boolean patternEnabled) {  
  49.             this.headerMessage = headerMessage;  
  50.             this.leftMode = leftMode;  
  51.             this.rightMode = rightMode;  
  52.             this.footerMessage = footerMessage;  
  53.             this.patternEnabled = patternEnabled;  
  54.         }  
  55.   
  56.         final int headerMessage;  
  57.         final LeftButtonMode leftMode;  
  58.         final RightButtonMode rightMode;  
  59.         final int footerMessage;  
  60.         final boolean patternEnabled;  
  61.     }  

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  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.         /** 
  17.          * @param text 
  18.          *            The displayed text for this mode. 
  19.          * @param enabled 
  20.          *            Whether the button should be enabled. 
  21.          */  
  22.         LeftButtonMode(int text, boolean enabled) {  
  23.             this.text = text;  
  24.             this.enabled = enabled;  
  25.         }  
  26.   
  27.         final int text;  
  28.         final boolean enabled;  
  29.     }  
  30.   
  31.     /** 
  32.      * The states of the right button. 
  33.      */  
  34.     enum RightButtonMode {  
  35.         // 继续  
  36.         Continue(R.string.lockpattern_continue_button_text, true),  
  37.         //继续时禁用  
  38.         ContinueDisabled(R.string.lockpattern_continue_button_text, false),  
  39.         //确认  
  40.         Confirm(R.string.lockpattern_confirm_button_text, true),  
  41.         //确认是禁用  
  42.         ConfirmDisabled(R.string.lockpattern_confirm_button_text, false),  
  43.         //OK  
  44.         Ok(android.R.string.ok, true);  
  45.   
  46.         /** 
  47.          * @param text 
  48.          *            The displayed text for this mode. 
  49.          * @param enabled 
  50.          *            Whether the button should be enabled. 
  51.          */  
  52.         RightButtonMode(int text, boolean enabled) {  
  53.             this.text = text;  
  54.             this.enabled = enabled;  
  55.         }  
  56.   
  57.         final int text;  
  58.         final boolean enabled;  
  59.     }  

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

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

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

好了,代码就分析到这里,非常感谢你看到了文章末尾,很晚了,睡觉去,如果大家有什么问题或建议,欢迎留言,一起讨论,谢谢!


19
1
主题推荐
android手机arraylistframeworkanimation
猜你在找
android 三种实现水平向滑动方式(ViewPager、ViewFilpper、ViewFlow)的比较
Android通过Runtime.getRuntime().exec实现Ping和Traceroute命令时readLine阻塞问题解决
WEBGL中文教程-第五课-为3d图像添加纹理-翻译于外文
linux shell 比较两个浮点数
LayoutInflater 详解
再谈协方差矩阵之主成分分析
android UEventObserver的用法
二叉树中路径和为某整数的所有路径
Android开发—数据库应用—添加列表活动(ListActivity)--分析记事本程序
Android项目实战--手机卫士34--流量管理
查看评论
11楼 autumn_xl 昨天 19:16发表 [回复]
感谢提供 源码帮助我们这些 初学者
10楼 tessoo 2014-07-28 14:30发表 [回复]
亲 qq 多少 想请你做我们的签约讲师 我的电话18611277391
9楼 jackly_n 2014-06-13 13:25发表 [回复]
代码很清晰
8楼 时熊猫Time 2014-05-26 16:07发表 [回复]
谢谢作者分享
7楼 android0012345 2014-05-18 21:20发表 [回复]
期待楼主更多的更精彩的博文,楼主杂不更新了。期待呀
6楼 params 2014-05-16 17:22发表 [回复]
太厉害了, 谢谢!
5楼 蓝槐魂 2014-03-13 12:10发表 [回复]
果断大牛啊,顶起
4楼 冒冒爱编程 2013-12-30 14:57发表 [回复]
大神你很强,小白的楷模!
3楼 彼岸此岸你好吗 2013-12-26 22:54发表 [回复]
顶起!支持下!
2楼 xiaoxueheha 2013-12-02 11:17发表 [回复]
FTP服务器bug问题:如果文件中含有中文(如文件名:XX(破解版).apk),当下载文件时没有问题,但上传时报错。电脑操作系统为win7
1楼 xiaoxueheha 2013-11-29 09:57发表 [回复] [引用] [举报]
楼主啊,你什么时候把你的高仿雅虎天气布局和你的简洁天气结合呢,很期待那应用哦
Re: weidi1989 2013-11-29 10:00发表 [回复] [引用] [举报]
回复xiaoxueheha:遇到了几个比较严重的问题,一直没有找到解决办法,就停下来很久了。呵呵
Re: xiaoxueheha 2013-11-29 10:24发表 [回复] [引用] [举报]
回复weidi1989:一直在用你那天气软件哦,所以好奇的问一下哦 真的很不错哦 ,希望有更好的哦,谢谢

0 0
原创粉丝点击