写点什么

如何在 AndroidStudio 中使用手势解锁

  • 2021 年 11 月 07 日
  • 本文字数:2269 字

    阅读完需:约 7 分钟

//设置监听类


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 (poin


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


t != 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;

评论

发布
暂无评论
如何在AndroidStudio中使用手势解锁