Android实现九宫格图案解锁

2022-10-07,,,

本文实例为大家分享了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));
            }
        }
    }

    /**
     * @param canvas
     */
    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);
                }
            }
        }
    }


    /**
     *
     * @param canvas
     * @param
     * @param
     */
    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;
    }

    /**
     * @param point
     * @return
     */
    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;
        }
    }

    /**
     * @return
     */
    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;
        }

        /**
         * @param a
         * @param b
         * @return
         */
        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)) ;
        }

        /**
         * 
         * @param paintx
         * @param pointy
         * @param r
         * @param movex
         * @param movey
         * @return
         */
        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>

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

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

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

《Android实现九宫格图案解锁.doc》

下载本文的Word格式文档,以方便收藏与打印。