android 에서 미끄럼 9 궁 격 켜 기 비밀번호

원본 링크:
구 궁 격 켜 기 비밀번호(소스 코드)
http://www.eoeandroid.com/thread-245395-1-1.html
 
효과 도 는 사이트 가 차지 할 때 업로드 할 수 없 기 때문에 나중에 보충 합 니 다.
 
NinePointLineView.java
package org.demo.custon_view;

import org.demo.utils.MLog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class NinePointLineView extends View {

    Paint linePaint = new Paint();

    Paint whiteLinePaint = new Paint();

    Paint textPaint = new Paint();

    //
    Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.lock);
    int defaultBitmapRadius = defaultBitmap.getWidth() / 2;

    //
    Bitmap selectedBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.indicator_lock_area);
    int selectedBitmapDiameter = selectedBitmap.getWidth();
    int selectedBitmapRadius = selectedBitmapDiameter / 2;

    //    9     
    PointInfo[] points = new PointInfo[9];

    //   ACTION_DOWN    
    PointInfo startPoint = null;

    //      
    int width, height;

    //  ACTION_MOVE    X,Y  
    int moveX, moveY;

    //     ACTION_UP
    boolean isUp = false;

    //           
    StringBuffer lockString = new StringBuffer();

    public NinePointLineView(Context context) {
        super(context);
        this.setBackgroundColor(Color.WHITE);
        initPaint();
    }

    public NinePointLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setBackgroundColor(Color.WHITE);
        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        MLog.i("onMeasure");
        //        
        width = getWidth();
        height = getHeight();
        if (width != 0 && height != 0) {
            initPoints(points);
        }
        MLog.i("width、height = " + width + "、" + height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        MLog.i("onLayout");
        super.onLayout(changed, left, top, right, bottom);
    }

    private int startX = 0, startY = 0;

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawText("       :" + lockString, 0, 40, textPaint);

        if (moveX != 0 && moveY != 0 && startX != 0 && startY != 0) {
            //          
            drawLine(canvas, startX, startY, moveX, moveY);
        }

        drawNinePoint(canvas);

        super.onDraw(canvas);
    }

    //   ,  DOWN MOVE、UP    ,    UP  ,      DOWN;
    //    DOWN ,          ,  MOVE UP     View onTouchEvent  
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        boolean flag = true;

        if (isUp) {//      ,         lockString

            finishDraw();

            //  UP ,   false,        ,      Down  
            flag = false;

        } else {//

            handlingEvent(event);

            //      true,   View     ,      MOVE UP  
            flag = true;

        }
        return flag;
    }

    private void handlingEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            moveX = (int) event.getX();
            moveY = (int) event.getY();
            MLog.i("onMove:" + moveX + "、" + moveY);
            for (PointInfo temp : points) {
                if (temp.isInMyPlace(moveX, moveY) && temp.isNotSelected()) {
                    temp.setSelected(true);
                    startX = temp.getCenterX();
                    startY = temp.getCenterY();
                    int len = lockString.length();
                    if (len != 0) {
                        int preId = lockString.charAt(len - 1) - 48;
                        points[preId].setNextId(temp.getId());
                    }
                    lockString.append(temp.getId());
                    break;
                }
            }

            invalidate(0, height - width, width, height);
            break;

        case MotionEvent.ACTION_DOWN:
            int downX = (int) event.getX();
            int downY = (int) event.getY();
            MLog.i("onDown:" + downX + "、" + downY);
            for (PointInfo temp : points) {
                if (temp.isInMyPlace(downX, downY)) {
                    temp.setSelected(true);
                    startPoint = temp;
                    startX = temp.getCenterX();
                    startY = temp.getCenterY();
                    lockString.append(temp.getId());
                    break;
                }
            }
            invalidate(0, height - width, width, height);
            break;

        case MotionEvent.ACTION_UP:
            MLog.i("onUp");
            startX = startY = moveX = moveY = 0;
            isUp = true;
            invalidate();
            break;
        default:
            MLog.i("      !!");
            break;
        }
    }

    private void finishDraw() {
        for (PointInfo temp : points) {
            temp.setSelected(false);
            temp.setNextId(temp.getId());
        }
        lockString.delete(0, lockString.length());
        isUp = false;
        invalidate();
    }

    private void initPoints(PointInfo[] points) {

        int len = points.length;

        int seletedSpacing = (width - selectedBitmapDiameter * 3) / 4;

        //               
        int seletedX = seletedSpacing;
        int seletedY = height - width + seletedSpacing;

        //             
        int defaultX = seletedX + selectedBitmapRadius - defaultBitmapRadius;
        int defaultY = seletedY + selectedBitmapRadius - defaultBitmapRadius;

        //       
        for (int i = 0; i < len; i++) {
            if (i == 3 || i == 6) {
                seletedX = seletedSpacing;
                seletedY += selectedBitmapDiameter + seletedSpacing;

                defaultX = seletedX + selectedBitmapRadius
                        - defaultBitmapRadius;
                defaultY += selectedBitmapDiameter + seletedSpacing;

            }
            points[i] = new PointInfo(i, defaultX, defaultY, seletedX, seletedY);

            seletedX += selectedBitmapDiameter + seletedSpacing;
            defaultX += selectedBitmapDiameter + seletedSpacing;

        }
    }

    private void initPaint() {
        initLinePaint(linePaint);
        initTextPaint(textPaint);
        initWhiteLinePaint(whiteLinePaint);
    }

    /**
     *        
     * @param paint
     */
    private void initTextPaint(Paint paint) {
        textPaint.setTextSize(30);
        textPaint.setAntiAlias(true);
        textPaint.setTypeface(Typeface.MONOSPACE);
    }

    /**
     *        
     * 
     * @param paint
     */
    private void initLinePaint(Paint paint) {
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(defaultBitmap.getWidth());
        paint.setAntiAlias(true);
        paint.setStrokeCap(Cap.ROUND);
    }

    /**
     *        
     * 
     * @param paint
     */
    private void initWhiteLinePaint(Paint paint) {
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(defaultBitmap.getWidth() - 5);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Cap.ROUND);

    }

    /**
     *         
     * 
     * @param canvas
     */
    private void drawNinePoint(Canvas canvas) {

        if (startPoint != null) {
            drawEachLine(canvas, startPoint);
        }

        //         
        for (PointInfo pointInfo : points) {
            if (pointInfo.isSelected()) {//     
                canvas.drawBitmap(selectedBitmap, pointInfo.getSeletedX(),
                        pointInfo.getSeletedY(), null);
            }
            //    
            canvas.drawBitmap(defaultBitmap, pointInfo.getDefaultX(),
                    pointInfo.getDefaultY(), null);
        }

    }

    /**
     *              
     * 
     * @param canvas
     * @param point
     */
    private void drawEachLine(Canvas canvas, PointInfo point) {
        if (point.hasNextId()) {
            int n = point.getNextId();
            drawLine(canvas, point.getCenterX(), point.getCenterY(),
                    points[n].getCenterX(), points[n].getCenterY());
            //   
            drawEachLine(canvas, points[n]);
        }
    }

    /**
     *      ,        ,         
     * 
     * @param canvas
     * @param startX
     * @param startY
     * @param stopX
     * @param stopY
     */
    private void drawLine(Canvas canvas, float startX, float startY,
            float stopX, float stopY) {
        canvas.drawLine(startX, startY, stopX, stopY, linePaint);
        canvas.drawLine(startX, startY, stopX, stopY, whiteLinePaint);
    }

    /**
     *        
     * 
     * @author zkwlx
     * 
     */
    private class PointInfo {

        //     ID
        private int id;

        //             ID,       ID
        private int nextId;

        //      
        private boolean selected;

        //          X  
        private int defaultX;

        //          Y  
        private int defaultY;

        //           X  
        private int seletedX;

        //           Y  
        private int seletedY;

        public PointInfo(int id, int defaultX, int defaultY, int seletedX,
                int seletedY) {
            this.id = id;
            this.nextId = id;
            this.defaultX = defaultX;
            this.defaultY = defaultY;
            this.seletedX = seletedX;
            this.seletedY = seletedY;
        }

        public boolean isSelected() {
            return selected;
        }

        public boolean isNotSelected() {
            return !isSelected();
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

        public int getId() {
            return id;
        }

        public int getDefaultX() {
            return defaultX;
        }

        public int getDefaultY() {
            return defaultY;
        }

        public int getSeletedX() {
            return seletedX;
        }

        public int getSeletedY() {
            return seletedY;
        }

        public int getCenterX() {
            return seletedX + selectedBitmapRadius;
        }

        public int getCenterY() {
            return seletedY + selectedBitmapRadius;
        }

        public boolean hasNextId() {
            return nextId != id;
        }

        public int getNextId() {
            return nextId;
        }

        public void setNextId(int nextId) {
            this.nextId = nextId;
        }

        /**
         *   (x,y)          
         * 
         * @param x
         * @param y
         * @return
         */
        public boolean isInMyPlace(int x, int y) {
            boolean inX = x > seletedX
                    && x < (seletedX + selectedBitmapDiameter);
            boolean inY = y > seletedY
                    && y < (seletedY + selectedBitmapDiameter);

            return (inX && inY);
        }

    }

}

 
NinePointView.java
package org.demo.custon_view;

import org.demo.utils.MLog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class NinePointView extends View {

    Paint linePaint = new Paint();

    Paint textPaint = new Paint();

    Path path = new Path();

    //
    Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.lock);
    int defaultBitmapRadius = defaultBitmap.getWidth() / 2;

    //
    Bitmap selectedBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.indicator_lock_area);
    int selectedBitmapDiameter = selectedBitmap.getWidth();
    int selectedBitmapRadius = selectedBitmapDiameter / 2;

    //          
    Bitmap indicateBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.indicator_lock_area_next);
    Bitmap tempBitmap = null;

    //    9     
    PointInfo[] points = new PointInfo[9];

    //      
    int width, height;

    //  ACTION_MOVE    X,Y  
    int moveX, moveY;

    //     ACTION_UP
    boolean isUp = false;

    //           
    StringBuffer lockString = new StringBuffer();

    Matrix matrix = new Matrix();

    public NinePointView(Context context) {
        super(context);
        this.setBackgroundColor(Color.WHITE);
        initLinePaint(linePaint);
        initTextPaint(textPaint);
    }

    public NinePointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setBackgroundColor(Color.WHITE);
        initLinePaint(linePaint);
        initTextPaint(textPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        MLog.i("onMeasure");
        width = getWidth();
        height = getHeight();
        if (width != 0 && height != 0) {
            initPoints(points);
        }
        MLog.i("width、height = " + width + "、" + height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        MLog.i("onLayout");
        super.onLayout(changed, left, top, right, bottom);
    }

    private int startX = 0, startY = 0;

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawText("       :" + lockString, 0, 40, textPaint);

        if (moveX != 0 && moveY != 0 && startX != 0 && startY != 0) {
            //          
            canvas.drawLine(startX, startY, moveX, moveY, linePaint);
        }

        drawNinePoint(canvas, linePaint);

        super.onDraw(canvas);
    }

    //   ,  DOWN MOVE、UP    ,    UP  ,      DOWN;
    //    DOWN ,          ,  MOVE UP     VIEW onTouchEvent  
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        boolean flag = true;

        if (isUp) {//      ,    Canvas  

            finishDraw();

            //  UP ,   false,        ,      Down  
            flag = false;

        } else {//

            handlingEvent(event);

            //      true,     View      ,      ,      MOVE UP  
            flag = true;

        }
        return flag;
    }

    private void handlingEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            moveX = (int) event.getX();
            moveY = (int) event.getY();
            MLog.i("onMove:" + moveX + "、" + moveY);
            for (PointInfo temp : points) {
                if (temp.isInMyPlace(moveX, moveY) && temp.isNotSelected()) {
                    temp.setSelected(true);
                    startX = temp.getCenterX();
                    startY = temp.getCenterY();
                    int len = lockString.length();
                    if (len != 0) {
                        int preId = lockString.charAt(len - 1) - 48;
                        points[preId].setNextId(temp.getId());
                    }
                    lockString.append(temp.getId());
                    break;
                }
            }

            invalidate(0, height - width, width, height);
            break;

        case MotionEvent.ACTION_DOWN:
            int downX = (int) event.getX();
            int downY = (int) event.getY();
            MLog.i("onDown:" + downX + "、" + downY);
            for (PointInfo temp : points) {
                if (temp.isInMyPlace(downX, downY)) {
                    temp.setSelected(true);
                    startX = temp.getCenterX();
                    startY = temp.getCenterY();
                    lockString.append(temp.getId());
                    break;
                }
            }
            invalidate(0, height - width, width, height);
            break;

        case MotionEvent.ACTION_UP:
            MLog.i("onUp");
            startX = startY = moveX = moveY = 0;
            isUp = true;
            invalidate();
            break;
        default:
            MLog.i("      !!");
            break;
        }
    }

    private void finishDraw() {
        for (PointInfo temp : points) {
            temp.setSelected(false);
            temp.setNextId(temp.getId());
        }
        lockString.delete(0, lockString.length());
        isUp = false;
        invalidate();
    }

    private void initPoints(PointInfo[] points) {

        int len = points.length;

        int seletedSpacing = (width - selectedBitmapDiameter * 3) / 4;

        //               
        int seletedX = seletedSpacing;
        int seletedY = height - width + seletedSpacing;

        //             
        int defaultX = seletedX + selectedBitmapRadius - defaultBitmapRadius;
        int defaultY = seletedY + selectedBitmapRadius - defaultBitmapRadius;

        for (int i = 0; i < len; i++) {
            if (i == 3 || i == 6) {
                seletedX = seletedSpacing;
                seletedY += selectedBitmapDiameter + seletedSpacing;

                defaultX = seletedX + selectedBitmapRadius
                        - defaultBitmapRadius;
                defaultY += selectedBitmapDiameter + seletedSpacing;

            }
            points[i] = new PointInfo(i, defaultX, defaultY, seletedX, seletedY);

            seletedX += selectedBitmapDiameter + seletedSpacing;
            defaultX += selectedBitmapDiameter + seletedSpacing;

        }
    }

    private void initTextPaint(Paint paint) {
        textPaint.setTextSize(30);
        textPaint.setAntiAlias(true);
        textPaint.setTypeface(Typeface.MONOSPACE);
    }

    /**
     *       
     * 
     * @param paint
     */
    private void initLinePaint(Paint paint) {
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(defaultBitmap.getWidth());
        paint.setAntiAlias(true);
        paint.setStrokeCap(Cap.ROUND);
    }

    /**
     *         
     * 
     * @param canvas
     */
    private void drawNinePoint(Canvas canvas, Paint paint) {

        //            
        for (PointInfo pointInfo : points) {
            if (pointInfo.hasNextId()) {
                int n = pointInfo.getNextId();
                canvas.drawLine(pointInfo.getCenterX(), pointInfo.getCenterY(),
                        points[n].getCenterX(), points[n].getCenterY(), paint);
            }
        }

        //         
        for (PointInfo pointInfo : points) {
            if (pointInfo.isSelected()) {
                if (pointInfo.hasNextId()) {
                    matrix.reset();
                    int i = (int) Math.abs(Math.random() * 1000 - 640);
                    MLog.i("      :" + i);
                    matrix.setRotate(i);
                    tempBitmap = Bitmap.createBitmap(indicateBitmap, 0, 0,
                            indicateBitmap.getWidth(),
                            indicateBitmap.getHeight(), matrix, false);
                    canvas.drawBitmap(tempBitmap, pointInfo.getSeletedX(),
                            pointInfo.getSeletedY(), paint);
                } else {
                    canvas.drawBitmap(selectedBitmap, pointInfo.getSeletedX(),
                            pointInfo.getSeletedY(), paint);
                }
            }
            canvas.drawBitmap(defaultBitmap, pointInfo.getDefaultX(),
                    pointInfo.getDefaultY(), paint);
        }

    }

    private class PointInfo {

        //     ID
        private int id;

        //             ID,       ID
        private int nextId;

        //      
        private boolean selected;

        //          X  
        private int defaultX;

        //          Y  
        private int defaultY;

        //           X  
        private int seletedX;

        //           Y  
        private int seletedY;

        public PointInfo(int id, int defaultX, int defaultY, int seletedX,
                int seletedY) {
            this.id = id;
            this.nextId = id;
            this.defaultX = defaultX;
            this.defaultY = defaultY;
            this.seletedX = seletedX;
            this.seletedY = seletedY;
        }

        public boolean isSelected() {
            return selected;
        }

        public boolean isNotSelected() {
            return !isSelected();
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

        public int getId() {
            return id;
        }

        public int getDefaultX() {
            return defaultX;
        }

        public int getDefaultY() {
            return defaultY;
        }

        public int getSeletedX() {
            return seletedX;
        }

        public int getSeletedY() {
            return seletedY;
        }

        public int getCenterX() {
            return seletedX + selectedBitmapRadius;
        }

        public int getCenterY() {
            return seletedY + selectedBitmapRadius;
        }

        public boolean hasNextId() {
            return nextId != id;
        }

        public int getNextId() {
            return nextId;
        }

        public void setNextId(int nextId) {
            this.nextId = nextId;
        }

        /**
         *   (x,y)          
         * 
         * @param x
         * @param y
         * @return
         */
        public boolean isInMyPlace(int x, int y) {
            boolean inX = x > seletedX
                    && x < (seletedX + selectedBitmapDiameter);
            boolean inY = y > seletedY
                    && y < (seletedY + selectedBitmapDiameter);

            if (inX && inY) {
                return true;
            } else {
                return false;
            }
        }

    }

}

 

좋은 웹페이지 즐겨찾기