文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

怎么在Android应用中实现一个图案解锁功能

2023-05-31 03:29

关注

这期内容当中小编将会给大家带来有关怎么在Android应用中实现一个图案解锁功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

最关健的就是那个自定义九宫格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 } 

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯