LockIconView-锁

一、概述

 

二、图标切换机制

DrawableState存储着控件的enable、pressed、checked等状态,绘制时根据这些状态绘制出不同的图像。DrawableState的基础属性是不可修改的,但是我们可以通过修改DrawableState来给控件添加一些本来没有的属性。比如ImageView继承自View,本来是没有checked属性的,基础属性只有一个state_enabled,baseStates={android.R.attr.state_enable},通过setImageState(int[] extraState, boolean merge)方法可以合并添加新的属性。其中,第一个参数表示追加属性,第二个参数表示是否和基础属性合并。

ImageView iv = findViewById(R.id.iv);
iv.setIamgeState(new int[]{android.R.attr.state_checked, android.R.attr.state_pressed}, true);
//extraStates = {android.R.attr.state_enabled, android.R.attr.state_checked, android.R.attr.state_pressed} iv.setIamgeState(
new int[]{android.R.attr.state_checked, android.R.attr.state_pressed}, false);
//extraStates = {android.R.attr.state_checked, android.R.attr.state_pressed} iv.setIamgeState(
new int[]{}, true);
//extraStates = {android.R.attr.state_enabled} iv.setImageState(
new int[]{}, false);
//extraStates = {}

 LockIconView就是利用这种机制来切换图标的。

(1)R.drawable.super_lock_icon

在LockIconViewController的构造函数里,对LockIconView设置了R.drawable.super_lock_icon,这个drawable包含了锁、解锁、指纹等各种状态的图标和切换动画,并指定了对应的state,如指纹图标:

    <item
        android:id="@+id/locked"
        android:drawable="@drawable/ic_lock_32dp"
        android:state_first="true"
        android:state_single="false"/>

    <item
        android:id="@+id/locked_fp"
        android:state_middle="true"
        android:state_single="false"
        android:drawable="@drawable/ic_kg_fingerprint" />

    <item
        android:id="@+id/unlocked"
        android:state_last="true"
        android:state_single="false"
        android:drawable="@drawable/ic_unlocked_32dp" />

其中,state_single用来判断是否AOD状态。

  • state_first = true,state_single = false时,显示锁定图标
  • state_middle = true,state_single = false时,显示指纹图标
  • state_last = true,state_single = false时,显示开锁图标

(2)updateVisibility()方法

在LockIconViewController的updateVisibility()方法中,通过调用LockIconView的updateIcon方法来切换ImageState,从而实现切换图标。

    public void updateIcon(@IconType int icon, boolean aod) {
        mIconType = icon;
        mAod = aod;

        mLockIcon.setImageState(getLockIconState(mIconType, mAod), true);
    }

(3)刷新事件

updateVisibility()的触发则依赖于人脸启动、KeyguardState等其他外部事件,多达十余处。


 

三、图标刷新事件依赖

 

posted @ 2023-05-29 17:07  西贝雪  阅读(58)  评论(0编辑  收藏  举报