Android 사용자 정의 UI 제스처 암호 개선 버 전 원본 다운로드

이전 글 의 밑받침 아래 안 드 로 이 드 제스처 비밀번호,소스 코드 다운로드,놓 치지 마 세 요.

원본 다운로드:http://xiazai.jb51.net/201610/yuanma/androidLock(jb51.net).rar
첫 번 째 그림 의 레이아웃 파일 을 먼저 봅 니 다.
activity_main.xml

<LinearLayout 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" 
 android:orientation="vertical" 
 android:gravity="center_horizontal" > 
 
 <TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="40dip" 
 android:textSize="24sp" 
 android:text="    " /> 
 
 <Button 
 android:id="@+id/btn_set_lockpattern" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="50dip" 
 android:text="      " /> 
 
 <Button 
 android:id="@+id/btn_verify_lockpattern" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="10dip" 
 android:text="      " /> 
 
 <TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="10dip" 
 android:text="      2"/> 
 
</LinearLayout> 
위 에 배 치 된 자바 코드 를 보 세 요.
MainActivity

package com.wujay.fund; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity implements OnClickListener { 
 private Button mBtnSetLock; 
 private Button mBtnVerifyLock; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 setUpView(); 
 setUpListener(); 
 } 
 
 private void setUpView() { 
 mBtnSetLock = (Button) findViewById(R.id.btn_set_lockpattern); 
 mBtnVerifyLock = (Button) findViewById(R.id.btn_verify_lockpattern); 
 } 
 
 private void setUpListener() { 
 mBtnSetLock.setOnClickListener(this); 
 mBtnVerifyLock.setOnClickListener(this); 
 } 
 
 @Override 
 public void onClick(View v) { 
 switch (v.getId()) { 
 case R.id.btn_set_lockpattern: 
  startSetLockPattern(); 
  break; 
 case R.id.btn_verify_lockpattern: 
  startVerifyLockPattern(); 
  break; 
 default: 
  break; 
 } 
 } 
 
 private void startSetLockPattern() { 
 Intent intent = new Intent(MainActivity.this, GestureEditActivity.class); 
 startActivity(intent); 
 } 
 
 private void startVerifyLockPattern() { 
 Intent intent = new Intent(MainActivity.this, GestureVerifyActivity.class); 
 startActivity(intent); 
 } 
} 
도구 류 와 bean 류 를 따로 보 세 요.
AppUtil

package com.wujay.fund.common; 
 
import android.content.Context; 
import android.view.WindowManager; 
 
public class AppUtil { 
 public static int[] getScreenDispaly(Context context) { 
 WindowManager windowManager = (WindowManager) context 
  .getSystemService(Context.WINDOW_SERVICE); 
 int width = windowManager.getDefaultDisplay().getWidth();//         
 int height = windowManager.getDefaultDisplay().getHeight();//         
 int result[] = { width, height }; 
 return result; 
 } 
} 
9 궁 격 단위 bean 류
GesturePoint

package com.wujay.fund.entity; 
 
 
import com.wujay.fund.R; 
import com.wujay.fund.common.Constants; 
 
import android.widget.ImageView; 
 
public class GesturePoint { 
 /** 
 *     
 */ 
 private int pointState; 
 
 /**     Point       , 1  (     1  )*/ 
 private int num; 
 private int leftX; 
 private int rightX; 
 private int topY; 
 private int bottomY; 
 private ImageView image; 
 
 private int centerX; 
 private int centerY; 
 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) { 
 pointState = state; 
 switch (state) { 
 case Constants.POINT_STATE_NORMAL: 
  this.image.setBackgroundResource(R.drawable.gesture_node_normal); 
  break; 
 case Constants.POINT_STATE_SELECTED: 
  this.image.setBackgroundResource(R.drawable.gesture_node_pressed); 
  break; 
 case Constants.POINT_STATE_WRONG: 
  this.image.setBackgroundResource(R.drawable.gesture_node_wrong); 
  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 + "]"; 
 } 
} 
점 마다 3 가지 상 태 를 볼 게 요.
Constants

package com.wujay.fund.common; 

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; //      

} 


다시 보 니 비밀 번 호 를 그 리 는 인터페이스 입 니 다.
activity_gesture_edit.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="#48423D" > 
 
 <RelativeLayout 
 android:id="@+id/top_layout" 
 android:layout_width="match_parent" 
 android:layout_height="46dip" 
 android:background="#000000" 
 android:paddingLeft="20dip" 
 android:paddingRight="20dip" 
 android:layout_alignParentTop="true" > 
 
 <TextView 
  android:id="@+id/text_title" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_centerInParent="true" 
  android:gravity="center" 
  android:text="@string/setup_gesture_code" 
  android:textSize="20sp" 
  android:textColor="#ffffff" /> 
 </RelativeLayout> 
 
 <LinearLayout 
 android:id="@+id/gesture_tip_layout" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_below="@id/top_layout" 
 android:gravity="center" 
 android:orientation="vertical" > 
 
 
 <TextView 
  android:id="@+id/text_tip" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:gravity="center_horizontal" 
  android:text="@string/set_gesture_pattern" 
  android:textColor="#F98F12" 
  android:layout_marginTop="10dip" /> 
 </LinearLayout> 
 
 <FrameLayout 
 android:id="@+id/gesture_container" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@id/gesture_tip_layout" 
 android:layout_gravity="center_horizontal" 
 android:layout_marginTop="20dip" > 
 </FrameLayout> 
 
 <TextView 
 android:id="@+id/text_reset" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_horizontal" 
 android:layout_below="@id/gesture_container" 
 android:layout_marginTop="20dip" 
 android:text="@string/set_gesture_pattern_reason" 
 android:textColor="#816E6A" /> 
 
</RelativeLayout> 
다시 보 니 비밀 번 호 를 그 리 는 클래스 입 니 다.
GestureEditActivity

package com.wujay.fund; 
 
import com.wujay.fund.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.text.Html; 
import android.text.TextUtils; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.FrameLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.wujay.fund.widget.GestureContentView; 
import com.wujay.fund.widget.GestureDrawline.GestureCallBack; 
 
/** 
 *          
 */ 
public class GestureEditActivity extends Activity implements OnClickListener { 
 // 2               
 private TextView mTextTip; 
 //          
 private FrameLayout mGestureContainer; 
 private GestureContentView mGestureContentView; 
 //          
 private TextView mTextReset; 
 //             
 private boolean mIsFirstInput = true; 
 //          ,      
 private String mFirstPassword = null; 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_gesture_edit); 
 setUpViews(); 
 setUpListeners(); 
 } 
 
 /** 
 *                                    4 
 */ 
 private boolean isInputPassValidate(String inputPassword) { 
 if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) { 
  return false; 
 } 
 return true; 
 } 
 
 private void setUpViews() { 
 //          
 mTextReset = (TextView) findViewById(R.id.text_reset); 
 //        
 mTextReset.setClickable(false); 
 // 2               
 mTextTip = (TextView) findViewById(R.id.text_tip); 
 //          
 mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container); 
 /** 
  *            viewGroup GestureContentView(Context context, boolean 
  * isVerify, String passWord, GestureCallBack callBack) 
  */ 
 mGestureContentView = new GestureContentView(this, false, "", 
  new GestureCallBack() { 
   @Override 
   public void onGestureCodeInput(String inputCode) { 
   //          --     null。        4  
   if (!isInputPassValidate(inputCode)) { 
    mTextTip.setText(Html 
     .fromHtml("<font color='#c70c1e'>    4  ,      </font>")); 
    //          
    mGestureContentView.clearDrawlineState(0L); 
    return; 
   } 
   if (mIsFirstInput) { 
    //        --          ,          
    mFirstPassword = inputCode; 
    //         ,         
    mGestureContentView.clearDrawlineState(0L); 
    //                  
    mTextReset.setClickable(true); 
    mTextReset 
     .setText(getString(R.string.reset_gesture_code)); 
   } else { 
    if (inputCode.equals(mFirstPassword)) { 
    Toast.makeText(GestureEditActivity.this, 
     "    ", Toast.LENGTH_SHORT).show(); 
    mGestureContentView.clearDrawlineState(0L); 
    GestureEditActivity.this.finish(); 
    } else { 
    mTextTip.setText(Html 
     .fromHtml("<font color='#c70c1e'>         ,     </font>")); 
    //        
    Animation shakeAnimation = AnimationUtils 
     .loadAnimation( 
      GestureEditActivity.this, 
      R.anim.shake); 
    mTextTip.startAnimation(shakeAnimation); 
    //       ,1.5     
    mGestureContentView.clearDrawlineState(1300L); 
    } 
   } 
   mIsFirstInput = false; 
   } 
 
   @Override 
   public void checkedSuccess() { 
 
   } 
 
   @Override 
   public void checkedFail() { 
 
   } 
  }); 
 //                 
 mGestureContentView.setParentView(mGestureContainer); 
 } 
 
 /*****************************************************/ 
 private void setUpListeners() { 
 mTextReset.setOnClickListener(this); 
 } 
 
 @Override 
 public void onClick(View v) { 
 switch (v.getId()) { 
 case R.id.text_reset: 
  mIsFirstInput = true; 
  mTextTip.setText(getString(R.string.set_gesture_pattern)); 
  break; 
 default: 
  break; 
 } 
 } 
 
} 
GestureContentView

package com.wujay.fund.widget; 
 
import java.util.ArrayList; 
import java.util.List; 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import com.wujay.fund.R; 
import com.wujay.fund.common.AppUtil; 
import com.wujay.fund.entity.GesturePoint; 
import com.wujay.fund.widget.GestureDrawline.GestureCallBack; 
 
/** 
 *         
 */ 
public class GestureContentView extends ViewGroup { 
 private GestureDrawline gestureDrawline; 
 
 /************************************************************************ 
 *   9 ImageView   ,    
 * 
 * @param context 
 * @param isVerify 
 *            
 * @param passWord 
 *         
 * @param callBack 
 *            
 */ 
 private int[] screenDispaly; 
 //        3  
 private int blockWidth; 
 // 9       
 private List<GesturePoint> list; 
 //    
 private Context context; 
 //          
 private boolean isVerify; 
 
 public GestureContentView(Context context, boolean isVerify, 
  String passWord, GestureCallBack callBack) { 
 super(context); 
 //        
 screenDispaly = AppUtil.getScreenDispaly(context); 
 //        1/3 
 blockWidth = screenDispaly[0] / 3; 
 this.list = new ArrayList<GesturePoint>(); 
 this.context = context; 
 this.isVerify = isVerify; 
 //   9    
 addChild(); 
 //           view 
 gestureDrawline = new GestureDrawline(context, list, isVerify, 
  passWord, callBack); 
 } 
 
 /** 
 *   9       
 */ 
 //     2             
 private int baseNum = 6; 
 
 private void addChild() { 
 for (int i = 0; i < 9; i++) { 
  ImageView image = new ImageView(context); 
  image.setBackgroundResource(R.drawable.gesture_node_normal); 
  this.addView(image); 
  invalidate(); 
  //    ---012 0 ,345 1, 678 2-- 
  int row = i / 3; 
  //    ---012 012 , 345 012 , 678 012 
  int col = i % 3; 
  //          
  int leftX = col * blockWidth + blockWidth / baseNum; 
  int topY = row * blockWidth + blockWidth / baseNum; 
  int rightX = col * blockWidth + blockWidth - blockWidth / baseNum; 
  int bottomY = row * blockWidth + blockWidth - blockWidth / baseNum; 
  //        
  GesturePoint p = new GesturePoint(leftX, rightX, topY, bottomY, 
   image, i + 1); 
  //   9      
  this.list.add(p); 
 } 
 } 
 
 /** 
 *                 
 */ 
 
 public void setParentView(ViewGroup parent) { 
 //         
 int width = screenDispaly[0]; 
 //           --         
 LayoutParams layoutParams = new LayoutParams(width, width); 
 //           --         
 this.setLayoutParams(layoutParams); 
 //              
 gestureDrawline.setLayoutParams(layoutParams); 
 parent.addView(gestureDrawline); 
 parent.addView(this); 
 } 
 
 /**************************************         ****************************************/ 
 @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); 
  //            
  v.layout(col * blockWidth + blockWidth / baseNum, row * blockWidth 
   + blockWidth / baseNum, col * blockWidth + blockWidth 
   - blockWidth / baseNum, row * blockWidth + blockWidth 
   - blockWidth / baseNum); 
 } 
 } 
 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 //        view    
 for (int i = 0; i < getChildCount(); i++) { 
  View v = getChildAt(i); 
  v.measure(widthMeasureSpec, heightMeasureSpec); 
 } 
 } 
 
 /** 
 *         ---                delayTime    
 * 
 * @param delayTime 
 */ 
 public void clearDrawlineState(long delayTime) { 
 gestureDrawline.clearDrawlineState(delayTime); 
 } 
 
} 
GestureDrawline

package com.wujay.fund.widget; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import com.wujay.fund.common.AppUtil; 
import com.wujay.fund.common.Constants; 
import com.wujay.fund.entity.GesturePoint; 
import android.content.Context; 
import android.graphics.Bitmap; 
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; 
import android.widget.Toast; 
 
/** 
 *          
 * 
 */ 
public class GestureDrawline extends View { 
 private int mov_x;//        
 private int mov_y; 
 
 private Map<String, GesturePoint> autoCheckPointMap;//          
 private boolean isDrawEnable = true; //        
 
 /********************************************************************** 
 *      
 */ 
 private int[] screenDispaly; 
 private Paint paint;//      
 private Canvas canvas;//    
 private Bitmap bitmap;//    
 private List<GesturePoint> list;//     view      
 private List<Pair<GesturePoint, GesturePoint>> lineList;//        
 private StringBuilder passWordSb; 
 private boolean isVerify; 
 private String passWord; 
 private GestureCallBack callBack; 
 
 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(10);//   5   
 paint.setColor(Color.rgb(245, 142, 33));//          
 paint.setAntiAlias(true);//       
 
 this.list = list; 
 this.lineList = new ArrayList<Pair<GesturePoint, GesturePoint>>(); 
 
 initAutoCheckPointMap(); 
 this.callBack = callBack; 
 
 //         
 this.isVerify = isVerify; 
 this.passWordSb = new StringBuilder(); 
 this.passWord = passWord; 
 } 
 
 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, paint); 
 
 } 
 
 /** 
 *                       Point    
 * 
 * @return       ,   null,                   
 */ 
 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 void clearScreenAndDrawList() { 
 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
 for (Pair<GesturePoint, GesturePoint> pair : lineList) { 
  // drawLine(float startX, float startY, float stopX, float stopY, 
  // Paint paint) 
  canvas.drawLine(pair.first.getCenterX(), pair.first.getCenterY(), 
   pair.second.getCenterX(), pair.second.getCenterY(), paint);//    
 } 
 } 
 
 /** 
 *             
 * 
 * @param pointStart 
 * @param pointEnd 
 * @return 
 */ 
 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 GesturePoint currentPoint; 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
 if (isDrawEnable == false) { 
  //              --         ,              ,         
  return true; 
 } 
 paint.setColor(Color.rgb(245, 142, 33));//          
 switch (event.getAction()) { 
 case MotionEvent.ACTION_DOWN: 
  //      ,       xy   
  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); 
  passWordSb.append(currentPoint.getNum()); 
  } 
  invalidate(); 
  break; 
 case MotionEvent.ACTION_MOVE: 
  //          ,          --          
  clearScreenAndDrawList(); 
 
  //                 
  GesturePoint pointAt = getPointAt((int) event.getX(), 
   (int) event.getY()); 
  //                 
  if (currentPoint == null && pointAt == null) { 
  return true; 
  } else {//                     
  if (currentPoint == null) {//       point    null 
   //     ,             currentPoint 
   currentPoint = pointAt; 
   //  currentPoint        true; 
   currentPoint.setPointState(Constants.POINT_STATE_SELECTED); 
   passWordSb.append(currentPoint.getNum()); 
  } 
  } 
  if (pointAt == null || currentPoint.equals(pointAt)) { 
  //             ,                      
  //             ,             
  canvas.drawLine(currentPoint.getCenterX(), 
   currentPoint.getCenterY(), event.getX(), event.getY(), 
   paint);//    
  } else { 
  //                       
  //             ,             
  canvas.drawLine(currentPoint.getCenterX(), 
   currentPoint.getCenterY(), pointAt.getCenterX(), 
   pointAt.getCenterY(), paint);//    
  pointAt.setPointState(Constants.POINT_STATE_SELECTED); 
 
  //             
  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); 
   //      point; 
   currentPoint = pointAt; 
  } else { 
   Pair<GesturePoint, GesturePoint> pair = new Pair<GesturePoint, GesturePoint>( 
    currentPoint, pointAt); 
   lineList.add(pair); 
   passWordSb.append(pointAt.getNum()); 
   //      point; 
   currentPoint = pointAt; 
  } 
  } 
  invalidate(); 
  break; 
 case MotionEvent.ACTION_UP://          
  if (isVerify) { 
  //        
  //          ,            
  if (passWord.equals(passWordSb.toString())) { 
   //                     
   callBack.checkedSuccess(); 
  } else { 
   //                。 
   callBack.checkedFail(); 
  } 
  } else { 
  callBack.onGestureCodeInput(passWordSb.toString()); 
  } 
  break; 
 default: 
  break; 
 } 
 return true; 
 } 
 
 /************************************ 
 *     /          
 */ 
 private void drawErrorPathTip() { 
 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
 paint.setColor(Color.rgb(154, 7, 21));//          
 for (Pair<GesturePoint, GesturePoint> pair : lineList) { 
  pair.first.setPointState(Constants.POINT_STATE_WRONG); 
  pair.second.setPointState(Constants.POINT_STATE_WRONG); 
  canvas.drawLine(pair.first.getCenterX(), pair.first.getCenterY(), 
   pair.second.getCenterX(), pair.second.getCenterY(), paint);//    
 } 
 invalidate(); 
 } 
 
 /** 
 *              
 * 
 * @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); 
  } 
  invalidate(); 
  isDrawEnable = true; 
 } 
 } 
 
 public interface GestureCallBack { 
 
 /** 
  *     /        
  */ 
 public abstract void onGestureCodeInput(String inputCode); 
 
 /** 
  *                   
  */ 
 public abstract void checkedSuccess(); 
 
 /** 
  *                    
  */ 
 public abstract void checkedFail(); 
 } 
 
} 
다음은 비밀번호 잠 금 을 검증 하 는 레이아웃 을 살 펴 보 겠 습 니 다.
activity_gesture_verify.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" 
 android:background="#48443c" > 
 
 
 <LinearLayout 
 android:id="@+id/gesture_tip_layout" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_below="@id/top_layout" 
 android:orientation="vertical" 
 android:paddingTop="20dip" > 
 
 
 <TextView 
  android:id="@+id/text_tip" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginTop="10dip" 
  android:gravity="center_horizontal" 
  android:textColor="#000000" 
  android:visibility="invisible" /> 
 </LinearLayout> 
 
 <FrameLayout 
 android:id="@+id/gesture_container" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@id/gesture_tip_layout" > 
 </FrameLayout> 
 
 
</RelativeLayout> 
암 호 를 검증 하 는 코드 구현 클래스
GestureVerifyActivity

package com.wujay.fund; 
 
import com.wujay.fund.R; 
import com.wujay.fund.widget.GestureContentView; 
import com.wujay.fund.widget.GestureDrawline.GestureCallBack; 
import android.app.Activity; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Html; 
import android.text.TextUtils; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
 
/** 
 *     /     
 */ 
public class GestureVerifyActivity extends Activity{ 
 private TextView mTextTip; 
 private FrameLayout mGestureContainer; 
 private GestureContentView mGestureContentView; 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_gesture_verify); 
 setUpViews(); 
 } 
 
 
 private void setUpViews() { 
 //          
 mTextTip = (TextView) findViewById(R.id.text_tip); 
 mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container); 
  
 //            viewGroup 
 mGestureContentView = new GestureContentView(this, true, "12589", 
  new GestureCallBack() { 
 
   @Override 
   public void onGestureCodeInput(String inputCode) { 
 
   } 
 
   @Override 
   public void checkedSuccess() { 
   mGestureContentView.clearDrawlineState(0L); 
   Toast.makeText(GestureVerifyActivity.this, "    ", 1000).show(); 
   GestureVerifyActivity.this.finish(); 
   } 
 
   @Override 
   public void checkedFail() { 
   mGestureContentView.clearDrawlineState(1300L); 
   mTextTip.setVisibility(View.VISIBLE); 
   mTextTip.setText(Html 
    .fromHtml("<font color='#c70c1e'>    </font>")); 
   //        
   Animation shakeAnimation = AnimationUtils.loadAnimation(GestureVerifyActivity.this, R.anim.shake); 
   mTextTip.startAnimation(shakeAnimation); 
   } 
  }); 
 //                 
 mGestureContentView.setParentView(mGestureContainer); 
 } 
 
 
} 
shake.xml

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
android:fromXDelta="0" 
android:toXDelta="10" 
android:duration="120" 
android:interpolator="@android:anim/cycle_interpolator" 
android:repeatMode="restart" 
android:repeatCount="2"/> 
추천 글:
Android 사용자 정의 UI 제스처 비밀번호 심 플 버 전
Android 사용자 정의 UI 제스처 암호 개선 버 전
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기