写点什么

Android SystemUI 源码分析与修改,并发编程挑战

作者:嘟嘟侠客
  • 2021 年 11 月 27 日
  • 本文字数:2033 字

    阅读完需:约 7 分钟

实现过程如下:


1.首先在 SystemUI 中添加音量加减的资源文件,路径如下:


frameworks/base/packages/SystemUI/res/


将图片放入对应的 drawable 文件夹,包括音量+,和音量-,见上图。


2.修改导航栏的布局文件,路径:


frameworks/base/packages/SystemUI/res/


在对应的 layout 文件夹中找到 navigation_bar.xml 文件进行修改:


在返回键前面添加“音量减”,返回键的布局:


<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"


android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"


android:layout_height="match_parent"


android:src="@drawable/ic_sysbar_back"


systemui:keyCode="4"


android:layout_weight="0"


systemui:glowBackground="@drawable/ic_sysbar_highlight"


android:contentDescription="@string/accessibility_back"


/>


音量减的布局如下,这里先把 Visibility 定义为 Gone,然后在代码中控制是否显示:


<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sub"


android:src="@drawable/sub_normal"


android:layout_width="@dimen/navigation_key_width"


android:layout_height="match_parent"


android:layout_weight="0"


systemui:keyCode="302"


systemui:glowBackground="@drawable/ic_sysbar_highlight"


android:visibility="gone"/>


“音量加”添加到“最近应用”之后,最近应用的布局:


<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"


android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"


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/add"


android:src="@drawable/add_normal"


android:layout_width="@dimen/navigation_key_width"


android:layout_height="match_parent"


android:layout_weight="0"


systemui:keyCode="301"


systemui:glowBackground="@drawable/ic_sysbar_highlight"


android:visibility="gone"/>


3.接着修改代码逻辑,文件路径:


frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java


在 private void prepareNavigationBarView() {……}函数中添加显示音量加减的代码:


mNavigationBarView.getAddVolume().setVisibility(View.VISIBLE);


mNavigationBarView.getSubVolume()


《Android 学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享


.setVisibility(View.VISIBLE);


对应的函数 getAddVolume()和 getAddVolume()要在


frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java


中实现:


public View getAddVolume(){


return mCurrentView.findViewById(R.id.add);


}


public View getSubVolume(){


return mCurrentView.findViewById(R.id.sub);


}


最后就是功能实现了,在


frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java


中添加监听函数:


private View.OnTouchListener mAddVolumeOnTouchListener = new View.OnTouchListener() {


public boolean onTouch(View v, MotionEvent ev) {


final int action = ev.getAction();


switch(action) {


case MotionEvent.ACTION_DOWN:


is_down = true;


Adjust_Volume(true);


maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);


break;


case MotionEvent.ACTION_MOVE:


is_down = true;


maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);


// maddHandler.removeCallbacks(maddRun);


break;


case MotionEvent.ACTION_UP:


is_down = false;


maddHandler.removeCallbacks(maddRun);


break;


}


return true;


}


};


private View.OnTouchListener mSubVolumeOnTouchListener = new View.OnTouchListener() {


public boolean onTouch(View v, MotionEvent ev) {


final int action = ev.getAction();


int x, y;


//int mCode = ev.getAction();

最后

我见过很多技术 leader 在面试的时候,遇到处于迷茫期的大龄程序员,比面试官年龄都大。这些人有一些共同特征:可能工作了 5、6 年,还是每天重复给业务部门写代码,工作内容的重复性比较高,没有什么技术含量的工作。问到这些人的职业规划时,他们也没有太多想法。


其实 30 岁到 40 岁是一个人职业发展的黄金阶段,一定要在业务范围内的扩张,技术广度和深度提升上有自己的计划,才有助于在职业发展上有持续的发展路径,而不至于停滞不前。


不断奔跑,你就知道学习的意义所在!




本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

用户头像

嘟嘟侠客

关注

还未添加个人签名 2021.03.19 加入

还未添加个人简介

评论

发布
暂无评论
Android SystemUI源码分析与修改,并发编程挑战