Android仿手机QQ图案解锁功能
发布时间 - 2026-01-11 01:11:28 点击率:次本文实例为大家分享了Android仿手机QQ图案解锁的具体代码,供大家参考,具体内容如下

ps:请不要再问我,为什么导入之后会乱码了。
其实,代码基本上都是从原生系统中提取的:LockPatternView、加密工具类,以及解锁逻辑等,我只是稍作修改,大家都知道,原生系统界面比较丑陋,因此,我特意把QQ的apk解压了,从中拿了几张图案解锁的图片,一个简单的例子就这样诞生了。
好了,废话不多说,我们来看看效果(最后两张是最新4.4系统,炫一下,呵呵):
1.最关健的就是那个自定义九宫格View,代码来自framework下:LockPatternView,原生系统用的图片资源比较多,好像有7、8张吧,而且绘制的比较复杂,我找寻半天,眼睛都找瞎了,发现解压的QQ里面就3张图片,一个圈圈,两个点,没办法,只能修改代码了,在修改的过程中,才发现,其实可以把原生的LockPatternView给简化,绘制更少的图片,达到更好的效果。总共优化有:①去掉了连线的箭头,②原生的连线只有白色一种,改成根据不同状态显示黄色和红色两张色,③.原生view是先画点再画线,使得线覆盖在点的上面,影响美观,改成先画连线再画点。
关健部分代码onDraw函数:
@Override
protected void onDraw(Canvas canvas) {
final ArrayList<Cell> pattern = mPattern;
final int count = pattern.size();
final boolean[][] drawLookup = mPatternDrawLookup;
if (mPatternDisplayMode == DisplayMode.Animate) {
// figure out which circles to draw
// + 1 so we pause on complete pattern
final int oneCycle = (count + 1) * MILLIS_PER_CIRCLE_ANIMATING;
final int spotInCycle = (int) (SystemClock.elapsedRealtime() - mAnimatingPeriodStart)
% oneCycle;
final int numCircles = spotInCycle / MILLIS_PER_CIRCLE_ANIMATING;
clearPatternDrawLookup();
for (int i = 0; i < numCircles; i++) {
final Cell cell = pattern.get(i);
drawLookup[cell.getRow()][cell.getColumn()] = true;
}
// figure out in progress portion of ghosting line
final boolean needToUpdateInProgressPoint = numCircles > 0
&& numCircles < count;
if (needToUpdateInProgressPoint) {
final float percentageOfNextCircle = ((float) (spotInCycle % MILLIS_PER_CIRCLE_ANIMATING))
/ MILLIS_PER_CIRCLE_ANIMATING;
final Cell currentCell = pattern.get(numCircles - 1);
final float centerX = getCenterXForColumn(currentCell.column);
final float centerY = getCenterYForRow(currentCell.row);
final Cell nextCell = pattern.get(numCircles);
final float dx = percentageOfNextCircle
* (getCenterXForColumn(nextCell.column) - centerX);
final float dy = percentageOfNextCircle
* (getCenterYForRow(nextCell.row) - centerY);
mInProgressX = centerX + dx;
mInProgressY = centerY + dy;
}
// TODO: Infinite loop here...
invalidate();
}
final float squareWidth = mSquareWidth;
final float squareHeight = mSquareHeight;
float radius = (squareWidth * mDiameterFactor * 0.5f);
mPathPaint.setStrokeWidth(radius);
final Path currentPath = mCurrentPath;
currentPath.rewind();
// TODO: the path should be created and cached every time we hit-detect
// a cell
// only the last segment of the path should be computed here
// draw the path of the pattern (unless the user is in progress, and
// we are in stealth mode)
final boolean drawPath = (!mInStealthMode || mPatternDisplayMode == DisplayMode.Wrong);
// draw the arrows associated with the path (unless the user is in
// progress, and
// we are in stealth mode)
boolean oldFlag = (mPaint.getFlags() & Paint.FILTER_BITMAP_FLAG) != 0;
mPaint.setFilterBitmap(true); // draw with higher quality since we
// render with transforms
// draw the lines
if (drawPath) {
boolean anyCircles = false;
for (int i = 0; i < count; i++) {
Cell cell = pattern.get(i);
// only draw the part of the pattern stored in
// the lookup table (this is only different in the case
// of animation).
if (!drawLookup[cell.row][cell.column]) {
break;
}
anyCircles = true;
float centerX = getCenterXForColumn(cell.column);
float centerY = getCenterYForRow(cell.row);
if (i == 0) {
currentPath.moveTo(centerX, centerY);
} else {
currentPath.lineTo(centerX, centerY);
}
}
// add last in progress section
if ((mPatternInProgress || mPatternDisplayMode == DisplayMode.Animate)
&& anyCircles) {
currentPath.lineTo(mInProgressX, mInProgressY);
}
// chang the line color in different DisplayMode
if (mPatternDisplayMode == DisplayMode.Wrong)
mPathPaint.setColor(Color.RED);
else
mPathPaint.setColor(Color.YELLOW);
canvas.drawPath(currentPath, mPathPaint);
}
// draw the circles
final int paddingTop = getPaddingTop();
final int paddingLeft = getPaddingLeft();
for (int i = 0; i < 3; i++) {
float topY = paddingTop + i * squareHeight;
// float centerY = mPaddingTop + i * mSquareHeight + (mSquareHeight
// / 2);
for (int j = 0; j < 3; j++) {
float leftX = paddingLeft + j * squareWidth;
drawCircle(canvas, (int) leftX, (int) topY, drawLookup[i][j]);
}
}
mPaint.setFilterBitmap(oldFlag); // restore default flag
}
2.第二个值得学习的地方是(代码来自设置应用中):在创建解锁图案时的枚举使用,原生代码中使用了很多枚举,将绘制图案时的状态、底部两个按钮状态、顶部一个TextView显示的提示文字都紧密的联系起来。因此,只用监听LockPatternView动态变化,对应改变底部Button和顶部TextView的状态即可实现联动,简单的方法可以实现很多代码才能实现的逻辑,个人很喜欢。
①全局的状态:
/**
* Keep track internally of where the user is in choosing a pattern.
*/
protected enum Stage {
// 初始状态
Introduction(R.string.lockpattern_recording_intro_header,
LeftButtonMode.Cancel, RightButtonMode.ContinueDisabled,
ID_EMPTY_MESSAGE, true),
// 帮助状态
HelpScreen(R.string.lockpattern_settings_help_how_to_record,
LeftButtonMode.Gone, RightButtonMode.Ok, ID_EMPTY_MESSAGE,
false),
// 绘制过短
ChoiceTooShort(R.string.lockpattern_recording_incorrect_too_short,
LeftButtonMode.Retry, RightButtonMode.ContinueDisabled,
ID_EMPTY_MESSAGE, true),
// 第一次绘制图案
FirstChoiceValid(R.string.lockpattern_pattern_entered_header,
LeftButtonMode.Retry, RightButtonMode.Continue,
ID_EMPTY_MESSAGE, false),
// 需要再次绘制确认
NeedToConfirm(R.string.lockpattern_need_to_confirm,
LeftButtonMode.Cancel, RightButtonMode.ConfirmDisabled,
ID_EMPTY_MESSAGE, true),
// 确认出错
ConfirmWrong(R.string.lockpattern_need_to_unlock_wrong,
LeftButtonMode.Cancel, RightButtonMode.ConfirmDisabled,
ID_EMPTY_MESSAGE, true),
// 选择确认
ChoiceConfirmed(R.string.lockpattern_pattern_confirmed_header,
LeftButtonMode.Cancel, RightButtonMode.Confirm,
ID_EMPTY_MESSAGE, false);
/**
* @param headerMessage
* The message displayed at the top.
* @param leftMode
* The mode of the left button.
* @param rightMode
* The mode of the right button.
* @param footerMessage
* The footer message.
* @param patternEnabled
* Whether the pattern widget is enabled.
*/
Stage(int headerMessage, LeftButtonMode leftMode,
RightButtonMode rightMode, int footerMessage,
boolean patternEnabled) {
this.headerMessage = headerMessage;
this.leftMode = leftMode;
this.rightMode = rightMode;
this.footerMessage = footerMessage;
this.patternEnabled = patternEnabled;
}
final int headerMessage;
final LeftButtonMode leftMode;
final RightButtonMode rightMode;
final int footerMessage;
final boolean patternEnabled;
}
②.底部两个按钮的状态枚举:
/**
* The states of the left footer button.
*/
enum LeftButtonMode {
// 取消
Cancel(android.R.string.cancel, true),
// 取消时禁用
CancelDisabled(android.R.string.cancel, false),
// 重试
Retry(R.string.lockpattern_retry_button_text, true),
// 重试时禁用
RetryDisabled(R.string.lockpattern_retry_button_text, false),
// 消失
Gone(ID_EMPTY_MESSAGE, false);
/**
* @param text
* The displayed text for this mode.
* @param enabled
* Whether the button should be enabled.
*/
LeftButtonMode(int text, boolean enabled) {
this.text = text;
this.enabled = enabled;
}
final int text;
final boolean enabled;
}
/**
* The states of the right button.
*/
enum RightButtonMode {
// 继续
Continue(R.string.lockpattern_continue_button_text, true),
//继续时禁用
ContinueDisabled(R.string.lockpattern_continue_button_text, false),
//确认
Confirm(R.string.lockpattern_confirm_button_text, true),
//确认是禁用
ConfirmDisabled(R.string.lockpattern_confirm_button_text, false),
//OK
Ok(android.R.string.ok, true);
/**
* @param text
* The displayed text for this mode.
* @param enabled
* Whether the button should be enabled.
*/
RightButtonMode(int text, boolean enabled) {
this.text = text;
this.enabled = enabled;
}
final int text;
final boolean enabled;
}
就这样,只要LockPatternView的状态一发生改变,就会动态改变底部两个Button的文字和状态。很简洁,逻辑性很强。
3.第三个个人觉得比较有用的就是加密这一块了,为了以后方便使用,我把图案加密和字符加密分成两个工具类:LockPatternUtils和LockPasswordUtils两个文件,本文使用到的是LockPatternUtils。其实所谓的图案加密也是将其通过SHA-1加密转化成二进制数再保存到文件中(原生系统保存在/system/目录下,我这里没有权限,就保存到本应用目录下),解密时,也是将获取到用户的输入通过同样的方法加密,再与保存到文件中的对比,相同则密码正确,不同则密码错误。关健代码就是以下4个函数:
/**
* Serialize a pattern. 加密
*
* @param pattern
* The pattern.
* @return The pattern in string form.
*/
public static String patternToString(List<LockPatternView.Cell> pattern) {
if (pattern == null) {
return "";
}
final int patternSize = pattern.size();
byte[] res = new byte[patternSize];
for (int i = 0; i < patternSize; i++) {
LockPatternView.Cell cell = pattern.get(i);
res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
}
return new String(res);
}
/**
* Save a lock pattern.
*
* @param pattern
* The new pattern to save.
* @param isFallback
* Specifies if this is a fallback to biometric weak
*/
public void saveLockPattern(List<LockPatternView.Cell> pattern) {
// Compute the hash
final byte[] hash = LockPatternUtils.patternToHash(pattern);
try {
// Write the hash to file
RandomAccessFile raf = new RandomAccessFile(sLockPatternFilename,
"rwd");
// Truncate the file if pattern is null, to clear the lock
if (pattern == null) {
raf.setLength(0);
} else {
raf.write(hash, 0, hash.length);
}
raf.close();
} catch (FileNotFoundException fnfe) {
// Cant do much, unless we want to fail over to using the settings
// provider
Log.e(TAG, "Unable to save lock pattern to " + sLockPatternFilename);
} catch (IOException ioe) {
// Cant do much
Log.e(TAG, "Unable to save lock pattern to " + sLockPatternFilename);
}
}
/*
* Generate an SHA-1 hash for the pattern. Not the most secure, but it is at
* least a second level of protection. First level is that the file is in a
* location only readable by the system process.
*
* @param pattern the gesture pattern.
*
* @return the hash of the pattern in a byte array.
*/
private static byte[] patternToHash(List<LockPatternView.Cell> pattern) {
if (pattern == null) {
return null;
}
final int patternSize = pattern.size();
byte[] res = new byte[patternSize];
for (int i = 0; i < patternSize; i++) {
LockPatternView.Cell cell = pattern.get(i);
res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
}
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hash = md.digest(res);
return hash;
} catch (NoSuchAlgorithmException nsa) {
return res;
}
}
/**
* Check to see if a pattern matches the saved pattern. If no pattern
* exists, always returns true.
*
* @param pattern
* The pattern to check.
* @return Whether the pattern matches the stored one.
*/
public boolean checkPattern(List<LockPatternView.Cell> pattern) {
try {
// Read all the bytes from the file
RandomAccessFile raf = new RandomAccessFile(sLockPatternFilename,
"r");
final byte[] stored = new byte[(int) raf.length()];
int got = raf.read(stored, 0, stored.length);
raf.close();
if (got <= 0) {
return true;
}
// Compare the hash from the file with the entered pattern's hash
return Arrays.equals(stored,
LockPatternUtils.patternToHash(pattern));
} catch (FileNotFoundException fnfe) {
return true;
} catch (IOException ioe) {
return true;
}
}
好了,代码就分析到这里,非常感谢你看到了文章末尾。
本文源码(utf-8编码):Android仿手机QQ图案解锁
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android手机QQ图案解锁
# Android手机图案解锁
# Android图案解锁
# Android唤醒、解锁屏幕代码实例
# android 九宫格滑动解锁开机实例源码学习
# android滑动解锁震动效果的开启和取消
# Android实现九宫格解锁
# 轻松实现Android自定义九宫格图案解锁
# Android指纹解锁方法解析
# Android手机屏幕敲击解锁功能代码
# Android APP数字解锁实例详解
# Android指纹解锁示例代码
# 轻松实现安卓(Android)九宫格解锁
# 解锁
# 好了
# 关健
# 两张
# 重试
# 的是
# 这一
# 就会
# 目录下
# 大家都
# 你看
# 是从
# 将其
# 我把
# 问我
# 没办法
# 半天
# 第二个
# 很喜欢
# 才发现
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Python文件流缓冲机制_IO性能解析【教程】
什么是javascript作用域_全局和局部作用域有什么区别?
如何快速打造个性化非模板自助建站?
香港服务器网站推广:SEO优化与外贸独立站搭建策略
Laravel怎么实现API接口鉴权_Laravel Sanctum令牌生成与请求验证【教程】
Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】
大学网站设计制作软件有哪些,如何将网站制作成自己app?
EditPlus 正则表达式 实战(3)
中山网站制作网页,中山新生登记系统登记流程?
Laravel如何自定义错误页面(404, 500)?(代码示例)
Android GridView 滑动条设置一直显示状态(推荐)
node.js报错:Cannot find module 'ejs'的解决办法
Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置
Laravel中的Facade(门面)到底是什么原理
HTML5打空格有哪些误区_新手常犯的空格使用错误【技巧】
python中快速进行多个字符替换的方法小结
Laravel如何使用.env文件管理环境变量?(最佳实践)
Laravel怎么进行数据库事务处理_Laravel DB Facade事务操作确保数据一致性
简历没回改:利用AI润色让你的文字更专业
JS中页面与页面之间超链接跳转中文乱码问题的解决办法
简单实现Android文件上传
香港服务器网站卡顿?如何解决网络延迟与负载问题?
iOS中将个别页面强制横屏其他页面竖屏
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】
Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】
bootstrap日历插件datetimepicker使用方法
如何在阿里云部署织梦网站?
Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析
网站制作企业,网站的banner和导航栏是指什么?
Python3.6正式版新特性预览
JavaScript如何操作视频_媒体API怎么控制播放
EditPlus中的正则表达式实战(5)
Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】
长沙做网站要多少钱,长沙国安网络怎么样?
Laravel如何与Docker(Sail)协同开发?(环境搭建教程)
linux写shell需要注意的问题(必看)
零基础网站服务器架设实战:轻量应用与域名解析配置指南
在centOS 7安装mysql 5.7的详细教程
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
如何在阿里云ECS服务器部署织梦CMS网站?
成都品牌网站制作公司,成都营业执照年报网上怎么办理?
Bootstrap整体框架之JavaScript插件架构
javascript中的try catch异常捕获机制用法分析
Laravel API路由如何设计_Laravel构建RESTful API的路由最佳实践
网站优化排名时,需要考虑哪些问题呢?
laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法

