安卓图形解锁
1 package com.hrk.notepad;
2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.content.Context; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.util.AttributeSet; 11 import android.view.MotionEvent; 12 import android.view.View; 13 14 public class DrawPassword extends View 15 { 16 private boolean[] visited = null; 17 private List<Integer> list = null; 18 private float[][][] points = null; 19 private float radius; 20 private float curx, cury; 21 private Paint unvisitedPaint = null; 22 private Paint visitedPaint = null; 23 private Paint circlePaint = null; 24 private Paint linePaint = null; 25 private onFinishListener listener = null; 26 27 public DrawPassword(Context context, AttributeSet attrs, int defStyleAttr) 28 { 29 super(context, attrs, defStyleAttr); 30 init(); 31 } 32 33 public DrawPassword(Context context, AttributeSet attrs) 34 { 35 super(context, attrs); 36 init(); 37 } 38 39 public DrawPassword(Context context) 40 { 41 super(context); 42 init(); 43 } 44 45 private void init() 46 { 47 visited = new boolean[9]; 48 points = new float[3][3][2]; 49 list = new ArrayList<Integer>(); 50 unvisitedPaint = new Paint(); 51 unvisitedPaint.setColor(Color.BLUE); 52 unvisitedPaint.setAntiAlias(true); 53 54 visitedPaint = new Paint(); 55 visitedPaint.setColor(Color.YELLOW); 56 visitedPaint.setAntiAlias(true); 57 58 circlePaint = new Paint(); 59 circlePaint.setColor(Color.WHITE); 60 circlePaint.setAntiAlias(true); 61 62 linePaint = new Paint(); 63 linePaint.setColor(Color.YELLOW); 64 linePaint.setAntiAlias(true); 65 66 } 67 68 protected void onSizeChanged(int w, int h, int oldw, int oldh) 69 { 70 // TODO Auto-generated method stub 71 super.onSizeChanged(w, h, oldw, oldh); 72 setMeasuredDimension(w, h); 73 float wdis = w / 4, hdis = h / 4; 74 radius = Math.min(wdis, hdis) / 2.3f; 75 linePaint.setStrokeWidth(radius/5); 76 for (int i = 0; i < 3; i++) 77 { 78 for (int j = 0; j < 3; j++) 79 { 80 points[i][j][0] = wdis * (j + 1); 81 points[i][j][1] = hdis * (i + 1); 82 } 83 } 84 } 85 86 @Override 87 protected void onDraw(Canvas canvas) 88 { 89 // TODO Auto-generated method stub 90 super.onDraw(canvas); 91 for (int i = 0; i < 3; i++) 92 { 93 for (int j = 0; j < 3; j++) 94 { 95 canvas.drawCircle(points[i][j][0], points[i][j][1], radius, circlePaint); 96 if(visited[i*3+j]) 97 { 98 canvas.drawCircle(points[i][j][0], points[i][j][1], radius / 4, visitedPaint); 99 } 100 else 101 { 102 canvas.drawCircle(points[i][j][0], points[i][j][1], radius / 4, unvisitedPaint); 103 } 104 } 105 } 106 if (list.size() > 0) 107 { 108 int i; 109 for(i=0;i<list.size()-1;i++) 110 { 111 canvas.drawLine(points[list.get(i)/3][list.get(i)%3][0], 112 points[list.get(i)/3][list.get(i)%3][1], 113 points[list.get(i+1)/3][list.get(i+1)%3][0], 114 points[list.get(i+1)/3][list.get(i+1)%3][1], 115 linePaint); 116 } 117 if(curx>=0&&cury>=0) 118 canvas.drawLine(points[list.get(i)/3][list.get(i)%3][0], 119 points[list.get(i)/3][list.get(i)%3][1], 120 curx, 121 cury, 122 linePaint); 123 } 124 } 125 126 @Override 127 public boolean onTouchEvent(MotionEvent event) 128 { 129 // TODO Auto-generated method stub 130 switch (event.getActionMasked()) 131 { 132 case MotionEvent.ACTION_DOWN: 133 curx = event.getX(); 134 cury = event.getY(); 135 break; 136 137 case MotionEvent.ACTION_UP: 138 139 if(list.size()>0) 140 { 141 curx=-1; 142 cury=-1; 143 if(listener!=null) 144 listener.onFinished(list.toString()); 145 } 146 break; 147 148 case MotionEvent.ACTION_MOVE: 149 150 for (int i = 0; i < 3; i++) 151 { 152 for (int j = 0; j < 3; j++) 153 { 154 if(Math.sqrt((event.getX() - points[i][j][0]) * (event.getX() - points[i][j][0]) + (event.getY() - points[i][j][1]) * (event.getY() - points[i][j][1]))<radius&&visited[i*3+j]==false) 155 { 156 visited[i*3+j]=true; 157 list.add(i*3+j); 158 } 159 } 160 } 161 curx = event.getX(); 162 cury = event.getY(); 163 break; 164 165 default: 166 break; 167 } 168 postInvalidate(); 169 return true; 170 } 171 172 public void setOnFinishListener(onFinishListener listener) 173 { 174 this.listener = listener; 175 } 176 177 public void reset() 178 { 179 list.clear(); 180 postInvalidate(); 181 } 182 183 public String getPassword() 184 { 185 if(list.size()>0) 186 { 187 return list.toString(); 188 } 189 else 190 { 191 return ""; 192 } 193 } 194 195 public interface onFinishListener 196 { 197 void onFinished(String password); 198 } 199 }
自己写的安卓图形解锁代码,转载请注明出处:http://www.cnblogs.com/hrkblogs/