android studio 触摸锁,如何在AndroidStudio中使用手势解锁

目录

思路

通常来讲,进行手势解锁的设置有两种思路,一种是使用boolean类型的数组来识别手势,一种是使用普通的3*3的int型数组来识别。我所讲的就是第二种,说白了就是把普通的密码锁换一种方式输入,好比在屏幕上你划了一个Z来解锁,而用密码锁就是7895123。话很少说,进入正题web

设置所须要的变量与常量 ;

设置所须要的设置与构造函数 ;

测量宽和高,计算绘制的间距 ;

绘制3*3小圆,保存绘制小圆的坐标 ;

绘制手势线条;

处理事件onTouchEvent ;

设置所须要的变量与常量

private int mWidth;//宽

private int mHeight;//高

private int circle_radius = 30;//小圆半径

private int cirle_radius_alw = 60;//容差

private int[][] unLockData = new int[3][3];//记录手势所表明的密码

private int horizontal_spac_circle;//水平间距

private int vertical_spac_circle;//垂直间距

private Map coordinateMap;//全部小圆点的坐标

private PointPacking startPoint;//画笔开始

private List points = new ArrayList<>();

//坐标

private int moveX = -1;

private int moveY = -1;

private int drawX = -1;

private int drawY = -1;

/**

* 显示手势轨迹

*/

private boolean flag_show_gesture = true;

private int paint_circle_width = 3;//圆跟圆之间的长度

//用来绘制各种圆的样子

private Paint drawPointBgPaint = new Paint();//初始圆

private Paint drawPointSelectPoint = new Paint();//已选择的圆

private Paint drawLinePaint = new Paint();//链接圆的线

private OnUnLockListener listener;//监听类

设置所须要的设置与构造函数

//设置监听类

public void setListener(OnUnLockListener listener) {

this.listener = listener;

}

/**

* 设置是否显示手势轨迹

*/

public void setFlag_show_gesture(boolean flag_show_gesture) {

this.flag_show_gesture = flag_show_gesture;

}

//设置构造函数

public GestureUnLockView(Context context) {

this(context, null);

}

public GestureUnLockView(Context context, @Nullable AttributeSet attrs) {

this(context, attrs, 0);

}

public GestureUnLockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

setLayerType(LAYER_TYPE_SOFTWARE, null);

initGestureUnLockView();

initPaint();

}

//绘制各种圆的样子

private void initPaint() {

drawPointBgPaint.setAntiAlias(true);

drawPointBgPaint.setColor(Color.parseColor("#a9dff9"));

drawPointSelectPoint.setAntiAlias(true);

drawPointSelectPoint.setColor(Color.parseColor("#08a3ee"));

drawLinePaint.setAntiAlias(true);

drawLinePaint.setColor(Color.parseColor("#08a3ee"));

drawLinePaint.setStyle(Paint.Style.FILL);

drawLinePaint.setStrokeWidth(5);

}

//初始化全部小圆点的坐标

private void initGestureUnLockView() {

coordinateMap = new HashMap<>();

}

测量宽和高,计算绘制的间距

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

mWidth = widthSize;//控件宽

mHeight = heightSize;//控件高

horizontal_spac_circle = (mWidth - unLockData[0].length * circle_radius * 2

- getPaddingLeft() - getPaddingRight() - cirle_radius_alw * 2 - paint_circle_width)

/ (unLockData[0].length - 1);//计算间距宽

vertical_spac_circle = (mHeight - unLockData.length * circle_radius * 2

- getPaddingTop() - getPaddingBottom() - cirle_radius_alw * 2 - paint_circle_width)

/ (unLockData.length - 1);//计算间距高

}

绘制3*3小圆,保存绘制小圆的坐标

/*

* 绘制小圆点

*

*/

private void drawCircle(Canvas canvas) {

coordinateMap.clear();

int count = 1;

for (int i = 0; i < unLockData.length; i++) {

for (int j = 0; j < unLockData[i].length; j++) {

int cx = horizontal_spac_circle * j + circle_radius + (circle_radius * 2 * j) + getPaddingLeft() + cirle_radius_alw + paint_circle_width;

int cy = vertical_spac_circle * i + circle_radius + (circle_radius * 2 * i) + getPaddingTop() + cirle_radius_alw + paint_circle_width;

canvas.drawCircle(cx, cy, circle_radius, drawPointBgPaint);

for (PointPacking point : points) {

if (cx == point.x && cy == point.y) {

drawPointSelectPoint.setStyle(Paint.Style.FILL);

drawPointSelectPoint.setShadowLayer(50, 0, 0, Color.parseColor("#aa08a3ee"));

canvas.drawCircle(cx, cy, circle_radius, drawPointSelectPoint);

drawPointSelectPoint.clearShadowLayer();

drawPointSelectPoint.setStyle(Paint.Style.STROKE);

drawPointSelectPoint.setStrokeWidth(paint_circle_width);

canvas.drawCircle(cx, cy, circle_radius + cirle_radius_alw, drawPointSelectPoint);

}

}

String key = String.valueOf(count);

Point point = new Point(cx, cy);

coordinateMap.put(key, point);

count++;

}

}

}

绘制手势线条

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

drawCircle(canvas);

if (flag_show_gesture) {

for (int i = 0; i < points.size(); i++) {

if (i == 0) {

drawX = points.get(0).x;

drawY = points.get(0).y;

} else {

canvas.drawLine(drawX, drawY, points.get(i).x, points.get(i).y, drawLinePaint);

drawX = points.get(i).x;

drawY = points.get(i).y;

}

}

if (moveX != -1 && moveY != -1 && points.size() > 0) {

canvas.drawLine(drawX, drawY, moveX, moveY, drawLinePaint);

}

}

}

处理事件onTouchEvent

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

startPoint = judgmentPosition(event.getX(), event.getY());

if (startPoint != null) {

points.add(startPoint);

}

break;

case MotionEvent.ACTION_MOVE:

moveX = (int) event.getX();

moveY = (int) event.getY();

PointPacking point = judgmentPosition(moveX, moveY);

boolean flag = false;

if (point != null) {

if (startPoint == null) {

startPoint = point;

}

if (points.size() > 0) {

for (PointPacking p : points) {

if (point.x == p.x && point.y == p.y) {

flag = true;

break;

}

}

if (!flag) {

points.add(point);

}

}

}

postInvalidate();

break;

case MotionEvent.ACTION_UP:

moveX = -1;

moveY = -1;

postDelayed(new Runnable() {

@Override

public void run() {

if (listener != null && points.size() > 0) {

StringBuilder sb = new StringBuilder();

for (PointPacking p : points) {

sb.append(p.position);

}

listener.unLock(sb.toString());

}

startPoint = null;

points.clear();

postInvalidate();

}

}, 300);

postInvalidate();

break;

default:

break;

}

return true;

}

效果图

71b3f0a5c92ad7537d8aeb66f69c4f12.png

做者:张江霖

原文连接canvas

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值