SystemUI底部三个虚拟按键的背景颜色修改

    三个虚拟按键的设置是在framework层中。
    先查看三个按键的布局的初始化:
位置 frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java:
protected PhoneStatusBarView makeStatusBarView() {
if (showNav) {
                mNavigationBarView =
                    (NavigationBarView) View.inflate(context, R.layout.navigation_bar, null);

                mNavigationBarView.setDisabledFlags(mDisabled);
                mNavigationBarView.setBar(this);
                mNavigationBarView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        checkUserAutohide(v, event);
                        return false;
                    }});
            }

}
    在上面的代码中,mNavigationBarView = (NavigationBarView) View.inflate(context, R.layout.navigation_bar, null),该代码动态加载了该布局。常看该布局的代码:
位置 frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml:
<com.android.systemui.statusbar.phone.NavigationBarView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@drawable/system_bar_background"
    >
<!--
 -->
    <FrameLayout android:id="@+id/rot0"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        >

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:id="@+id/nav_buttons"
            android:animateLayoutChanges="true"
            >

            <!-- navigation controls -->
            <View
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:visibility="invisible"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"
                android:layout_width="@dimen/navigation_key_width"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_back"
                systemui:keyCode="4"
                android:layout_weight="0"
                android:scaleType="center"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_back"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/home"
                android:layout_width="@dimen/navigation_key_width"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_home"
                systemui:keyCode="3"
                systemui:keyRepeat="false"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_home"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"
                android:layout_width="@dimen/navigation_key_width"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_recent"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/menu"
                android:layout_width="@dimen/navigation_menu_key_width"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_menu"
                systemui:keyCode="82"
                android:layout_weight="0"
                android:visibility="invisible"
                android:contentDescription="@string/accessibility_menu"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                />
        </LinearLayout>

        <!-- lights out layout to match exactly -->
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:id="@+id/lights_out"
            android:visibility="gone"
            >
            <ImageView
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:layout_marginStart="40dp"
                android:src="@drawable/ic_sysbar_lights_out_dot_small"
                android:scaleType="center"
                android:layout_weight="0"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <ImageView
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_lights_out_dot_large"
                android:scaleType="center"
                android:layout_weight="0"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <ImageView
                android:layout_width="80dp"
                android:layout_marginEnd="40dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_lights_out_dot_small"
                android:scaleType="center"
                android:layout_weight="0"
                />
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.android.systemui.statusbar.policy.KeyButtonView
                android:layout_width="80dp"
                android:id="@+id/search_light"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:src="@drawable/search_light"
                android:scaleType="center"
                android:visibility="gone"
                android:contentDescription="@string/accessibility_search_light"
                />

            <com.android.systemui.statusbar.policy.KeyButtonView
                android:id="@+id/camera_button"
                android:layout_height="match_parent"
                android:layout_width="80dp"
                android:layout_gravity="center_vertical|right"
                android:src="@drawable/ic_sysbar_camera"
                android:scaleType="center"
                android:visibility="gone"
                android:contentDescription="@string/accessibility_camera_button"
                />
        </FrameLayout>

        <com.android.systemui.statusbar.policy.DeadZone
            android:id="@+id/deadzone"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            systemui:minSize="@dimen/navigation_bar_deadzone_size"
            systemui:maxSize="@dimen/navigation_bar_deadzone_size_max"
            systemui:holdTime="@integer/navigation_bar_deadzone_hold"
            systemui:decayTime="@integer/navigation_bar_deadzone_decay"
            systemui:orientation="horizontal"
            android:layout_gravity="top"
            />
    </FrameLayout>

    <FrameLayout android:id="@+id/rot90"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:paddingTop="0dp"
        >

        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:id="@+id/nav_buttons"
            android:animateLayoutChanges="true"
            >

            <!-- navigation controls -->
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/menu"
                android:layout_height="40dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_menu_land"
                systemui:keyCode="82"
                android:layout_weight="0"
                android:visibility="invisible"
                android:contentDescription="@string/accessibility_menu"
                systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"
                android:layout_height="80dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_recent_land"
                android:layout_weight="0"
                android:contentDescription="@string/accessibility_recent"
                systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
                />
            <View
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/home"
                android:layout_height="80dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_home_land"
                systemui:keyCode="3"
                systemui:keyRepeat="false"
                android:layout_weight="0"
                android:contentDescription="@string/accessibility_home"
                systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
                />
            <View
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"
                android:layout_height="80dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_back_land"
                android:scaleType="center"
                systemui:keyCode="4"
                android:layout_weight="0"
                android:contentDescription="@string/accessibility_back"
                systemui:glowBackground="@drawable/ic_sysbar_highlight_land"
                />
            <View
                android:layout_height="40dp"
                android:layout_width="match_parent"
                android:layout_weight="0"
                android:visibility="invisible"
                />
        </LinearLayout>

        <!-- lights out layout to match exactly -->
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:id="@+id/lights_out"
            android:visibility="gone"
            >
            <ImageView
                android:layout_height="80dp"
                android:layout_marginTop="40dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_lights_out_dot_small"
                android:scaleType="center"
                android:layout_weight="0"
                />
            <View
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <ImageView
                android:layout_height="80dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_lights_out_dot_large"
                android:scaleType="center"
                android:layout_weight="0"
                />
            <View
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:visibility="invisible"
                />
            <ImageView
                android:layout_height="80dp"
                android:layout_marginBottom="40dp"
                android:layout_width="match_parent"
                android:src="@drawable/ic_sysbar_lights_out_dot_small"
                android:scaleType="center"
                android:layout_weight="0"
                />
        </LinearLayout>

        <com.android.systemui.statusbar.policy.KeyButtonView
            android:id="@+id/search_light"
            android:layout_height="80dp"
            android:layout_width="match_parent"
            android:layout_gravity="center_vertical"
            android:src="@drawable/search_light_land"
            android:scaleType="center"
            android:visibility="gone"
            android:contentDescription="@string/accessibility_search_light"
            />

        <!-- No camera button in landscape mode -->

        <com.android.systemui.statusbar.policy.DeadZone
            android:id="@+id/deadzone"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            systemui:minSize="@dimen/navigation_bar_deadzone_size"
            systemui:maxSize="@dimen/navigation_bar_deadzone_size_max"
            systemui:holdTime="@integer/navigation_bar_deadzone_hold"
            systemui:decayTime="@integer/navigation_bar_deadzone_decay"
            systemui:orientation="vertical"
            android:layout_gravity="top"
            />
    </FrameLayout>

    <!-- not used -->
    <View android:id="@+id/rot270"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:visibility="gone"
        />

</com.android.systemui.statusbar.phone.NavigationBarView>
其中,android:background="@drawable/system_bar_background"该属性用于设置布局的背景(个人尝试修改该属性,貌似没有起作用)
@drawable/system_bar_background,背景图片就是一种颜色
位置:frameworks/base/packages/SystemUI/res/values/colors.xml
<drawable name="system_bar_background">@color/system_bar_background_opaque</drawable>
<color name="system_bar_background_opaque">#77000000</color>

进入NavigationBarView的源码中查看:
位置:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
 public NavigationBarView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mDisplay = ((WindowManager)context.getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay();

        final Resources res = mContext.getResources();
        mBarSize = res.getDimensionPixelSize(R.dimen.navigation_bar_size);
        mVertical = false;
        mShowMenu = false;
        mDelegateHelper = new DelegateViewHelper(this);

        getIcons(res);

        mBarTransitions = new NavigationBarTransitions(this);

        mCameraDisabledByDpm = isCameraDisabledByDpm();
        watchForDevicePolicyChanges();
    }
其中,mBarTransitions = new NavigationBarTransitions(this)中实例化了一个大用于布局的过渡的实例,按键布局作为参数传递到该类中,查看NavigationBarTransitions类的源码:
位置:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarTransitions.java
 public NavigationBarTransitions(NavigationBarView view) {
        super(view, R.drawable.nav_background);
        mView = view;
        mBarService = IStatusBarService.Stub.asInterface(
                ServiceManager.getService(Context.STATUS_BAR_SERVICE));
    }

    看super(view, R.drawable.nav_background)代码,可以知道该类的构造函数调用了其父类的构造函数,其中,NavigationBarView实例和一个图片的Id作为参数传到该类的父类的构造函数中,进入NavigationBarTransitions类的父类,查看代码:
位置:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/BarTransitions.java

public BarTransitions(View view, int gradientResourceId) {
        mTag = "BarTransitions." + view.getClass().getSimpleName();
        mView = view;
        mBarBackground = new BarBackgroundDrawable(mView.getContext(), gradientResourceId);
        if (HIGH_END) {
            Log.i("Garment27", "BarTransitions---HIGH_END");
            mView.setBackground(mBarBackground);
        }
    }

从其构造函数可以看到,mView.setBackground(mBarBackground)这段代码就是把传入的图片设置成为按键布局的背景(在Launcher3中,该图片是一张透明的图片,所以得到的背景是透明的)
尝试把mView.setBackground(mBarBackground)这段代码屏蔽了,然后按键布局就只显示<color name="system_bar_background_opaque">#77000000</color>中定义的颜色,上面的statusbar和下面的虚拟按键布局的背景设置是共用一套的背景,是因为两个布局都是有一个过渡类,大家都是继承BarTransitions类。

其实,虚拟按键布局的背景颜色是会有一个过度的,在Launcher界面中,虚拟按键布局背景图是显示的是透明的图片,当我们进入到某一个应用中的时候,背景图就会过渡为黑色的背景。其中的代码是如何实现的,还有待研究。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值