iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现九宫格图案解锁
  • 367
分享到

Android实现九宫格图案解锁

2024-04-02 19:04:59 367人浏览 安东尼
摘要

本文实例为大家分享了Android实现九宫格图案解锁的具体代码,供大家参考,具体内容如下 前言:自定义了一个九宫格的VIew来绘制九宫格图案,实现了绘制图案解锁的功能。 效果图如下:

本文实例为大家分享了Android实现九宫格图案解的具体代码,供大家参考,具体内容如下

前言:自定义了一个九宫格的VIew来绘制九宫格图案,实现了绘制图案解锁的功能。

效果图如下:

1. 第一步

自定义九宫格View。

public class LockPatterView extends View {

    private static final int POINT_SIZE = 5;
    private Point[][] points = new Point[3][3];
    private boolean isInit,isSelect,isFinish,movePoint;
    private Matrix matrix = new Matrix();
    private float width,height,offstartX,offstartY,moveX,moveY;;
    private Bitmap bitmap_pressed,bitmap_nORMal,bitmap_error,bitmap_line,bitmap_line_error;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private List<Point> pointList = new ArrayList<Point>();
    private OnPatterChangeLister onPatterChangeLister;
    public LockPatterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LockPatterView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LockPatterView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(canvas canvas) {
        if (!isInit) {
            initPoints();
        }
        points2Canvas(canvas);

        if (pointList.size() > 0) {
            Point a = pointList.get(0);
            for (int i = 0; i < pointList.size(); i++) {
                Point b = pointList.get(i);
                line2Canvas(canvas, a, b);
                a = b;
            }
            if (movePoint) {
                line2Canvas(canvas, a, new Point(moveX, moveY));
            }
        }
    }

    
    private void points2Canvas(Canvas canvas) {
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points[i].length; j++) {
                Point point = points[i][j];
                if (point.state == Point.STATE_PRESSED) {
                    canvas.drawBitmap(bitmap_pressed, point.x - bitmap_normal.getWidth()/2, point.y - bitmap_normal.getHeight() / 2, paint);
                }else if (point.state == Point.STATE_ERROR) {
                    canvas.drawBitmap(bitmap_error, point.x - bitmap_normal.getWidth()/2, point.y - bitmap_normal.getHeight() / 2, paint);
                }else{
                    canvas.drawBitmap(bitmap_normal, point.x - bitmap_normal.getWidth()/2, point.y - bitmap_normal.getHeight() / 2, paint);
                }
            }
        }
    }


    
    public void line2Canvas(Canvas canvas,Point a,Point b){
        float linelength = (float) Point.distance(a, b);
        float degress = getDegrees(a,b);
        canvas.rotate(degress, a.x, a.y);

        if (a.state == Point.STATE_PRESSED) {
            matrix.setScale(linelength / bitmap_line.getWidth(), 1);
            matrix.postTranslate(a.x, a.y);
            canvas.drawBitmap(bitmap_line, matrix, paint);
        }else{
            matrix.setScale(linelength / bitmap_line.getWidth(), 1);
            matrix.postTranslate(a.x, a.y);
            canvas.drawBitmap(bitmap_line_error, matrix, paint);
        }
        canvas.rotate(-degress, a.x, a.y);
    }

    public float getDegrees(Point pointA, Point pointB) {
        return (float) Math.toDegrees(Math.atan2(pointB.y - pointA.y, pointB.x - pointA.x));
    }

    
    private void initPoints() {

        width = getWidth();
        height = getHeight();

        if (width > height) {
            offstartX = (width - height) / 2;
            width = height;
        }else{
            offstartY = (height - width) / 2;
            height = width;
        }

        bitmap_normal = BitmapFactory.decodeResource(getResources(), R.mipmap.normal);
        bitmap_pressed = BitmapFactory.decodeResource(getResources(), R.mipmap.press);
        bitmap_error = BitmapFactory.decodeResource(getResources(), R.mipmap.error);
        bitmap_line = BitmapFactory.decodeResource(getResources(), R.mipmap.line_normal);
        bitmap_line_error = BitmapFactory.decodeResource(getResources(), R.mipmap.line_error);

        points[0][0] = new Point(offstartX + width / 4,offstartY + height / 4);
        points[0][1] = new Point(offstartX + width / 2,offstartY + height / 4);
        points[0][2] = new Point(offstartX + width - width / 4,offstartY + height / 4);

        points[1][0] = new Point(offstartX + width / 4,offstartY + width / 2);
        points[1][1] = new Point(offstartX + width / 2,offstartY + width / 2);
        points[1][2] = new Point(offstartX + width - width / 4,offstartY + width / 2);

        points[2][0] = new Point(offstartX + width / 4,offstartY + width - width / 4);
        points[2][1] = new Point(offstartX + width / 2,offstartY + width - width / 4);
        points[2][2] = new Point(offstartX + width - width / 4,offstartY + width - width / 4);

        isInit = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        moveX = event.getX();
        moveY = event.getY();
        movePoint = false;
        isFinish = false;

        Point point = null;

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (onPatterChangeLister != null) {
                onPatterChangeLister.onPatterStart(true);
            }
            resetPoint();

            point = chechSelectPoint();
            if (point != null) {
                isSelect = true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (isSelect) {
                point = chechSelectPoint();
                if (point == null) {
                    movePoint = true;
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            isFinish = true;
            isSelect = false;
            break;

        }
        if (!isFinish && isSelect && point != null) {
            if (crossPoint(point)) {
                movePoint = true;
            }else{
                point.state = Point.STATE_PRESSED;
                pointList.add(point);
            }
        }

        if (isFinish) {
            if (pointList.size() == 1) {
                errorPoint();
            }else if(pointList.size() < POINT_SIZE && pointList.size() > 0 ){
                 errorPoint();
                 if (onPatterChangeLister != null) {
                        onPatterChangeLister.onPatterChange(null);
                    }
            }else{
                if (onPatterChangeLister != null) {
                    String pass = "";
                    for (int i = 0; i < pointList.size(); i++) {
                        pass = pass + pointList.get(i).index;
                    }
                    if (!TextUtils.isEmpty(pass)) {
                        onPatterChangeLister.onPatterChange(pass);
                    }
                }
            }
        }

        postInvalidate();
        return true;
    }

    
    private boolean crossPoint(Point point){
        if (pointList.contains(point)) {
            return true;
        }else{
            return false;
        }
    }

    
    public void resetPoint(){
        for (int i = 0; i < pointList.size(); i++) {
            Point point = pointList.get(i);
            point.state = Point.STATE_NORMAL;
        }
        pointList.clear();
    }

    
    public void errorPoint(){
        for (Point point : pointList) {
            point.state = Point.STATE_ERROR;
        }
    }

    
    private Point chechSelectPoint(){
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points[i].length; j++) {
                Point point = points[i][j];
                if (Point.with(point.x, point.y, bitmap_normal.getWidth() / 2, moveX, moveY)) {
                    return point;
                }
            }
        }

        return null;
    }

    public static class Point{
        public static int STATE_NORMAL = 0;
        public static int STATE_PRESSED = 1;
        public static int STATE_ERROR = 2;
        public float x,y;
        public int index = 0,state = 0;
        public Point(){};

        public Point(float x,float y){
            this.x = x;
            this.y = y;
        }

        
        public static double distance(Point a,Point b){
            return Math.sqrt(Math.abs(a.x - b.x) * Math.abs(a.x - b.x) + Math.abs(a.y - b.y) * Math.abs(a.y - b.y)) ;
        }

        
        public static boolean with(float paintX,float pointY,float r,float moveX,float moveY){
            return Math.sqrt((paintX - moveX) * (paintX - moveX) + (pointY - moveY) * (pointY - moveY)) < r ;
        }
    }

}

2. 第二步

编写布局文件activity_main.xml,在主布局文件中引入自定义的View。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="Http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.newdegree.mylock.LockPatterView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

最后,运行程序就能看到九宫格图案了。

总结:这样我们就完成了九宫格图案解锁的开发,我们可以在自己完成的程序中绘制各种解锁图案。

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

--结束END--

本文标题: Android实现九宫格图案解锁

本文链接: https://www.lsjlt.com/news/152853.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android实现九宫格图案解锁
    本文实例为大家分享了Android实现九宫格图案解锁的具体代码,供大家参考,具体内容如下 前言:自定义了一个九宫格的VIew来绘制九宫格图案,实现了绘制图案解锁的功能。 效果图如下:...
    99+
    2024-04-02
  • Android怎么实现九宫格图案解锁
    今天小编给大家分享一下Android怎么实现九宫格图案解锁的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。效果图如下: 第一步...
    99+
    2023-07-02
  • Android怎么实现九宫格解锁
    这篇文章主要介绍Android怎么实现九宫格解锁,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先理清一下逻辑,我们要做NxN的九宫格 下图是3x3的简单图例// -(--)-(--)-(--)-// -(--)-(...
    99+
    2023-05-30
    android
  • Android实现图片九宫格
    本文实例为大家分享了Android实现图片九宫格的具体代码,供大家参考,具体内容如下 九宫格分三类 实现的效果 具体实现 activity_main <xml v...
    99+
    2024-04-02
  • Android自定义控件实现九宫格解锁
    关于九宫格解锁,我看了不少博客,但是都感觉很复杂,可能我的功夫还不到,所以很多东西我不了解,但是我还是打算写一个自己的九宫格。我相信我的九宫格大家都能很快的理解,当然如果需要实现更复...
    99+
    2024-04-02
  • Android自定义View实现九宫格图形解锁(Kotlin版)
    本文实例为大家分享了Android自定义View实现九宫格图形解锁的具体代码,供大家参考,具体内容如下 效果: 代码: package com.example.kotlin_t...
    99+
    2024-04-02
  • Android自制九宫格解锁控件
    本文实例为大家分享了Android自制九宫格解锁控件的具体代码,供大家参考,具体内容如下 前两天从网上学习了下如何自定义一个九宫格解锁的控件,于是自己根据逻辑写了一遍,自定义控件的代...
    99+
    2024-04-02
  • Android实现九宫格抽奖
    本文实例为大家分享了Android实现九宫格抽奖的具体代码,供大家参考,具体内容如下 package cq.cake.luckdraw; import android.graph...
    99+
    2024-04-02
  • Android自定义控件实现九宫格解锁功能
    最终Android九宫格解锁效果如下进行定义实体point点public class Point { private float x; private float y; //正常模式 public static final int NORM...
    99+
    2023-05-31
    android 九宫格 解锁
  • Android自定义view实现滑动解锁九宫格控件
    目录前言需求效果图前言 上一篇文章用贝塞尔曲线画了一个看起来不错的小红点功能,技术上没什么难度,主要就是数学上的计算。这篇文章也差不多,模仿了一个常用的滑动解锁的九宫格控件。 需求 ...
    99+
    2023-02-09
    Android滑动解锁九宫格 Android滑动解锁 Android九宫格控件
  • Android怎么绘制九宫格解锁控件
    这篇“Android怎么绘制九宫格解锁控件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android怎么绘制九宫格解锁控件...
    99+
    2023-06-29
  • android九宫格锁屏控件使用详解
    本文实例为大家分享了android九宫格锁屏控件的具体代码,供大家参考,具体内容如下 代码: public class LockView extends View {     //...
    99+
    2024-04-02
  • Android RecyclerView实现九宫格效果
    RecyclerView更加优化的复用机制和方便实现UI效果,几乎替代Listview和GridView的使用。但是分割线的实现,需要自己继承ItemDecoration来绘制。 效...
    99+
    2024-04-02
  • Appium自动化测试实现九宫格解锁
    目录背景高级手势轻敲(tap)短按 (press)长按 (long_press)等待 (wait)移动到 (moveTo)释放 (release)执行 (perform)九宫格解锁背...
    99+
    2024-04-02
  • Android实现九宫格手势密码
    本文实例为大家分享了Android实现九宫格手势密码的具体代码,供大家参考,具体内容如下 介绍下自己编写的九宫格手势密码。先见图 思路:首先是9个格子,接着是格子连线;那么我们的步...
    99+
    2024-04-02
  • Android 实现九宫格抽奖功能
    目录效果展示实现步骤 1.生成抽奖矩形:2.添加奖品图片:3.实现抽奖动画:4.实现动态设置参数:5.添加抽奖结果回调效果展示 实现步骤 1.生成抽奖矩形: 其中每个矩形的宽高相...
    99+
    2024-04-02
  • 怎么用CSS实现九宫格图
    本篇内容介绍了“怎么用CSS实现九宫格图”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!下图是手Q吃喝玩乐&...
    99+
    2024-04-02
  • java实现九宫格拼图游戏
    本文实例为大家分享了java实现九宫格拼图游戏的具体代码,供大家参考,具体内容如下 设计步骤:  先将框架构思出来,首先将拼图游戏的雏形实现,即一个界面,九个按钮,按钮上的...
    99+
    2024-04-02
  • JS实现九宫格拼图游戏
    本文实例为大家分享了JS实现九宫格拼图游戏的具体代码,供大家参考,具体内容如下 <!doctype html> <html> <head>  &l...
    99+
    2024-04-02
  • 怎么在Android 应用中实现一个九宫格手势锁
    怎么在Android 应用中实现一个九宫格手势锁?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。主要的方法是重写View.onTouchEvent( Motion...
    99+
    2023-05-31
    android roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作