Android 5秒學(xué)會(huì)使用手勢(shì)解鎖功能
Android手勢(shì)解鎖
本文講述的是一個(gè)手勢(shì)解鎖的庫(kù),可以定制顯示隱藏宮格點(diǎn)、路徑、并且?guī)в行【艑m格顯示圖,和震動(dòng)!讓你學(xué)會(huì)使用這個(gè)簡(jiǎn)單,高效的庫(kù)!
先來(lái)一波效果效果展示:
手勢(shì)解鎖效果
今天給大家介紹的是本人良心制作的一個(gè)手勢(shì)解鎖開源庫(kù),大家有什么建議和想法都可以發(fā)到我的郵箱: diosamolee2014@gmail.com 或者評(píng)論,我會(huì)為大家提供我力所能及的幫助!
GitHub地址:
https://github.com/Diosamo/Gesture_Lock
添加依賴:
添加的gradle
第一步:
Add it in your root build.gradle at the end of repositories:
repositories {
maven { url "https://dl.bintray.com/freecoders/GestureLock"
}
}
第二步:
Step 2. Add the dependency
dependencies {
compile 'com.gesturelock:GestureLock:1.0'
}
布局使用:
下面是我測(cè)試時(shí)寫的完整的所有布局:(懶人直接copy)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="50dp">
<TextView
android:textColor="#434242"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="繪制圖案"
android:paddingBottom="10dp"/>
<com.gestruelock.IndicatorLockView
android:id="@+id/lockviewIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:indicator_selected="@drawable/indicator_selected"
app:indicator_unselected="@drawable/indicator_unselected"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text=""
android:paddingTop="20dp"
/>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="30dp"
>
<com.gestruelock.ExpandLockView
android:id="@+id/lockviewExpand"
android:layout_width="280dp"
android:layout_height="280dp"
app:lock_selected1="@drawable/gusture_icon_left"
app:lock_selected2="@drawable/gusture_icon_center"
app:lock_selected3="@drawable/gusture_icon_right"
app:lock_trackColor="#ff0432"
app:lock_selected_error="@drawable/circle_error"
app:lock_unselected="@drawable/gusture_icon_default"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
到這里小伙伴直接運(yùn)行,就可以看到手勢(shì)解鎖的布局,大家也可以根據(jù)自己的需求去改變這個(gè)xml.
com.gestruelock.IndicatorLockView : 路徑顯示圖,小的九個(gè)點(diǎn) com.gestruelock.ExpandLockView: 手勢(shì)解鎖的九宮格
使用配置:
下面的代碼是在Activity中直接使用的代碼:(懶人直接copy)
public class MainActivity extends AppCompatActivity implements ExpandLockView.OnLockPanelListener, ExpandLockView.OnUpdateIndicatorListener, ExpandLockView.OnUpdateMessageListener, ExpandLockView.OnFinishDrawPasswordListener {
private ExpandLockView mLockviewExpand;
private IndicatorLockView lockviewIndicator;
private TextView tvMessage;
private Animation mShakeAnimal;
private Vibrator mVibrator;
//返回信息如果是正確的
private String succeeMsg="再次輸入密碼,密碼已設(shè)置,密碼正確,密碼正確,請(qǐng)輸入新密碼";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLockviewExpand = (ExpandLockView) findViewById(R.id.lockviewExpand);
tvMessage = (TextView) findViewById(R.id.tvMessage);
lockviewIndicator = (IndicatorLockView) findViewById(R.id.lockviewIndicator);
mVibrator =(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE); //震動(dòng)
// mLockviewExpand.getPaintL().setStrokeWidth(20); //獲取paint 修改連接線段的樣式
// mLockviewExpand.setLock_trackColor(0xff0000); //給路徑設(shè)置不同顏色
//加載動(dòng)畫資源文件
mShakeAnimal = AnimationUtils.loadAnimation(this, R.anim.shake);
mLockviewExpand.setActionMode(0);//set mode 設(shè)置手勢(shì)密碼
// mLockviewExpand.setActionMode(1);//set mode 驗(yàn)證手勢(shì)密碼
// mLockviewExpand.setActionMode(2);//set mode 更換手勢(shì)密碼
// mLockviewExpand.setHiddenTrack(true); //隱藏軌跡和按鈕
mLockviewExpand.setShowError(true); //顯示失敗視圖
// mLockviewExpand.setLockTime(2);//設(shè)置顯示的鎖住的時(shí)間
//設(shè)置各種回調(diào)事件
mLockviewExpand.setOnLockPanelListener(this);
mLockviewExpand.setOnUpdateIndicatorListener(this);
mLockviewExpand.setOnUpdateMessageListener(this);
mLockviewExpand.setOnFinishDrawPasswordListener(this);
}
@Override
public void initData() {
}
//密碼盤被鎖住發(fā)生的回調(diào)
@Override
public void onLockPanel() {
}
//更新小點(diǎn)顯示圖
@Override
public void onUpdateIndicator() {
if (mLockviewExpand.getPointTrace().size() > 0) {
lockviewIndicator.setPath(mLockviewExpand.getPointTrace());
}
}
//返回信息如果是正確的
@Override
public void onUpdateMessage(String message) {
if (succeeMsg.contains(message)){
tvMessage.setTextColor(0xff434242);//設(shè)置提示文字顏色
}else {//Error
tvMessage.setTextColor(0xffe44d4d);
tvMessage.startAnimation(mShakeAnimal); //動(dòng)畫效果
}
tvMessage.setText(message);
}
//vibration 震動(dòng)對(duì)應(yīng)的接口
@Override
public void vibration(String time) {
if ("long".equals(time)){
mVibrator.vibrate(new long[]{50,200},-1);//長(zhǎng)震動(dòng)
}else {
mVibrator.vibrate(new long[]{50,50},-1);//震動(dòng)
}
}
//設(shè)置密碼成功
@Override
public void onSetPassword() {
Toast.makeText(mContext, "密碼設(shè)置成功", Toast.LENGTH_SHORT).show();
finish();
}
//解開密碼鎖成功
@Override
public void onOpenLock() {
Toast.makeText(mContext, "成功解鎖", Toast.LENGTH_SHORT).show();
finish();
}
/* 禁止返回按鈕的點(diǎn)擊 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()== KeyEvent.ACTION_DOWN &&activityNum == 0) {
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
}
手勢(shì)解鎖一共有3種模式,對(duì)應(yīng)設(shè)置密碼,驗(yàn)證密碼,修改密碼:
mLockviewExpand.setActionMode(0);//set mode 設(shè)置手勢(shì)密碼 mLockviewExpand.setActionMode(1);//set mode 驗(yàn)證手勢(shì)密碼 mLockviewExpand.setActionMode(2);//set mode 更換手勢(shì)密碼
對(duì)應(yīng)回調(diào)接口設(shè)置:
mLockviewExpand.setOnLockPanelListener(this);// 手勢(shì)解鎖次數(shù)超出后,鎖定解鎖的回調(diào) mLockviewExpand.setOnUpdateIndicatorListener(this);//手勢(shì)完成后的回調(diào),設(shè)置上面的路徑顯示圖 mLockviewExpand.setOnUpdateMessageListener(this);//手勢(shì)完成后,返回提示的信息 mLockviewExpand.setOnFinishDrawPasswordListener(this);//手勢(shì)解鎖成功,密碼設(shè)置成功的回調(diào)
其他配置:
mLockviewExpand.getPaintL().setStrokeWidth(20); //獲取paint 修改連接線段的樣式 mLockviewExpand.setLock_trackColor(0xff0000); //給路徑設(shè)置不同顏色 mLockviewExpand.setHiddenTrack(true); //隱藏軌跡和按鈕 mLockviewExpand.setShowError(true); //顯示失敗視圖 mLockviewExpand.setLockTime(2);//設(shè)置顯示的鎖住的時(shí)間 xml配置(com.gestruelock.ExpandLockView 只針對(duì)這個(gè)控件):
圖片的指定就是在這里,可以設(shè)置左邊,中間和右邊的圖片,如果一樣就全部指定同一張圖片,庫(kù)里面自帶上面圖片顯示效果的圖片資源!
app:lock_selected1="@drawable/left" //設(shè)置最左邊圖片 app:lock_selected2="@drawable/center" //設(shè)置最中間圖片 app:lock_selected3="@drawable/right" //設(shè)置最右邊圖片 app:lock_trackColor="#04ff9b" //設(shè)置軌跡顏色 app:lock_selected_error="@drawable/circle_error" //設(shè)置錯(cuò)誤圖片 app:lock_unselected="@drawable/gusture_icon_default" //設(shè)置未選中圖片
總結(jié):
以上所述是小編給大家介紹的Android 5秒學(xué)會(huì)使用手勢(shì)解鎖功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android?Studio實(shí)現(xiàn)購(gòu)買售賣系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)購(gòu)買售賣系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
android使用flutter的ListView實(shí)現(xiàn)滾動(dòng)列表的示例代碼
現(xiàn)如今打開一個(gè) App,比如頭條、微博,都會(huì)有長(zhǎng)列表,那么android使用flutter的ListView滾動(dòng)列表如何實(shí)現(xiàn),本文就來(lái)詳細(xì)的介紹一下,感興趣的同學(xué)可以來(lái)了解一下2018-12-12
Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)
下面小編就為大家?guī)?lái)一篇Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
android 上傳aar到私有maven服務(wù)器的示例
這篇文章主要介紹了android 上傳aar到私有maven服務(wù)器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android UI設(shè)計(jì)系列之自定義SwitchButton開關(guān)實(shí)現(xiàn)類似IOS中UISwitch的動(dòng)畫效果(2
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義SwitchButton開關(guān)實(shí)現(xiàn)類似IOS中UISwitch的動(dòng)畫效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
基于barcodescanner實(shí)現(xiàn)Android二維碼掃描功能
這篇文章主要為大家詳細(xì)介紹了基于barcodescanner實(shí)現(xiàn)Android二維碼掃描功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android空心圓及層疊效果實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android空心圓及層疊效果實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

