Android 사용자 정의 UI 제스처 비밀번호 끝 판

전에 3 편 을 쓴 적 이 있 습 니 다제스처 암호demo 는 실제 기업 프로젝트 에 통합 되 지 않 았 습 니 다.요 며칠 동안 제스처 암호 프로젝트 를 받 았 습 니 다.어제 마침 끝 냈 습 니 다.오늘 시간 을 내 서 정리 한 결과 아직 완선 되 지 않 았 습 니 다.변경 해 야 할 부분 이 있 지만 기본 적 인 절 차 는 모두 달 릴 수 있 습 니 다.
원본 다운로드 주소:http://xiazai.jb51.net/201610/yuanma/AndroidGestureLock(jb51.net).rar

먼저 메 인 인터페이스의 입 구 를 보 세 요.안에 button 이 2 개 있 습 니 다.(하 나 는 제스처 비밀 번 호 를 설정 하고 하 나 는 제스처 비밀 번 호 를 검사 합 니 다)
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="com.example.gesturelock.MainActivity" > 
 
 <Button 
 android:id="@+id/setting" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentTop="true" 
 android:text="  " /> 
 
 <Button 
 android:id="@+id/verify" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_alignParentTop="true" 
 android:layout_toRightOf="@+id/setting" 
 android:text="  " /> 
 
</RelativeLayout> 
메 인 인터페이스의 코드 를 보고 있 습 니 다.
MainActivity

package com.example.gesturelock; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity implements OnClickListener { 
 
 private Button setting, verify; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 setting = (Button) findViewById(R.id.setting); 
 verify = (Button) findViewById(R.id.verify); 
 setting.setOnClickListener(this); 
 verify.setOnClickListener(this); 
 } 
 
 @Override 
 public void onClick(View v) { 
 switch (v.getId()) { 
 case R.id.setting: 
 
  startActivity(new Intent(this, GestureEditActivity.class)); 
  break; 
 case R.id.verify: 
  startActivity(new Intent(this, GestureVerifyActivity.class)); 
 
  break; 
 
 default: 
  break; 
 } 
 } 
 
} 
생각해 보 니 먼저 데이터 베 이 스 를 붙 이 는 것 이 좋 겠 다-바로 제스처 비밀 번 호 를 저장 하고 입력 할 수 있 는 제스처 비밀번호 횟수 이다.
publicSQLiteOpenHelper

package com.example.gesturelock; 
 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
 
public class publicSQLiteOpenHelper extends SQLiteOpenHelper{ 
 private static String name = "publicsqlite"; 
 private static String GESTURE_PASSWORD = "gesture_password"; 
 private static final int version = 1; 
 public SQLiteDatabase db; 
 
 public publicSQLiteOpenHelper(Context context) 
 { 
  super(context, name, null, version); 
 } 
 @Override 
 public void onCreate(SQLiteDatabase db) 
 { 
 
  String sqlpass = "create table " + "gesture_password"+ "(" 
   + GesturePassword.WXID + " text not null," 
   + GesturePassword.PASSWORD + " text not null," 
   + GesturePassword.REMAINOPPORTUNITY + " text not null)"; 
  db.execSQL(sqlpass); 
 } 
 @Override 
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 { 
  db.execSQL("DROP TABLE IF EXISTS password"); 
  onCreate(db); 
 } 
  
 public class GesturePassword 
 { 
  public static final String WXID = "wxid"; 
  public static final String PASSWORD = "password"; 
  public static final String REMAINOPPORTUNITY = "remainopportunity"; 
  
 } 
 
  
  
 /** 
  *         
  */ 
 public void insertGestureInfo(SQLiteDatabase database ,String password,String times){ 
  ContentValues values = null; 
  try { 
  values = new ContentValues(); 
  values.put(GesturePassword.WXID, "001"); 
  values.put(GesturePassword.PASSWORD, password); 
  values.put(GesturePassword.REMAINOPPORTUNITY, ""+times); 
  database.insert(publicSQLiteOpenHelper.GESTURE_PASSWORD, null, 
   values); 
   
  } catch (Exception e) { 
  e.printStackTrace(); 
  throw new SQLException(e.getMessage()); 
  }finally{ 
  if (values != null) { 
   values.clear(); 
   values = null; 
  } 
  } 
  
 } 
 /** 
  *        ---      8  
  * 
  * 
  */ 
  
  public void updateGestureInfo(SQLiteDatabase database,String password,String times) { 
  ContentValues values = null; 
   
  try { 
   
   if (password == null) { 
   values = new ContentValues(); 
   values.put(GesturePassword.WXID, "001"); 
   values.put(GesturePassword.REMAINOPPORTUNITY, ""+times); 
   database.update(publicSQLiteOpenHelper.GESTURE_PASSWORD, 
    values, "WXID=?", 
    new String[] { "001" }); 
    
   }else { 
    
   values = new ContentValues(); 
   values.put(GesturePassword.WXID, "001"); 
   values.put(GesturePassword.PASSWORD, password); 
   values.put(GesturePassword.REMAINOPPORTUNITY, ""+times); 
   database.update(publicSQLiteOpenHelper.GESTURE_PASSWORD, 
    values, "WXID=?", 
    new String[] { "001" }); 
   } 
  } catch (SQLException e) { 
   
  } finally { 
   if (values != null) { 
   values.clear(); 
   values = null; 
   } 
  } 
  } 
  /** 
  *             
  * @return 
  */ 
  public boolean queryGestureTableCount(SQLiteDatabase database){ 
  Cursor cursor = null; 
   
  try { 
   cursor = database.query(publicSQLiteOpenHelper.GESTURE_PASSWORD, null, null, null, null, null, null); 
   if (cursor != null && cursor.getCount()>0) { 
   return true; 
    
   }else { 
   return false; 
   } 
   
  } catch (Exception e) { 
   
  } 
  finally{ 
   if (cursor != null) { 
   cursor.close(); 
   cursor = null; 
   } 
  } 
  return false; 
  } 
  
  /** 
  *        --       
  * 
  */ 
  public String queryGestureTime(SQLiteDatabase database) { 
  Cursor cursor = null; 
  try { 
   cursor = database.query(publicSQLiteOpenHelper.GESTURE_PASSWORD, null, null, null, null, null, null); 
 
   if ((cursor != null) && (cursor.getCount() > 0)) { 
   if (cursor.moveToFirst()) { 
    int columnIndex = cursor 
     .getColumnIndex(GesturePassword.REMAINOPPORTUNITY); 
    return cursor.getString(columnIndex); 
   } 
 
   } 
  } catch (Exception e) { 
   
  } finally { 
   if (cursor != null) { 
   cursor.close(); 
   cursor = null; 
   } 
  } 
  return ""; 
  } 
  
  /** 
  *        --     
  * 
  */ 
  public String queryGesturePassword(SQLiteDatabase database) { 
  Cursor cursor = null; 
  try { 
   cursor = database.query(publicSQLiteOpenHelper.GESTURE_PASSWORD, null, null, null, null, null, null); 
 
   if ((cursor != null) && (cursor.getCount() > 0)) { 
   if (cursor.moveToFirst()) { 
    int columnIndex = cursor 
     .getColumnIndex(GesturePassword.PASSWORD); 
    return cursor.getString(columnIndex); 
   } 
 
   } 
  } catch (Exception e) { 
   
  } finally { 
   if (cursor != null) { 
   cursor.close(); 
   cursor = null; 
   } 
  } 
  return ""; 
  } 
} 
이어서 도구 류 를 보 세 요.바로 입력 한 제스처 비밀번호 가 간단 합 니 다.MD5 암호 화,그리고 화면 크기 도구 류 입 니 다.

/** 
 * 
 */ 
package com.example.gesturelock; 
 
import android.content.Context; 
import android.view.WindowManager; 
 
public class AppUtil { 
 
 /** 
 *       �? 
 * @param context 
 * @return 
 */ 
 public static int[] getScreenDispaly(Context context) { 
 WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
 @SuppressWarnings("deprecation") 
 int width = windowManager.getDefaultDisplay().getWidth(); 
 @SuppressWarnings("deprecation") 
 int height = windowManager.getDefaultDisplay().getHeight(); 
 int result[] = { width, height }; 
 return result; 
 } 
 
} 

package com.example.gesturelock; 
 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
 
import android.util.Log; 
 
public class MD5EncodeUtil { 
 public static String MD5Ecode(String gesture){ 
 Log.i("GYL","gesture---"+gesture.toString()); 
 byte[] toencode=gesture.toString().getBytes(); 
 Log.i("GYL","byte[]----"+toencode.toString()); 
  
 try{ 
  MessageDigest md5=MessageDigest.getInstance("MD5"); 
  md5.reset(); 
  md5.update(toencode); 
  return HexEncode(md5.digest()); 
 }catch(NoSuchAlgorithmException e){ 
  e.printStackTrace(); 
 } 
 return ""; 
 } 
 public static String HexEncode(byte[] toencode){ 
 StringBuilder sb=new StringBuilder(toencode.length*2); 
 for(byte b:toencode){ 
  sb.append(Integer.toHexString((b&0xf0)>>>4)); 
  sb.append(Integer.toHexString(b&0x0f));  
 } 
 return sb.toString().toUpperCase(); 
 } 
} 

한 걸음 한 걸음,다음 에 bean 류 를 보면 모든 원점 의 속성 입 니 다.
Constants

package com.example.gesturelock; 
 
public class Constants { 
 public static final int POINT_STATE_NORMAL = 0; //      
 public static final int POINT_STATE_SELECTED = 1; //      
 public static final int POINT_STATE_WRONG = 2; //      
 
} 
GesturePoint

package com.example.gesturelock; 
 
import android.widget.ImageView; 
 
public class GesturePoint { 
 /** 
 *   x   
 */ 
 private int leftX; 
 /** 
 *   x   
 */ 
 private int rightX; 
 /** 
 *   y   
 */ 
 private int topY; 
 /** 
 *   y   
 */ 
 private int bottomY; 
 /** 
 *       ImageView   
 */ 
 private ImageView image; 
 
 /** 
 *   x  
 */ 
 private int centerX; 
 
 /** 
 *   y  
 */ 
 private int centerY; 
 
 /** 
 *     
 */ 
 private int pointState; 
 
 /** 
 *     Point       , 1  (     1  ) 
 */ 
 private int num; 
 
 public GesturePoint(int leftX, int rightX, int topY, int bottomY, 
  ImageView image, int num) { 
 super(); 
 this.leftX = leftX; 
 this.rightX = rightX; 
 this.topY = topY; 
 this.bottomY = bottomY; 
 this.image = image; 
 this.centerX = (leftX + rightX) / 2; 
 this.centerY = (topY + bottomY) / 2; 
 this.num = num; 
 } 
 
 public int getLeftX() { 
 return leftX; 
 } 
 
 public void setLeftX(int leftX) { 
 this.leftX = leftX; 
 } 
 
 public int getRightX() { 
 return rightX; 
 } 
 
 public void setRightX(int rightX) { 
 this.rightX = rightX; 
 } 
 
 public int getTopY() { 
 return topY; 
 } 
 
 public void setTopY(int topY) { 
 this.topY = topY; 
 } 
 
 public int getBottomY() { 
 return bottomY; 
 } 
 
 public void setBottomY(int bottomY) { 
 this.bottomY = bottomY; 
 } 
 
 public ImageView getImage() { 
 return image; 
 } 
 
 public void setImage(ImageView image) { 
 this.image = image; 
 } 
 
 public int getCenterX() { 
 return centerX; 
 } 
 
 public void setCenterX(int centerX) { 
 this.centerX = centerX; 
 } 
 
 public int getCenterY() { 
 return centerY; 
 } 
 
 public void setCenterY(int centerY) { 
 this.centerY = centerY; 
 } 
 
 public int getPointState() { 
 return pointState; 
 } 
 
 public void setPointState(int state, int x, int y) { 
 
 pointState = state; 
 switch (state) { 
 
 case Constants.POINT_STATE_NORMAL: 
  this.image.setBackgroundResource(R.drawable.gesturewhite); 
  break; 
 case Constants.POINT_STATE_SELECTED: 
 
  //       ,    ,     
  if (y == 0 && x > 0) { 
  this.image.setBackgroundResource(R.drawable.blueright); 
  } else if (y == 0 && x < 0) { 
  this.image.setBackgroundResource(R.drawable.blueleft); 
  } else if (x == 0 && y > 0) { 
  this.image.setBackgroundResource(R.drawable.bluedown); 
  } else if (x == 0 && y < 0) { 
  this.image.setBackgroundResource(R.drawable.blueup); 
  } else if (x > 0 && y > 0) { 
  this.image.setBackgroundResource(R.drawable.bluerightdown); 
  } else if (x < 0 && y < 0) { 
  this.image.setBackgroundResource(R.drawable.blueleftup); 
  } else if (x < 0 && y > 0) { 
  this.image.setBackgroundResource(R.drawable.blueleftdown); 
  } else if (x > 0 && y < 0) { 
  this.image.setBackgroundResource(R.drawable.bluerightup); 
  } else if (x == 0 && y == 0) { 
  this.image.setBackgroundResource(R.drawable.bluedot); 
  } 
 
  break; 
 case Constants.POINT_STATE_WRONG: 
 
  if (y == 0 && x > 0) { 
  this.image.setBackgroundResource(R.drawable.redright); 
  } else if (y == 0 && x < 0) { 
  this.image.setBackgroundResource(R.drawable.redleft); 
  } else if (x == 0 && y > 0) { 
  this.image.setBackgroundResource(R.drawable.reddown); 
  } else if (x == 0 && y < 0) { 
  this.image.setBackgroundResource(R.drawable.redup); 
  } else if (x > 0 && y > 0) { 
  this.image.setBackgroundResource(R.drawable.redrightdown); 
  } else if (x < 0 && y < 0) { 
  this.image.setBackgroundResource(R.drawable.redleftup); 
  } else if (x < 0 && y > 0) { 
  this.image.setBackgroundResource(R.drawable.redleftdown); 
  } else if (x > 0 && y < 0) { 
  this.image.setBackgroundResource(R.drawable.redrightup); 
  } else if (x == 0 && y == 0) { 
  this.image.setBackgroundResource(R.drawable.reddot); 
  } 
  break; 
 default: 
  break; 
 } 
 } 
 
 public int getNum() { 
 return num; 
 } 
 
 public void setNum(int num) { 
 this.num = num; 
 } 
 
 @Override 
 public int hashCode() { 
 final int prime = 31; 
 int result = 1; 
 result = prime * result + bottomY; 
 result = prime * result + ((image == null) ? 0 : image.hashCode()); 
 result = prime * result + leftX; 
 result = prime * result + rightX; 
 result = prime * result + topY; 
 return result; 
 } 
 
 @Override 
 public boolean equals(Object obj) { 
 if (this == obj) 
  return true; 
 if (obj == null) 
  return false; 
 if (getClass() != obj.getClass()) 
  return false; 
 GesturePoint other = (GesturePoint) obj; 
 if (bottomY != other.bottomY) 
  return false; 
 if (image == null) { 
  if (other.image != null) 
  return false; 
 } else if (!image.equals(other.image)) 
  return false; 
 if (leftX != other.leftX) 
  return false; 
 if (rightX != other.rightX) 
  return false; 
 if (topY != other.topY) 
  return false; 
 return true; 
 } 
 
 @Override 
 public String toString() { 
 return "Point [leftX=" + leftX + ", rightX=" + rightX + ", topY=" 
  + topY + ", bottomY=" + bottomY + "]"; 
 } 
} 
다음은 그 9 개의 점 그리 기 화면 을 보 겠 습 니 다.
GestureContentView

package com.example.gesturelock; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import com.example.gesturelock.GestureDrawline.GestureCallBack; 
 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
 
 
/** 
 *         
 * 
 */ 
public class GestureContentView extends ViewGroup { 
 
 
 /** 
 *          
 */ 
 private int blockWidth; 
 private int spacing ; 
 
 /** 
 *                
 */ 
 private List<GesturePoint> list; 
 private Context context; 
 private GestureDrawline gestureDrawline; 
 
 /** 
 *   9 ImageView   ,    
 * @param context 
 * @param isVerify           
 * @param passWord        
 * @param callBack           
 */ 
 public GestureContentView(Context context, boolean isVerify,String passWord, GestureCallBack callBack) { 
 super(context); 
 this.blockWidth = dip2px(context,66f); //     dp  
 this.spacing = dip2px(context, 30f); //       dp  
 this.list = new ArrayList<GesturePoint>(); 
 this.context = context; 
 //   9    
 addChild(); 
 //           view 
 gestureDrawline = new GestureDrawline(context, list, isVerify,passWord, callBack); 
 } 
 
 public static int dip2px(Context context, float dpValue) { 
 // TODO Auto-generated method stub 
 final float scale = context.getResources().getDisplayMetrics().density; 
 return (int) (dpValue * scale + 0.5f); 
 } 
 
 private void addChild(){ 
 for (int i = 0; i < 9; i++) { 
  ImageView image = new ImageView(context); 
  image.setBackgroundResource(R.drawable.gesturewhite);  
  this.addView(image,new LayoutParams(blockWidth, blockWidth)); 
  invalidate(); 
  //     
  int row = i / 3; 
  //     
  int col = i % 3; 
  //          
  int leftX = col * blockWidth + col * spacing; 
  int topY = row * blockWidth + row * spacing; 
  int rightX = leftX + blockWidth; 
  int bottomY = topY + blockWidth; 
  
  //         
  GesturePoint p = new GesturePoint(leftX, rightX, topY, bottomY, image,i+1); 
  this.list.add(p); 
 } 
 } 
 
 public void setParentView(ViewGroup parent){ 
 //         
 LayoutParams layoutParams = 
  new LayoutParams(blockWidth * 3 + spacing * 2, blockWidth * 3 + spacing * 2); 
 parent.addView(gestureDrawline,layoutParams); 
 parent.addView(this,layoutParams); 
 } 
 
 @Override 
 protected void onLayout(boolean changed, int l, int t, int r, int b) { 
 for (int i = 0; i < getChildCount(); i++) { 
  //    
  int row = i/3; 
  //    
  int col = i%3; 
  View v = getChildAt(i); 
  //         ,d  X       ,             ,  top bottom                
  int leftX = col * blockWidth + col * spacing; 
  int topY = row * blockWidth + row * spacing; 
  int rightX = leftX + blockWidth; 
  int bottomY = topY + blockWidth; 
  
  v.layout(leftX, topY, rightX, bottomY) ; 
 } 
 } 
 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 } 
 
 /** 
 *     delayTime    
 * @param delayTime 
 */ 
 public void clearDrawlineState(long delayTime) { 
 gestureDrawline.clearDrawlineState(delayTime); 
 } 
 
} 
그리고 손가락 이 연 결 될 때 중간 연결선 을 그 리 는 코드 를 계속 보 세 요.
GestureDrawline

package com.example.gesturelock; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
 
import android.annotation.SuppressLint; 
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.Style; 
import android.graphics.PorterDuff; 
import android.os.Handler; 
 
import android.util.Log; 
import android.util.Pair; 
import android.view.MotionEvent; 
import android.view.View; 
 
/** 
 *          
 * 
 */ 
public class GestureDrawline extends View { 
 private int mov_x;//        
 private int mov_y; 
 private Paint paint;//      
 private Canvas canvas;//    
 private Bitmap bitmap;//    
 private List<GesturePoint> list;// 
 private List<Pair<GesturePoint, GesturePoint>> lineList;//        
 private Map<String, GesturePoint> autoCheckPointMap;//          
 private boolean isDrawEnable = true; //        
 /** 
 *          
 */ 
 private int[] screenDispaly; 
 
 private GesturePoint currentPoint; 
 private GestureCallBack callBack; 
 private StringBuilder passWordSb; 
 private boolean isVerify; 
 private String passWord; 
 
 public GestureDrawline(Context context, List<GesturePoint> list, 
  boolean isVerify, String passWord, GestureCallBack callBack) { 
 super(context); 
 //     
 screenDispaly = AppUtil.getScreenDispaly(context); 
 //   
 paint = new Paint(Paint.DITHER_FLAG);// 
 //     --   
 bitmap = Bitmap.createBitmap(screenDispaly[0], screenDispaly[0],Bitmap.Config.ARGB_8888); // 
 canvas = new Canvas(); 
 canvas.setBitmap(bitmap); 
 //       
 paint.setStyle(Style.STROKE); 
 paint.setStrokeWidth(15);// 
 paint.setColor(getResources().getColor(R.color.gestureline_green));//          
 paint.setAntiAlias(true); 
 //9      
 this.list = list; 
 //       
 this.lineList = new ArrayList<Pair<GesturePoint, GesturePoint>>(); 
 initAutoCheckPointMap(); 
 this.callBack = callBack; 
  
 this.isVerify = isVerify; 
 this.passWord = passWord; 
 //    
 this.passWordSb = new StringBuilder(); 
 } 
 
 private void initAutoCheckPointMap() { 
 autoCheckPointMap = new HashMap<String, GesturePoint>(); 
 autoCheckPointMap.put("1,3", getGesturePointByNum(2)); 
 autoCheckPointMap.put("1,7", getGesturePointByNum(4)); 
 autoCheckPointMap.put("1,9", getGesturePointByNum(5)); 
 autoCheckPointMap.put("2,8", getGesturePointByNum(5)); 
 autoCheckPointMap.put("3,7", getGesturePointByNum(5)); 
 autoCheckPointMap.put("3,9", getGesturePointByNum(6)); 
 autoCheckPointMap.put("4,6", getGesturePointByNum(5)); 
 autoCheckPointMap.put("7,9", getGesturePointByNum(8)); 
 } 
 
 /**       */ 
 private GesturePoint getGesturePointByNum(int num) { 
 for (GesturePoint point : list) { 
  if (point.getNum() == num) { 
  return point; 
  } 
 } 
 return null; 
 } 
 
 /**         */ 
 @Override 
 protected void onDraw(Canvas canvas) { 
 canvas.drawBitmap(bitmap, 0, 0, null); 
 } 
 
 //      
 @SuppressLint("ClickableViewAccessibility") 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
 if (isDrawEnable == false) { 
  return true; 
 } 
 //          
 paint.setColor(getResources().getColor(R.color.gestureline_green)); 
 switch (event.getAction()) { 
 case MotionEvent.ACTION_DOWN: 
  mov_x = (int) event.getX(); 
  mov_y = (int) event.getY(); 
  /**       */ 
  currentPoint = getPointAt(mov_x, mov_y); 
  if (currentPoint != null) { 
  /**          ,    */ 
  currentPoint.setPointState(Constants.POINT_STATE_SELECTED, 0, 0); 
  passWordSb.append(currentPoint.getNum()); 
  } 
  invalidate(); 
  //     
  callBack.onPointDown() ; 
  break; 
 case MotionEvent.ACTION_MOVE: 
  //     
  clearScreenAndDrawList(); 
  //     
  callBack.onGestureLineMove(); 
  //            
  GesturePoint pointAt = getPointAt((int) event.getX(), 
   (int) event.getY()); 
  if (currentPoint == null && pointAt == null) { 
  return true; 
  } else { 
  //          
  if (currentPoint == null && pointAt != null) { 
   //     ,             currentPoint 
   currentPoint = pointAt; 
   /**          ,    */ 
   currentPoint.setPointState(Constants.POINT_STATE_SELECTED, 
    0, 0); 
   passWordSb.append(currentPoint.getNum()); 
  } 
  } 
  /***/ 
  if (pointAt == null 
   || currentPoint.equals(pointAt) 
   || Constants.POINT_STATE_SELECTED == pointAt 
    .getPointState()) { 
 
   canvas.drawLine(currentPoint.getCenterX(), 
   currentPoint.getCenterY(), event.getX(), event.getY(), 
   paint); 
  } else { 
 
 
  int x = pointAt.getCenterX() - currentPoint.getCenterX(); 
  int y = pointAt.getCenterY() - currentPoint.getCenterY(); 
  currentPoint 
   .setPointState(Constants.POINT_STATE_SELECTED, x, y); 
  if (pointAt.equals(list.get(list.size() - 1))) { 
   pointAt 
   .setPointState(Constants.POINT_STATE_SELECTED, 0, 0); 
  } 
 
  GesturePoint betweenPoint = getBetweenCheckPoint(currentPoint, 
   pointAt); 
  if (betweenPoint != null 
   && Constants.POINT_STATE_SELECTED != betweenPoint 
    .getPointState()) { 
 
   //              
   Pair<GesturePoint, GesturePoint> pair1 = new Pair<GesturePoint, GesturePoint>( 
    currentPoint, betweenPoint); 
   lineList.add(pair1); 
   passWordSb.append(betweenPoint.getNum()); 
   Pair<GesturePoint, GesturePoint> pair2 = new Pair<GesturePoint, GesturePoint>( 
    betweenPoint, pointAt); 
   lineList.add(pair2); 
   passWordSb.append(pointAt.getNum()); 
   betweenPoint.setPointState(Constants.POINT_STATE_SELECTED,0, 0); 
   currentPoint = pointAt; 
  } else { 
   Pair<GesturePoint, GesturePoint> pair = new Pair<GesturePoint, GesturePoint>( 
    currentPoint, pointAt); 
   lineList.add(pair); 
   passWordSb.append(pointAt.getNum()); 
   currentPoint = pointAt; 
  } 
  } 
  invalidate(); 
  break; 
 case MotionEvent.ACTION_UP: 
  isDrawEnable = false; 
  if (isVerify) { 
  //        
  String encodeString=MD5EncodeUtil.MD5Ecode(String.valueOf( passWordSb)); 
  String passwordMd5Ecode = MD5EncodeUtil.MD5Ecode(passWord); 
  if (passwordMd5Ecode.equals(encodeString)) { 
   
   callBack.checkedSuccess(); 
  } else { 
   Log.e("TAG", "encode String fali"); 
   callBack.checkedFail(); 
  } 
  } else { 
  //       
  clearScreenAndDrawList(); 
  GesturePoint pointAtUp = getPointAt((int) event.getX(), 
   (int) event.getY()); 
  if (pointAtUp != null 
   && currentPoint.equals(pointAtUp) 
   && Constants.POINT_STATE_SELECTED == pointAtUp 
    .getPointState()&&passWordSb.length()==1) { 
   currentPoint.setPointState(Constants.POINT_STATE_WRONG, 
    0, 0); 
  } 
  invalidate(); 
  callBack.onGestureCodeInput(passWordSb.toString()); 
  } 
  break; 
  
 default: 
  break; 
 } 
 return true; 
 } 
 
 /** 
 * 
 * @param delayTime 
 *         
 */ 
 public void clearDrawlineState(long delayTime) { 
 if (delayTime > 0) { 
  //          
  isDrawEnable = false; 
  drawErrorPathTip(); 
 } 
 new Handler().postDelayed(new clearStateRunnable(), delayTime); 
 } 
 
 /** 
 */ 
 final class clearStateRunnable implements Runnable { 
 public void run() { 
  //   passWordSb 
  passWordSb = new StringBuilder(); 
  //          
  lineList.clear(); 
  //        
  clearScreenAndDrawList(); 
  for (GesturePoint p : list) { 
  p.setPointState(Constants.POINT_STATE_NORMAL, 0, 0); 
  } 
  invalidate(); 
  isDrawEnable = true; 
 } 
 } 
 
 /** 
 * 
 * @param x 
 * @param y 
 */ 
 private GesturePoint getPointAt(int x, int y) { 
 
 for (GesturePoint point : list) { 
  //    x 
  int leftX = point.getLeftX(); 
  int rightX = point.getRightX(); 
  if (!(x >= leftX && x < rightX)) { 
  continue; 
  } 
 
  int topY = point.getTopY(); 
  int bottomY = point.getBottomY(); 
  if (!(y >= topY && y < bottomY)) { 
  continue; 
  } 
 
  //       ,                          
  return point; 
 } 
 
 return null; 
 } 
 
 private GesturePoint getBetweenCheckPoint(GesturePoint pointStart, 
  GesturePoint pointEnd) { 
 int startNum = pointStart.getNum(); 
 int endNum = pointEnd.getNum(); 
 String key = null; 
 if (startNum < endNum) { 
  key = startNum + "," + endNum; 
 } else { 
  key = endNum + "," + startNum; 
 } 
 return autoCheckPointMap.get(key); 
 } 
 
 /** 
 *          ,           
 */ 
 private void clearScreenAndDrawList() { 
  
 
 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
 for (int i = 0; i < lineList.size(); i++) 
 { 
  int r = 0,x,y; 
  if ((lineList.get(i).first.getTopY() - lineList.get(i).first.getBottomY()) / 2 < 0) 
  r = -(lineList.get(i).first.getTopY() - lineList.get(i).first.getBottomY()) / 2; 
  else { 
  r = (lineList.get(i).first.getTopY() - lineList.get(i).first.getBottomY()) / 2; 
  } 
  float d = (float) 0.707 * r; 
  x = lineList.get(i).second.getCenterX() -lineList.get(i).first.getCenterX(); 
  y = lineList.get(i).second.getCenterY() -lineList.get(i).first.getCenterY(); 
  
  if (i == lineList.size() - 1) { 
  lineList.get(i).second.setPointState(Constants.POINT_STATE_SELECTED, 0, 0); 
  lineList.get(i).first.setPointState(Constants.POINT_STATE_SELECTED, x, y); 
  } 
  else { 
   lineList.get(i).first.setPointState(Constants.POINT_STATE_SELECTED, x, y); 
   lineList.get(i).second.setPointState(Constants.POINT_STATE_SELECTED, x, y); 
  } 
  
  
  if (y == 0 && x > 0) { 
  canvas.drawLine(lineList.get(i).first.getCenterX() + r, 
   lineList.get(i).first.getCenterY(), lineList.get(i).second.getCenterX() - r, 
   lineList.get(i).second.getCenterY(), paint);//    
  } 
  else if (y == 0 && x < 0 ) { 
  canvas.drawLine(lineList.get(i).first.getCenterX() - r, 
   lineList.get(i).first.getCenterY(), lineList.get(i).second.getCenterX() + r, 
   lineList.get(i).second.getCenterY(), paint);//    
  }else if (x == 0 && y > 0){ 
  canvas.drawLine(lineList.get(i).first.getCenterX(), 
   lineList.get(i).first.getCenterY() + r, lineList.get(i).second.getCenterX(), 
   lineList.get(i).second.getCenterY() - r, paint);//    
   
  }else if (x == 0 && y < 0 ) {   
  canvas.drawLine(lineList.get(i).first.getCenterX(), 
   lineList.get(i).first.getCenterY() - r, lineList.get(i).second.getCenterX() , 
   lineList.get(i).second.getCenterY() + r, paint);//    
  } 
  else if( x > 0 && y > 0 ){ 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() + d, 
   lineList.get(i).first.getCenterY() + d, lineList.get(i).second.getCenterX() - d , 
   lineList.get(i).second.getCenterY() - d, paint);//    
  }else if(x > 0 && y < 0 ) 
  { 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() + d, 
   lineList.get(i).first.getCenterY() - d, lineList.get(i).second.getCenterX()-d , 
   lineList.get(i).second.getCenterY() + d, paint);//      
  } 
  else if (x < 0 && y > 0){ 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() - d, 
   lineList.get(i).first.getCenterY() + d, lineList.get(i).second.getCenterX()+d , 
   lineList.get(i).second.getCenterY() - d, paint);//    
  } 
  else if(x < 0 && y < 0 ) 
  { 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() - d, 
   lineList.get(i).first.getCenterY() - d, lineList.get(i).second.getCenterX()+d , 
   lineList.get(i).second.getCenterY() + d, paint);//    
  } 
 } 
 
 } 
 
 private void drawErrorPathTip() { 
  
 
 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
 
paint.setColor(getResources().getColor(R.color.gestureline_red));//          
 if(lineList.size() == 0 && currentPoint!= null){ 
  
  currentPoint.setPointState(Constants.POINT_STATE_WRONG, 0, 0); 
 } 
 for (int i = 0; i < lineList.size(); i++) {  
  int r = 0,x,y; 
  if ((lineList.get(i).first.getTopY() - lineList.get(i).first.getBottomY()) / 2 < 0) 
  r = -(lineList.get(i).first.getTopY() - lineList.get(i).first.getBottomY()) / 2; 
  else { 
  r = (lineList.get(i).first.getTopY() - lineList.get(i).first.getBottomY()) / 2; 
  } 
  
  float d = (float) 0.707 * r; 
  x = lineList.get(i).second.getCenterX() -lineList.get(i).first.getCenterX(); 
  y = lineList.get(i).second.getCenterY() -lineList.get(i).first.getCenterY(); 
  
  
  if (i == lineList.size() - 1) { 
  lineList.get(i).second.setPointState(Constants.POINT_STATE_WRONG, 0, 0); 
  lineList.get(i).first.setPointState(Constants.POINT_STATE_WRONG, x, y); 
  } 
  else { 
   lineList.get(i).first.setPointState(Constants.POINT_STATE_WRONG, x, y); 
   lineList.get(i).second.setPointState(Constants.POINT_STATE_WRONG, x, y); 
  } 
  
  if (y == 0 && x > 0) { 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() + r, 
   lineList.get(i).first.getCenterY(), lineList.get(i).second.getCenterX() - r, 
   lineList.get(i).second.getCenterY(), paint);//    
  } 
  else if (y == 0 && x < 0 ) { 
  canvas.drawLine(lineList.get(i).first.getCenterX() - r, 
   lineList.get(i).first.getCenterY(), lineList.get(i).second.getCenterX() + r, 
   lineList.get(i).second.getCenterY(), paint);//    
  }else if (x == 0 && y > 0){ 
  canvas.drawLine(lineList.get(i).first.getCenterX(), 
   lineList.get(i).first.getCenterY() + r, lineList.get(i).second.getCenterX(), 
   lineList.get(i).second.getCenterY() - r, paint);//    
   
  }else if (x == 0 && y < 0 ) {   
  canvas.drawLine(lineList.get(i).first.getCenterX(), 
   lineList.get(i).first.getCenterY() - r, lineList.get(i).second.getCenterX() , 
   lineList.get(i).second.getCenterY() + r, paint);//    
  } 
  else if( x > 0 && y > 0 ){ 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() + d, 
   lineList.get(i).first.getCenterY() + d, lineList.get(i).second.getCenterX() - d , 
   lineList.get(i).second.getCenterY() - d, paint);//    
  }else if(x > 0 && y < 0 ) 
  { 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() + d, 
   lineList.get(i).first.getCenterY() - d, lineList.get(i).second.getCenterX()-d , 
   lineList.get(i).second.getCenterY() + d, paint);//      
  } 
  else if (x < 0 && y > 0){ 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() - d, 
   lineList.get(i).first.getCenterY() + d, lineList.get(i).second.getCenterX()+d , 
   lineList.get(i).second.getCenterY() - d, paint);//    
  } 
  else if(x < 0 && y < 0 ) 
  { 
   
  canvas.drawLine(lineList.get(i).first.getCenterX() - d, 
   lineList.get(i).first.getCenterY() - d, lineList.get(i).second.getCenterX()+d , 
   lineList.get(i).second.getCenterY() + d, paint);//    
  } 
 } 
 invalidate(); 
 } 
 
 
 public interface GestureCallBack { 
 
 public abstract void onGestureCodeInput(String inputCode); 
 
 public abstract void checkedSuccess(); 
 
 public abstract void checkedFail(); 
  
  
 public abstract void onGestureLineMove(); 
  
 public abstract void onPointDown() ; 
 } 
 
} 
ok 주석 준비 작업 이 완료 되 었 습 니 다.다음은 제스처 암호 인터페이스 설정 을 보십시오.
gesture_edit.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:background="#f2f2f2" > 
 
 <LinearLayout 
 android:id="@+id/ll_head" 
 android:layout_width="fill_parent" 
 android:layout_height="48dp" 
 android:background="@drawable/bg_head" 
 android:gravity="center_vertical" 
 android:orientation="horizontal" > 
 
 <LinearLayout 
  android:id="@+id/ll_head_left" 
  android:layout_width="wrap_content" 
  android:layout_height="fill_parent" 
  android:layout_marginTop="1dp" 
  android:gravity="center_vertical" 
  android:background="@drawable/selector_head_back" 
  android:orientation="horizontal" > 
 
  <ImageView 
  android:id="@+id/im_back" 
  android:layout_width="7dp" 
  android:layout_height="14dp" 
  android:layout_marginLeft="5dp" 
  android:background="@drawable/back" /> 
 
  <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginLeft="5dp" 
  android:text="@string/set_gesture_code" 
  android:textColor="#fff" 
  android:textSize="17.33dp" /> 
 </LinearLayout> 
 
 <View 
  android:layout_width="0dp" 
  android:layout_height="fill_parent" 
  android:layout_weight="1" /> 
 </LinearLayout> 
 
 <LinearLayout 
 android:id="@+id/gesture_tip_layout" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="75.67dp" 
 android:orientation="vertical" > 
 
 <TextView 
  android:id="@+id/tv_edit_texttip" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:gravity="center_horizontal" 
  android:text="@string/set_gesture_pattern" 
  android:textColor="@color/color_black" 
  android:textSize="@dimen/textsize_s3" /> 
 </LinearLayout> 
 
 <RelativeLayout 
 android:id="@+id/fl_edit_gesture_container" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="48dp" 
 android:gravity="center_horizontal" > 
 </RelativeLayout> 
 
 
</LinearLayout> 
GestureEditActivity

package com.example.gesturelock; 
 
import android.app.Activity; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.os.Handler; 
import android.text.TextUtils; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
import com.example.gesturelock.GestureDrawline.GestureCallBack; 
 
 
public class GestureEditActivity extends Activity implements OnClickListener { 
 
 private LinearLayout im_back; 
 private TextView tv_texttip; 
 private RelativeLayout mGestureContainer; 
 private GestureContentView mGestureContentView; 
 //          
 private String mFirstPassword = null; 
 //          
 private boolean mIsFirstInput = true; 
 static final String TABLES_NAME_GESTURE = "gesture_password"; 
 
 public final static int GESTURE_TIME = 8; 
 
 private publicSQLiteOpenHelper sqliteInstance; 
 private SQLiteDatabase database; 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.gesture_edit); 
 //      
 tv_texttip = (TextView) findViewById(R.id.tv_edit_texttip); 
 // 9    
 mGestureContainer = (RelativeLayout) findViewById(R.id.fl_edit_gesture_container); 
 //    
 im_back = (LinearLayout) findViewById(R.id.ll_head_left); 
 //           
 im_back.setOnClickListener(this); 
 
 mGestureContentView = new GestureContentView(this, false, "", 
  new GestureCallBack() { 
 
   
 
   @Override 
   public void onGestureCodeInput(String inputCode) { 
   
   if (mIsFirstInput) { //           
    if (!isInputPassValidate(inputCode)) { 
    //     4   
    tv_texttip.setText(getResources().getString( 
     R.string.drawguesturewarning)); 
    // 2       
    mGestureContentView.clearDrawlineState(2000L); 
    // 2       
    mHandler.postDelayed(connect4Dot, 2000); 
    return; 
    }else { 
    mFirstPassword = inputCode; 
     
    tv_texttip.setText(getResources().getString( 
     R.string.drawguestureagain)); 
    mHandler.postDelayed(clearGreenLine, 1000); 
    mIsFirstInput = false; 
    } 
   } else { 
    if (inputCode.equals(mFirstPassword)) { //                 
    tv_texttip.setText(getResources().getString( 
     R.string.your_gesture_code)); 
 
    sqliteInstance = new publicSQLiteOpenHelper(GestureEditActivity.this); 
    database = sqliteInstance.getWritableDatabase(); 
    boolean count = sqliteInstance.queryGestureTableCount(database); 
    if (count) { 
     //      
     sqliteInstance.updateGestureInfo(database,mFirstPassword, "8"); 
    }else { 
     //       
     sqliteInstance.insertGestureInfo(database,mFirstPassword, "8"); 
 
    } 
 
    mHandler.postDelayed(setGestureOkFinish, 1000); 
    } else { 
 
    if (!isInputPassValidate(inputCode)) { 
     //     4   
     tv_texttip.setText(getResources().getString( 
      R.string.drawguesturewarning)); 
     // 2       
     mGestureContentView.clearDrawlineState(2000L); 
     // 2       
     mHandler.postDelayed(connect4Dot, 2000); 
    } else { //                  
     tv_texttip.setText(getResources() 
      .getString(R.string.drawagain)); 
     //        
//     Animation shakeAnimation = 
//     AnimationUtils.loadAnimation(GestureEditActivity.this,R.anim.shake); 
//     tv_texttip.startAnimation(shakeAnimation); 
     //       ,1.5     
     mGestureContentView 
      .clearDrawlineState(2000L); 
     mHandler.postDelayed(changeText2again, 2000); 
    } 
 
    } 
   } 
   
   } 
 
   @Override 
   public void checkedFail() { 
 
   } 
 
   @Override 
   public void onGestureLineMove() { 
   tv_texttip.setText(getResources().getString( 
    R.string.release_hande_when_finish)); 
   } 
 
   @Override 
   public void onPointDown() { 
 
   } 
 
   @Override 
   public void checkedSuccess() { 
 
   } 
  }); 
 //                 
 mGestureContentView.setParentView(mGestureContainer); 
 } 
 
 @Override 
 protected void onResume() { 
 super.onResume(); 
 } 
 
 Handler mHandler = new Handler(); 
 
 Runnable clearGreenLine = new Runnable() { 
 public void run() { 
  mGestureContentView.clearDrawlineState(0L); 
 } 
 }; 
 
 Runnable connect4Dot = new Runnable() { 
 public void run() { 
  tv_texttip.setText(getResources().getString( 
   R.string.set_gesture_pattern)); 
 } 
 }; 
 
 Runnable setGestureOkFinish = new Runnable() { 
 public void run() { 
  finish(); 
 } 
 }; 
 Runnable changeText2again = new Runnable() { 
 public void run() { 
  tv_texttip.setText(getResources().getString( 
   R.string.drawguestureagain)); 
 } 
 }; 
 
 private boolean isInputPassValidate(String inputPassword) { 
 if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) { 
  return false; 
 } 
 return true; 
 } 
 
 
 protected void onDestroy() { 
 super.onDestroy(); 
 } 
 
 @Override 
 public void onClick(View v) { 
 switch (v.getId()) { 
 case R.id.ll_head_left: 
  finish(); 
  break; 
 } 
 } 
} 
마지막 으로 검사 인터페이스 보기
gesture_verify.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:orientation="vertical" 
 android:layout_height="match_parent" 
 android:background="@color/color_main" > 
 
 <LinearLayout 
 android:id="@+id/ll_head" 
 android:layout_width="fill_parent" 
 android:layout_height="48dp" 
 android:background="@drawable/bg_head" 
 android:gravity="center_vertical" 
 android:orientation="horizontal" > 
 
 <LinearLayout 
  android:id="@+id/ll_head_left" 
  android:layout_width="wrap_content" 
  android:layout_height="fill_parent" 
  android:layout_marginTop="1dp" 
  android:gravity="center_vertical" 
  android:background="@drawable/selector_head_back" 
  android:orientation="horizontal" > 
 
  <ImageView 
  android:id="@+id/im_back" 
  android:layout_width="7dp" 
  android:layout_height="14dp" 
  android:layout_marginLeft="5dp" 
  android:background="@drawable/back" /> 
 
  <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginLeft="5dp" 
  android:text="@string/verify_gesture_code" 
  android:textColor="#fff" 
  android:textSize="17.33dp" /> 
 </LinearLayout> 
 
 <View 
  android:layout_width="0dp" 
  android:layout_height="fill_parent" 
  android:layout_weight="1" /> 
 </LinearLayout> 
 
 
 <LinearLayout 
 android:id="@+id/gesture_tip_layout" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="75.67dp" 
 android:orientation="vertical" > 
 
 <TextView 
  android:id="@+id/tv_edit_texttip" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:gravity="center_horizontal" 
  android:text="@string/set_gesture_pattern" 
  android:textColor="@color/color_black" 
  android:textSize="@dimen/textsize_s3" /> 
 </LinearLayout> 
 <RelativeLayout 
 android:id="@+id/fl_verify_gesture_container" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="48dp" 
 android:gravity="center_horizontal" > 
 </RelativeLayout> 
 
 
 
</LinearLayout> 
마지막 검사 코드

package com.example.gesturelock; 
 
import java.util.Timer; 
import java.util.TimerTask; 
 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.text.TextUtils; 
import android.util.Log; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
import com.example.gesturelock.GestureDrawline.GestureCallBack; 
 
 
public class GestureVerifyActivity extends Activity { 
 private static String TAG = "GestureVerifyActivity"; 
 private TextView tv_texttip; 
 private RelativeLayout mGestureContainer; 
 private LinearLayout ib_back; 
 private GestureContentView mGestureContentView; 
 private Timer timer = new Timer() ; 
 private TimerTask closeActivityTask = null; 
 
 static final String TABLES_NAME_GESTURE = "gesture_password"; 
 private int remainopportunity ; 
 
 private publicSQLiteOpenHelper sqliteInstance; 
 private SQLiteDatabase database; 
 
 private void resetCloseEvent(){ 
 clearCloseEvent() ; 
 closeActivityTask = new TimerTask() { 
  public void run() { 
  Message message = new Message(); 
  message.what = 1; 
  handler.sendMessage(message); 
  } 
 }; 
 timer.schedule(closeActivityTask, 30000); 
 } 
 private void clearCloseEvent(){ 
 if(closeActivityTask != null){ 
  closeActivityTask.cancel() ; 
  closeActivityTask = null ; 
  Log.i(TAG, "----------closeActivityTask----------"+closeActivityTask); 
 } 
 } 
 
 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.gesture_verify); 
 
 resetCloseEvent(); 
 
 tv_texttip = (TextView) findViewById(R.id.tv_edit_texttip); 
 ib_back = (LinearLayout) findViewById(R.id.ll_head_left); 
 mGestureContainer = (RelativeLayout) findViewById(R.id.fl_verify_gesture_container); 
  
 sqliteInstance = new publicSQLiteOpenHelper(GestureVerifyActivity.this); 
 database = sqliteInstance.getWritableDatabase(); 
 //           、     
 firstPassward = sqliteInstance.queryGesturePassword(database); 
 remainopportunityStr = sqliteInstance.queryGestureTime(database); 
 if (TextUtils.isEmpty(remainopportunityStr)) { 
  remainopportunity = 8; 
  //   8  
  sqliteInstance.updateGestureInfo(database,null, "8"); 
 } else { 
  remainopportunity = Integer.parseInt(remainopportunityStr); 
 } 
 if(remainopportunity == 0){ 
  tv_texttip.setTextColor(getResources().getColor(R.color.gestureline_red)); 
  tv_texttip.setText(getResources().getString(R.string.no_chance)); 
 } 
  
 ib_back.setOnClickListener(new View.OnClickListener() { 
  
  @Override 
  public void onClick(View v) { 
  finish(); 
  } 
 }); 
  
 
 mGestureContentView = new GestureContentView(this, true, firstPassward, 
  new GestureCallBack() { 
   @Override 
   public void onGestureCodeInput(String inputCode) { 
    
 
   } 
 
   @SuppressLint("ShowToast") 
   @Override 
   public void checkedSuccess() { 
    
   sqliteInstance.updateGestureInfo(database,null, "8"); 
   mGestureContentView.clearDrawlineState(0L); 
   resetCloseEvent(); 
   finish(); 
   //     
   sendBroadcast(new Intent("com.godinsec.seland.intent.CASIntent.INTENT_LOAD_SD")); 
    
   } 
   @Override 
   public void checkedFail() { 
   mGestureContentView.clearDrawlineState(2000L); 
   tv_texttip.setTextColor(getResources().getColor(R.color.gestureline_red)); 
   if(remainopportunity > 0){ 
    remainopportunity-- ; 
   } 
   sqliteInstance.updateGestureInfo(database,null, ""+remainopportunity); 
   resetCloseEvent(); 
   changeText(); 
    
   } 
 
   @Override 
   public void onGestureLineMove() { 
   if(remainopportunity > 0){ 
    tv_texttip.setTextColor(getResources().getColor(R.color.textcolor_black_bb)); 
    tv_texttip.setText(getResources().getString(R.string.release_hande_when_finish)); 
   } 
   } 
 
   @Override 
   public void onPointDown() { 
   clearCloseEvent() ; 
   } 
   
  }); 
 mGestureContentView.setParentView(mGestureContainer); 
 } 
 
 protected void changeText() { 
 Log.e("TAGG", "changeText"); 
 if (remainopportunity == 7) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_seven)); 
 } else if (remainopportunity == 6) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_six)); 
 } else if (remainopportunity == 5) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_five)); 
 } else if (remainopportunity == 4) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_four)); 
 } else if (remainopportunity == 3) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_three)); 
 } else if (remainopportunity == 2) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_two)); 
 } else if (remainopportunity == 1) { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_one)); 
 } else { 
  tv_texttip.setText(getResources().getString( 
   R.string.wrong_answer_zero)); 
  handler.postDelayed(errorFinish, 2000); 
 } 
 } 
 
 @Override 
 protected void onResume() { 
 super.onResume(); 
 } 
 
 Runnable errorFinish = new Runnable() { 
 public void run() { 
  finish(); 
 } 
 }; 
 
 @SuppressLint("HandlerLeak") 
 Handler handler = new Handler() { 
 
 @Override 
 public void handleMessage(Message msg) { 
  super.handleMessage(msg); 
  switch (msg.what) { 
  case 1: 
  finish(); 
  break; 
  } 
 } 
 }; 
 private String remainopportunityStr; 
 private String firstPassward; 
 
 protected void onDestroy() { 
 super.onDestroy(); 
 } 
 protected void onStop() { 
 super.onStop(); 
 } 
} 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기