設(shè)置密碼:
要設(shè)置密碼 就要在初始化點(diǎn)的時(shí)候進(jìn)行設(shè)置,我們通過(guò)自然數(shù)的方式進(jìn)行設(shè)置
當(dāng)點(diǎn)初始化完成 ,我們遍歷點(diǎn)的二維數(shù)組,進(jìn)行設(shè)置
//設(shè)置密碼
int index = 1;
for(Point[] points : this.points){
for(Point point : points){
point.index = index;
index++;
}
}
設(shè)置圖案監(jiān)聽(tīng)器
/**
* 圖案監(jiān)聽(tīng)器
*/
public static interface onPatternChangeListener{
/**
* 繪制圖案的密碼
* @param passwordstr 密碼
*/
void onPatternChange(String passwordstr);
/**
* 監(jiān)聽(tīng)是否重新繪制
* @param isClick
*/
void onPatternStart(boolean isClick);
}
/**
* 設(shè)置監(jiān)聽(tīng)
* @param onPatternChangeListener
*/
public void setPatternChangeListener(onPatternChangeListener onPatternChangeListener){
if(onPatternChangeListener!=null){
this.onPatternChangeListener = onPatternChangeListener;
}
}
在繪制完成后,通過(guò)監(jiān)聽(tīng)器獲取密碼,并且返回出去,繪制錯(cuò)誤也要做出相應(yīng)的提示
//如果繪制完成,判斷繪制選中點(diǎn)的結(jié)果
if (isFinsh) {
if (pointList.size() == 1) {
//如果只選中一個(gè)點(diǎn),條件不成立,將集合清空
resetPoint();
} else if (pointList.size() > 1 && pointList.size() < 5) {
//繪制錯(cuò)誤的狀態(tài),提示圖案太簡(jiǎn)單
errorPoint();
if(onPatternChangeListener!=null){
//圖案太簡(jiǎn)單,返回null
onPatternChangeListener.onPatternChange(null);
}
} else if (pointList.size() >= 5) {
//繪制完成
String password = "";
if(onPatternChangeListener!=null){
//遍歷繪制點(diǎn)的集合
for(int i = 0;i<pointList.size();i++){
//密碼拼接
password = password+pointList.get(i).index;
}
onPatternChangeListener.onPatternChange(password);
}
}
}
在Activity中監(jiān)聽(tīng)做出對(duì)應(yīng)的判斷
Activity布局
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.zzj.lockviewdemo.MainActivity">
<TextView
android:id="@+id/act_main_lockhint"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="請(qǐng)繪制圖案"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:gravity="center_horizontal"
/>
<com.zzj.lockviewdemo.views.LockView
android:id="@+id/act_main_lockview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
實(shí)現(xiàn)監(jiān)聽(tīng)接口,進(jìn)行判斷
@Override
public void onPatternChange(String passwordstr) {
if(passwordstr!=null){
lockHint.setText(passwordstr);
}else {
lockHint.setText("至少五個(gè)圖案");
}
}
@Override
public void onPatternStart(boolean isClick) {
if(isClick){
lockHint.setText("請(qǐng)繪制圖案");
}
}
最后效果圖:
Screenshot_1479457087.png
Screenshot_1479457102.png
Screenshot_1479457109.png
有關(guān)文章:
android開(kāi)發(fā)圖案解鎖學(xué)習(xí)記錄一(九宮格的繪制):http://www.lxweimin.com/p/47c731df655a
android開(kāi)發(fā)圖案解鎖學(xué)習(xí)記錄二(九宮格間連線(xiàn)時(shí)的onTouchEvent事件的處理):http://www.lxweimin.com/p/931afdc0c1c3
圖案鎖源碼:http://pan.baidu.com/s/1kVK2zll