天天看點

Android手勢密碼LockPatternView、LockPasswordUtils、LockPatternUtils等分析

Android手勢密碼LockPatternView、LockPasswordUtils、LockPatternUtils

在使用别人寫的這個手勢密碼的時候,我們通常是有自己的需求,可能這裡的代碼很多也很複雜,有沒有什麼很多注釋,要把整個代碼弄明白是要花很多時間而且基礎要非常好的,可能在趕項目的時候沒有時間去研究,下面我列出一些關鍵的地方,友善修改代碼。

1.在建立手勢密碼的時候CreateGesturePasswordActivity中

下面是手勢密碼設定的時候界面的一些變化,和要處理的一些事情

switch (mUiStage) {
case Introduction:
	//剛進去的時候  // 重試的時候
	mLockPatternView.clearPattern();
	break;
case HelpScreen:
	//剛進去的時候
	mLockPatternView.setPattern(DisplayMode.Animate, mAnimatePattern);
	break;
case ChoiceTooShort:
	//設定短了松開
	mLockPatternView.setDisplayMode(DisplayMode.Wrong);
	postClearPatternRunnable();
	break;
case FirstChoiceValid:
	//設定對了松開	
	break;
case NeedToConfirm:
	//點選繼續
	mLockPatternView.clearPattern();
	updatePreviewViews();
	break;
case ConfirmWrong:
	//第二次和第一次的不一樣
	mLockPatternView.setDisplayMode(DisplayMode.Wrong);
	postClearPatternRunnable();
	break;
case ChoiceConfirmed:
	//第二次和第一次的一樣
	break;
}
           

2.在LockPatternView中設定手勢密碼預設、正确和錯誤的顯示圖檔,其實那個 圈圈的大小是根據你的圖檔的大小變化的

注意: 這裡的三個圖檔的大小是一樣的

畫手勢的預設圖檔
mBitmapCircleDefault = getBitmapFor(R.drawable.gesture_pattern_item_bg);
畫手勢正确的圖檔
mBitmapCircleGreen = getBitmapFor(R.drawable.gesture_pattern_selected);
畫手勢錯誤的圖檔
mBitmapCircleRed = getBitmapFor(R.drawable.gesture_pattern_selected_wrong);
           

3.在LockPatternView中設定手勢密碼連接配接線的顔色紅色和黃色

這裡的顔色你可能要改變多個地方的顔色

設定黃色
mPathPaint.setColor(Color.RED);
設定紅色
mPathPaint.setColor(Color.YELLOW);
           

4.判斷有沒有手勢密碼

if (App.getInstance().getLockPatternUtils().savedPatternExists()) {
	//如果這裡是true就是有手勢密碼,false就是沒有手勢密碼
}
           

5.清除手勢密碼重新建立

String LOCK_PATTERN_FILE = "gesture.key";
String dataSystemDirectory = context.getFilesDir() .getAbsolutePath(); 
File file = new File(dataSystemDirectory , LOCK_PATTERN_FILE);
if (file.isFile()) { file.delete(); } 
然後跳轉到UnlockGesturePasswordActivity.class不到CreateGesturePasswordActivity.java的哪裡就可以了 
           

6.手勢密碼頂部記錄區,這裡的小圓圈是可以在這裡設定大小和間距的,這裡是比較麻煩的區域,需要你非常的細心設定。

注意: 這裡的背景切圖一定要做好,要美工給你标出大小和間距,

<LinearLayout
android:id="@+id/gesturepwd_setting_preview"
android:layout_width="40.0dip"
android:layout_height="40.0dip"
android:layout_gravity="center_horizontal"
android:background="@drawable/gesture_create_grid_bg"
android:orientation="vertical"
android:padding="5.0dip" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="6.0dip" >

        <View
            android:id="@+id/gesturepwd_setting_preview_0"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:background="@drawable/trans" />

        <View
            android:id="@+id/gesturepwd_setting_preview_1"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:layout_marginLeft="6.0dip"
            android:background="@drawable/trans" />

        <View
            android:id="@+id/gesturepwd_setting_preview_2"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:layout_marginLeft="6.0dip"
            android:background="@drawable/trans" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="6.0dip"
        android:layout_marginTop="6.0dip" >

        <View
            android:id="@+id/gesturepwd_setting_preview_3"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:background="@drawable/trans" />

        <View
            android:id="@+id/gesturepwd_setting_preview_4"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:layout_marginLeft="6.0dip"
            android:background="@drawable/trans" />

        <View
            android:id="@+id/gesturepwd_setting_preview_5"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:layout_marginLeft="6.0dip"
            android:background="@drawable/trans" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="6.0dip"
        android:layout_marginTop="6.0dip" >

        <View
            android:id="@+id/gesturepwd_setting_preview_6"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:background="@drawable/trans" />

        <View
            android:id="@+id/gesturepwd_setting_preview_7"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:layout_marginLeft="6.0dip"
            android:background="@drawable/trans" />

        <View
            android:id="@+id/gesturepwd_setting_preview_8"
            android:layout_width="6.0dip"
            android:layout_height="6.0dip"
            android:layout_marginLeft="6.0dip"
            android:background="@drawable/trans" />
    </LinearLayout>
</LinearLayout>
           

7.效果圖

Android手勢密碼LockPatternView、LockPasswordUtils、LockPatternUtils等分析

至于底部按鈕、提示文字、背景顔色等都是很容易解決的

積累一些吧,就算你不是很懂這裡的代碼,你也可以做你的手勢密碼。

源碼下載下傳: http://download.csdn.net/detail/pcaxb/8747015

繼續閱讀