随笔 - 65  文章 - 0  评论 - 21  阅读 - 32万

【Android Lock Pattern】图案解锁(一):LockPatternView源代码

选择Settings -> Security ->Set up screen lock

设置屏幕锁

选择Pattern

设置图案

在我的真机HTC Desire(Android 2.2)上,截图如下:

Android是一个开源的操作系统,所以我们可以通过下载该控件的源代码重用该控件。

 

1、LockPatterView源代码

图案解锁控件,对应着framework层的LockPatterView类,如下所示:

源代码:https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/LockPatternView.java

 

2、修改

下载的LockPatternView.java文件,无法在项目中直接使用,需要进行修改。

1)添加图片文件

添加6张图片文件到res/drawable:

对应LockPatternView.java的代码

mBitmapBtnDefault = getBitmapFor(R.drawable.btn_code_lock_default_holo);
mBitmapBtnTouched = getBitmapFor(R.drawable.btn_code_lock_touched_holo);
mBitmapCircleDefault = getBitmapFor(R.drawable.indicator_code_lock_point_area_default_holo);
mBitmapCircleGreen = getBitmapFor(R.drawable.indicator_code_lock_point_area_green_holo);
mBitmapCircleRed = getBitmapFor(R.drawable.indicator_code_lock_point_area_red_holo);

mBitmapArrowGreenUp = getBitmapFor(R.drawable.indicator_code_lock_drag_direction_green_up);
mBitmapArrowRedUp = getBitmapFor(R.drawable.indicator_code_lock_drag_direction_red_up);

btn_code_lock_default_holo.png

btn_code_lock_touched_holo.png

indicator_code_lock_point_area_default_holo.png

indicator_code_lock_point_area_green_holo.png

indicator_code_lock_point_area_red_holo.png

indicator_code_lock_drag_direction_green_up.png

indicator_code_lock_drag_direction_red_up.png

 

2)添加attrs配置文件

添加attrs.xml文件到res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="LockPatternView">
        <attr name="aspect" format="string" />
    </declare-styleable>
</resources>

 

3)更新strings配置文件

在res/strings.xml添加新的<string/>

<!-- Accessibility description sent when user starts drawing a lock pattern. -->
<string name="lockscreen_access_pattern_start">Pattern started</string>
<!-- Accessibility description sent when the pattern times out and is cleared. -->
<string name="lockscreen_access_pattern_cleared">Pattern cleared</string>
<!-- Accessibility description sent when user adds a cell to the pattern.  -->
<string name="lockscreen_access_pattern_cell_added">Cell added</string>
<!-- Accessibility description sent when user completes drawing a pattern. -->
<string name="lockscreen_access_pattern_detected">Pattern completed</string>

 

4)修改代码

修改mPaddingLeft为getPaddingLeft(),mPaddingRight为getPaddingRight(),mPaddingTop为getPaddingTop(),mPaddingBottom为getPaddingBottom()

修改mContext为getContext()

修改方法private void sendAccessEvent(int resId)

private void sendAccessEvent(int resId) {
    setContentDescription(getContext().getString(resId));
    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
    setContentDescription(null);
}

 

5)添加类LockPatternUtils

新建LockPatternUtils.java文件,实现public static String patternToString(List<LockPatternView.Cell> pattern)和public static List<LockPatternView.Cell> stringToPattern(String string)方法

具体可以参考Android的LockPatternUtils类

源代码:https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/LockPatternUtils.java

 

3、使用

LockPatternView的使用与其它View相同,并提供了事件的回调处理的方法public void setOnPatternListener(OnPatternListener onPatternListener)和接口LockPatternView.OnPatternListener

 

参考:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=231102

 

 

posted on   Anthony Li  阅读(17225)  评论(2编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

博客园博客已停止更新,博客地址:dyinigbleed.com

点击右上角即可分享
微信分享提示