============================================================
一般来说,进行手势解锁的设置有两种思路,一种是使用boolean类型的数组来识别手势,一种是使用普通的3*3的int型数组来识别。我所讲的就是第二种,说白了就是把普通的密码锁换一种方式输入,比如在屏幕上你划了一个Z来解锁,而用密码锁就是7895123。话不多说,进入正题
-
设置所需要的变量与常量 ;
-
设置所需要的设置与构造函数 ;
-
测量宽和高,计算绘制的间距 ;
-
绘制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<String, Point> 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);//计算间距高
}
/*
-
绘制小圆点
*/
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);
}
}
学习宝典
对我们开发者来说,一定要打好基础,随时准备战斗。不论寒冬是否到来,都要把自己的技术做精做深。虽然目前移动端的招聘量确实变少了,但中高端的职位还是很多的,这说明行业只是变得成熟规范起来了。竞争越激烈,产品质量与留存就变得更加重要,我们进入了技术赋能业务的时代。
不论遇到什么困难,都不应该成为我们放弃的理由!
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我针对Android程序员,我这边给大家整理了一套学习宝典!包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
【Android核心高级技术PDF文档,BAT大厂面试真题解析】
【算法合集】
【延伸Android必备知识点】
【Android部分高级架构视频学习资源】
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
d进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
【Android核心高级技术PDF文档,BAT大厂面试真题解析】
[外链图片转存中…(img-ycf17t4C-1715246135301)]
【算法合集】
[外链图片转存中…(img-JkKlj1AA-1715246135302)]
【延伸Android必备知识点】
[外链图片转存中…(img-sLjerPZG-1715246135303)]
【Android部分高级架构视频学习资源】
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!