快速實現手勢解鎖功能

添加 PatternLock 到項目

  • 第一步: 添加 JitPack 到項目的根 build.gradle
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
複製代碼
  • 第二步:添加庫依賴
dependencies {
    implementation 'com.github.huangziye:PatternLock:${latest_version}'
}
複製代碼
  • 在 xml 中編寫
<com.hzy.lock.PatternLockView android:id="@+id/mPatternLockView" android:layout_width="280dp" android:layout_height="280dp" android:layout_gravity="center_horizontal" android:layout_marginTop="16dp" app:aspectRatio="square" app:aspectRatioEnabled="true" app:dotAnimationDuration="150" app:dotCount="3" app:dotNormalSize="10dp" app:dotSelectedSize="24dp" app:normalStateColor="@color/colorPrimary" app:correctStateColor="@color/colorPrimary" app:wrongStateColor="@color/pomegranate" app:pathEndAnimationDuration="100" app:pathWidth="3dp"/>
複製代碼
  • 用 kotlin 代碼實現
private fun initPatternLockView() {
    //設置橫縱座標點的個數
    mPatternLockView.setDotCount(3)
    //設置未選中點的大小
    mPatternLockView.setDotNormalSize(
        ResourceUtil.getDimensionInPx(
            this,
            R.dimen.pattern_lock_dot_size
        ) as Int
    )
    //設置選中時點的大小
    mPatternLockView.setDotSelectedSize(
        ResourceUtil.getDimensionInPx(
            this,
            R.dimen.pattern_lock_dot_selected_size
        ) as Int
    )
    //設置路徑線的寬度
    mPatternLockView.setPathWidth(
        ResourceUtil.getDimensionInPx(
            this,
            R.dimen.pattern_lock_path_width
        ) as Int
    )
    //設置寬高比是否啓用
    mPatternLockView.setAspectRatioEnabled(true)
    //設置寬高比
    mPatternLockView.setAspectRatio(PatternLockView.AspectRatio.ASPECT_RATIO_HEIGHT_BIAS)
    //設置View的模式
    mPatternLockView.setViewMode(PatternLockView.PatternViewMode.CORRECT)
    //設置點動畫持續時間
    mPatternLockView.setDotAnimationDuration(150)

    // 設置Pat結束動畫持續時間
    mPatternLockView.setPathEndAnimationDuration(100)
    //設置正確的狀態顏色
    mPatternLockView.setCorrectStateColor(ResourceUtil.getColor(this, R.color.colorPrimary))
    //是否設置爲隱身模式
    mPatternLockView.setInStealthMode(false)
    //設置是否啓用觸覺反饋
    mPatternLockView.setTactileFeedbackEnabled(true)
    //設置輸入是否啓用
    mPatternLockView.setInputEnabled(true)
    mPatternLockView.addPatternLockListener(mPatternLockViewListener)
}
複製代碼

效果預覽

效果預覽


關於我


License

Copyright 2018, huangziye

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
複製代碼
相關文章
相關標籤/搜索