Android自定义控件实现九宫格解锁功能

这篇文章主要为大家详细介绍了Android自定义控件实现九宫格解锁功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最终Android九宫格解锁效果如下

1.进行定义实体point点

 public class Point { private float x; private float y; //正常模式 public static final int NORMAL_MODE = 1; //按下模式 public static final int PRESSED_MODE = 2; //错误模式 public static final int ERROR_MODE = 3; private int state = NORMAL_MODE; private String mark; public Point(float x, float y, String mark) { this.x = x; this.y = y; this.mark = mark; } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getMark() { return mark; } public void setMark(String mark) { this.mark = mark; } } 

2.自定义ScreenLockView

 public class ScreenLockView extends View { private static final String TAG = "ScreenLockView"; // 错误格子的图片 private Bitmap errorBitmap; // 正常格子的图片 private Bitmap normalBitmap; // 手指按下时格子的图片 private Bitmap pressedBitmap; // 错误时连线的图片 private Bitmap lineErrorBitmap; // 手指按住时连线的图片 private Bitmap linePressedBitmap; // 偏移量,使九宫格在屏幕中央 private int offset; // 九宫格的九个格子是否已经初始化 private boolean init; // 格子的半径 private int radius; // 密码 private String password = "123456"; // 九个格子 private Point[][] points = new Point[3][3]; private int width; private int height; private Matrix matrix = new Matrix(); private float moveX = -1; private float moveY = -1; // 是否手指在移动 private boolean isMove; // 是否可以触摸,当用户抬起手指,划出九宫格的密码不正确时为不可触摸 private boolean isTouch = true; // 用来存储记录被按下的点 private List pressedPoint = new ArrayList<>(); // 屏幕解锁监听器 private OnScreenLockListener listener; public ScreenLockView(Context context) { super(context); init(); } public ScreenLockView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ScreenLockView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { errorBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_error); normalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_normal); pressedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_pressed); lineErrorBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.line_error); linePressedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.line_pressed); radius = normalBitmap.getWidth() / 2; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (widthSize > heightSize) { offset = (widthSize - heightSize) / 2; } else { offset = (heightSize - widthSize) / 2; } setMeasuredDimension(widthSize, heightSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!init) { width = getWidth(); height = getHeight(); initPoint(); init = true; } drawPoint(canvas); if (moveX != -1 && moveY != -1) { drawLine(canvas); } } // 画直线 private void drawLine(Canvas canvas) { // 将pressedPoint中的所有格子依次遍历,互相连线 for (int i = 0; i  0) { point = isPoint(x, y); if (point != null) { if (!crossPoint(point)) { point.setState(Point.PRESSED_MODE); pressedPoint.add(point); } } moveX = x; moveY = y; isMove = true; } break; case MotionEvent.ACTION_UP: isMove = false; String tempPwd = ""; for (Point p : pressedPoint) { tempPwd += p.getMark(); } if (listener != null) { listener.getStringPassword(tempPwd); } if (tempPwd.equals(password)) { if (listener != null) { listener.isPassword(true); this.postDelayed(runnable, 1000); } } else { for (Point p : pressedPoint) { p.setState(Point.ERROR_MODE); } isTouch = false; this.postDelayed(runnable, 1000); if (listener != null) { listener.isPassword(false); } } break; } invalidate(); } return true; } private boolean crossPoint(Point point) { if (pressedPoint.contains(point)) { return true; } return false; } public interface OnScreenLockListener { public void getStringPassword(String password); public void isPassword(boolean flag); } public void setOnScreenLockListener(OnScreenLockListener listener) { this.listener = listener; } private Point isPoint(float x, float y) { Point point; for(int i = 0; i

3.RotateDegress类

 public class RotateDegrees { public static float getDegrees(Point a, Point b){ float degrees = 0 ; float aX = a.getX(); float aY = a.getY(); float bX = b.getX(); float bY = b.getY(); if(aX == bX){ if(aYbX){ if(aY>bY){ degrees = 180 + (float)(Math.atan2(aY-bY,aX-bX)*180/Math.PI); }else{ degrees = 180 - (float)(Math.atan2(bY -aY,aX - bX)*180/Math.PI); } }else{ if(aY>bY){ degrees = 360 -(float)(Math.atan2(aY - bY,bX-aX)*180/Math.PI); }else{ degrees = (float)(Math.atan2(bY - aY,bX - aX)*180/Math.PI); } } } return degrees; } public static float getDegrees(Point a, float bX,float bY){ Point b = new Point(bX,bY,null); return getDegrees(a,b); } } 

用到的图片资源

4.MainActivity中使用

 public class MainActivity extends AppCompatActivity { private ScreenLockView screenLockView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); screenLockView = (ScreenLockView) findViewById(R.id.slv); screenLockView.setOnScreenLockListener(new ScreenLockView.OnScreenLockListener() { @Override public void getStringPassword(String password) { } @Override public void isPassword(boolean flag) { String content; if (flag) { content = "密码正确"; } else { content = "密码错误"; } Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show(); } }); } } 

5.布局文件

   

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是Android自定义控件实现九宫格解锁功能的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动