文章详情

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

请输入下面的图形验证码

提交验证

短信预约提醒成功

轻松实现Android自定义九宫格图案解锁

2022-06-06 06:57

关注

Android实现九宫格图案解锁,自带将图案转化成数字密码的功能,代码如下:

LockPatternView.java


package com.jackie.lockpattern; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.text.TextUtils; 
import android.util.AttributeSet; 
import android.util.TypedValue; 
import android.view.MotionEvent; 
import android.view.View; 
import java.util.ArrayList; 
import java.util.List; 
 
public class LockPatternView extends View { 
  
 private Paint mCirclePaint; 
  
 private Paint mLinePaint; 
  
 private PointView[][] mPointViewArray = new PointView[3][3]; 
  
 private List<PointView> mSelectedPointViewList; 
  
 private int mPatternWidth; 
  
 private OnPatternChangeListener mOnPatternChangeListener; 
  
 private float mRadius; 
  
 private int mIndex = 1; 
  
 private boolean mIsSelected; 
  
 private boolean mIsFinished; 
  
 private boolean mIsMovingWithoutCircle = false; 
 private float mCurrentX, mCurrentY; 
  
 private static final int NORMAL_COLOR = 0xFF70DBDB; 
  
 private static final int SELECTED_COLOR = 0xFF979797; 
 public LockPatternView(Context context) { 
  this(context, null); 
 } 
 public LockPatternView(Context context, AttributeSet attrs) { 
  super(context, attrs); 
  mCirclePaint = new Paint(); 
  mCirclePaint.setAntiAlias(true); 
  mCirclePaint.setDither(true); 
  mCirclePaint.setColor(NORMAL_COLOR); 
  mCirclePaint.setStyle(Paint.Style.FILL); 
  mLinePaint = new Paint(); 
  mLinePaint.setAntiAlias(true); 
  mLinePaint.setDither(true); 
  mLinePaint.setStrokeWidth(20); 
  mLinePaint.setColor(SELECTED_COLOR); 
  mLinePaint.setStyle(Paint.Style.STROKE); 
  mRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()); 
  mSelectedPointViewList = new ArrayList<>(); 
 } 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  //取屏幕长和宽中的较小值作为图案的边长 
  mPatternWidth = Math.min(getMeasuredWidth(), getMeasuredHeight()); 
  setMeasuredDimension(mPatternWidth, mPatternWidth); 
 } 
 @Override 
 protected void onDraw(Canvas canvas) { 
  //画圆 
  drawCircle(canvas); 
  //将选中的圆重新绘制一遍,将选中的点和未选中的点区别开来 
  for (PointView pointView : mSelectedPointViewList) { 
   mCirclePaint.setColor(SELECTED_COLOR); 
   canvas.drawCircle(pointView.x, pointView.y, mRadius, mCirclePaint); 
   mCirclePaint.setColor(NORMAL_COLOR); //每重新绘制一个,将画笔的颜色重置,保证不会影响其他圆的绘制 
  } 
  //点与点画线 
  if (mSelectedPointViewList.size() > 0) { 
   Point pointViewA = mSelectedPointViewList.get(0); //第一个选中的点为A点 
   for (int i = 0; i < mSelectedPointViewList.size(); i++) { 
    Point pointViewB = mSelectedPointViewList.get(i); //其他依次遍历出来的点为B点 
    drawLine(canvas, pointViewA, pointViewB); 
    pointViewA = pointViewB; 
   } 
   //点与鼠标当前位置绘制轨迹 
   if (mIsMovingWithoutCircle & !mIsFinished) { 
    drawLine(canvas, pointViewA, new PointView((int)mCurrentX, (int)mCurrentY)); 
   } 
  } 
  super.onDraw(canvas); 
 } 
  
 private void drawCircle(Canvas canvas) { 
  //初始化点的位置 
  for (int i = 0; i < mPointViewArray.length; i++) { 
   for (int j = 0; j < mPointViewArray.length; j++) { 
    //圆心的坐标 
    int cx = mPatternWidth / 4 * (j + 1); 
    int cy = mPatternWidth / 4 * (i + 1); 
    //将圆心放在一个点数组中 
    PointView pointView = new PointView(cx, cy); 
    pointView.setIndex(mIndex); 
    mPointViewArray[i][j] = pointView; 
    canvas.drawCircle(cx, cy, mRadius, mCirclePaint); 
    mIndex++; 
   } 
  } 
  mIndex = 1; 
 } 
  
 private void drawLine(Canvas canvas, Point pointA, Point pointB) { 
  canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, mLinePaint); 
 } 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  mCurrentX = event.getX(); 
  mCurrentY = event.getY(); 
  PointView selectedPointView = null; 
  switch(event.getAction()) { 
   case MotionEvent.ACTION_DOWN: 
    //重新绘制 
    if (mOnPatternChangeListener != null) { 
     mOnPatternChangeListener.onPatternStarted(true); 
    } 
    mSelectedPointViewList.clear(); 
    mIsFinished = false; 
    selectedPointView = checkSelectPoint(); 
    if (selectedPointView != null) { 
     //第一次按下的位置在圆内,被选中 
     mIsSelected = true; 
    } 
    break; 
   case MotionEvent.ACTION_MOVE: 
    if (mIsSelected) { 
     selectedPointView = checkSelectPoint(); 
    } 
    if (selectedPointView == null) { 
     mIsMovingWithoutCircle = true; 
    } 
    break; 
   case MotionEvent.ACTION_UP: 
    mIsFinished = true; 
    mIsSelected = false; 
    break; 
  } 
  //将选中的点收集起来 
  if (!mIsFinished && mIsSelected && selectedPointView != null) { 
   if (!mSelectedPointViewList.contains(selectedPointView)) { 
    mSelectedPointViewList.add(selectedPointView); 
   } 
  } 
  if (mIsFinished) { 
   if (mSelectedPointViewList.size() == 1) { 
    mSelectedPointViewList.clear(); 
   } else if (mSelectedPointViewList.size() < 5 && mSelectedPointViewList.size() > 0) { 
    //绘制错误 
    if (mOnPatternChangeListener != null) { 
     mOnPatternChangeListener.onPatternChange(null); 
    } 
   } else { 
    //绘制成功 
    String patternPassword = ""; 
    if (mOnPatternChangeListener != null) { 
     for (PointView pointView : mSelectedPointViewList) { 
      patternPassword += pointView.getIndex(); 
     } 
     if (!TextUtils.isEmpty(patternPassword)) { 
      mOnPatternChangeListener.onPatternChange(patternPassword); 
     } 
    } 
   } 
  } 
  invalidate(); 
  return true; 
 } 
  
 private PointView checkSelectPoint() { 
  for (int i = 0; i < mPointViewArray.length; i++) { 
   for (int j = 0; j < mPointViewArray.length; j++) { 
    PointView pointView = mPointViewArray[i][j]; 
    if (isWithinCircle(mCurrentX, mCurrentY, pointView.x, pointView.y, mRadius)) { 
     return pointView; 
    } 
   } 
  } 
  return null; 
 } 
  
 private boolean isWithinCircle(float x, float y, float cx, float cy, float radius) { 
  //如果点和圆心的距离小于半径,则证明点在圆内 
  if (Math.sqrt(Math.pow(x - cx, 2) + Math.pow(y- cy, 2)) <= radius) { 
   return true; 
  } 
  return false; 
 } 
  
 public void setOnPatternChangeListener(OnPatternChangeListener onPatternChangeListener) { 
  if (onPatternChangeListener != null) { 
   this.mOnPatternChangeListener = onPatternChangeListener; 
  } 
 } 
  
 public interface OnPatternChangeListener { 
   
  void onPatternChange(String patternPassword); 
   
  void onPatternStarted(boolean isStarted); 
 } 
} 

PointView.java


package com.jackie.lockpattern; 
import android.graphics.Point; 
 
public class PointView extends Point { 
 //用于转化密码的下标 
 public int index; 
 public PointView(int x, int y) { 
  super(x, y); 
 } 
 public int getIndex() { 
  return index; 
 } 
 public void setIndex(int index) { 
  this.index = index; 
 } 
} 

MainActivity.java


package com.jackie.lockpattern; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
public class MainActivity extends Activity implements LockPatternView.OnPatternChangeListener { 
 private TextView mLockPatternHint; 
 private LockPatternView mLockPatternView; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  mLockPatternHint = (TextView) findViewById(R.id.lock_pattern_hint); 
  mLockPatternView = (LockPatternView) findViewById(R.id.lock_pattern_view); 
  mLockPatternView.setOnPatternChangeListener(this); 
 } 
 @Override 
 public void onPatternChange(String patternPassword) { 
  if (patternPassword == null) { 
   mLockPatternHint.setText("至少5个点"); 
  } else { 
   mLockPatternHint.setText(patternPassword); 
  } 
 } 
 @Override 
 public void onPatternStarted(boolean isStarted) { 
  if (isStarted) { 
   mLockPatternHint.setText("请绘制图案"); 
  } 
 } 
} 

 效果图如下:

附上源码地址:https://github.com/shineflower/LockPattern.git

您可能感兴趣的文章:Android 自定义TextView实现滑动解锁高亮文字Android自定义滑动解锁控件使用详解使用Android自定义控件实现滑动解锁九宫格android滑动解锁震动效果的开启和取消android 九宫格滑动解锁开机实例源码学习Android点亮屏幕或屏幕解锁和锁定以及其他相关权限实现代码Android 监听锁屏、解锁、开屏 功能代码Android实现九宫格解锁的方法Android指纹解锁方法解析Android模拟实现滑动解锁界面


阅读原文内容投诉

免责声明:

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

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

软考中级精品资料免费领

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

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

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

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

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

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

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