当前位置: 首页 > news >正文

Android 修改SystemUI 音量条的声音进度条样式

一、前言

        Android System UI 开发经常会遇到修改音量进度条样式的需求,主要涉及的类有VolumeDialogImpl与xml文件,接下来会逐步实现流程。先看看效果。

修改前

修改后

二、找到对应类

通过aidegen 打断点调试对应代码类VolumeDialogImpl定位到volume_dialog就是对话框布局。mDialog.setContentView(R.layout.volume_dialog);

类路径:frameworks/base/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java

private void initDialog() {mDialog = new CustomDialog(mContext);initDimens();mConfigurableTexts = new ConfigurableTexts(mContext);mHovering = false;mShowing = false;mWindow = mDialog.getWindow();mWindow.requestFeature(Window.FEATURE_NO_TITLE);mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH| WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);mWindow.addPrivateFlags(WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY);mWindow.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);mWindow.setWindowAnimations(com.android.internal.R.style.Animation_Toast);WindowManager.LayoutParams lp = mWindow.getAttributes();lp.format = PixelFormat.TRANSLUCENT;lp.setTitle(VolumeDialogImpl.class.getSimpleName());lp.windowAnimations = -1;lp.gravity = mContext.getResources().getInteger(R.integer.volume_dialog_gravity);mWindow.setAttributes(lp);mWindow.setLayout(WRAP_CONTENT, WRAP_CONTENT);mDialog.setContentView(R.layout.volume_dialog);mDialogView = mDialog.findViewById(R.id.volume_dialog);mDialogView.setAlpha(0);mDialog.setCanceledOnTouchOutside(true);

三、音量条代码

 @SuppressLint("InflateParams")private void initRow(final VolumeRow row, final int stream, int iconRes, int iconMuteRes,boolean important, boolean defaultStream) {row.stream = stream;row.iconRes = iconRes;row.iconMuteRes = iconMuteRes;row.important = important;row.defaultStream = defaultStream;row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);row.view.setId(row.stream);row.view.setTag(row);row.header = row.view.findViewById(R.id.volume_row_header);row.header.setId(20 * row.stream);if (stream == STREAM_ACCESSIBILITY) {row.header.setFilters(new InputFilter[] {new InputFilter.LengthFilter(13)});}row.dndIcon = row.view.findViewById(R.id.dnd_icon);row.slider = row.view.findViewById(R.id.volume_row_slider);row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));row.number = row.view.findViewById(R.id.volume_number);row.anim = null;final LayerDrawable seekbarDrawable =(LayerDrawable) mContext.getDrawable(R.drawable.volume_row_seekbar);final LayerDrawable seekbarProgressDrawable = (LayerDrawable)((RoundedCornerProgressDrawable) seekbarDrawable.findDrawableByLayerId(android.R.id.progress)).getDrawable();row.sliderProgressSolid = seekbarProgressDrawable.findDrawableByLayerId(R.id.volume_seekbar_progress_solid);final Drawable sliderProgressIcon = seekbarProgressDrawable.findDrawableByLayerId(R.id.volume_seekbar_progress_icon);row.sliderProgressIcon = sliderProgressIcon != null ? (AlphaTintDrawableWrapper)((RotateDrawable) sliderProgressIcon).getDrawable() : null;row.slider.setProgressDrawable(seekbarDrawable);row.icon = row.view.findViewById(R.id.volume_row_icon);row.setIcon(iconRes, mContext.getTheme());if (row.icon != null) {if (row.stream != AudioSystem.STREAM_ACCESSIBILITY) {row.icon.setOnClickListener(v -> {Events.writeEvent(Events.EVENT_ICON_CLICK, row.stream, row.iconState);mController.setActiveStream(row.stream);if (row.stream == AudioManager.STREAM_RING) {final boolean hasVibrator = mController.hasVibrator();if (mState.ringerModeInternal == AudioManager.RINGER_MODE_NORMAL) {if (hasVibrator) {mController.setRingerMode(AudioManager.RINGER_MODE_VIBRATE, false);} else {final boolean wasZero = row.ss.level == 0;mController.setStreamVolume(stream,wasZero ? row.lastAudibleLevel : 0);}} else {mController.setRingerMode(AudioManager.RINGER_MODE_NORMAL, false);if (row.ss.level == 0) {mController.setStreamVolume(stream, 1);}}} else {final boolean vmute = row.ss.level == row.ss.levelMin;mController.setStreamVolume(stream,vmute ? row.lastAudibleLevel : row.ss.levelMin);}row.userAttempt = 0;  // reset the grace period, slider updates immediately});} else {row.icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);}}}

四、音量条布局则位于volume_dialog_row.xml中

<!--Copyright (C) 2015 The Android Open Source ProjectLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
-->
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:tag="row"android:layout_height="wrap_content"android:layout_width="@dimen/volume_dialog_panel_width"android:clipChildren="false"android:clipToPadding="false"android:gravity="center"android:paddingTop="@dimen/volume_dialog_ringer_rows_padding"android:paddingBottom="@dimen/volume_dialog_ringer_rows_padding"android:background="@drawable/volume_row_rounded_background"android:theme="@style/volume_dialog_theme"><LinearLayoutandroid:layout_height="wrap_content"android:layout_width="@dimen/volume_dialog_panel_width"android:gravity="center"android:layout_gravity="center"android:orientation="vertical" ><TextViewandroid:id="@+id/volume_row_header"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="end"android:maxLength="10"android:maxLines="1"android:visibility="gone"android:textColor="?android:attr/colorControlNormal"android:textAppearance="@style/TextAppearance.Volume.Header" /><FrameLayoutandroid:id="@+id/volume_row_slider_frame"android:layout_width="match_parent"android:layout_height="@dimen/volume_row_slider_height"><SeekBarandroid:id="@+id/volume_row_slider"android:paddingLeft="0dp"android:paddingRight="0dp"android:paddingStart="0dp"android:paddingEnd="0dp"android:clickable="true"android:layout_width="@dimen/volume_row_slider_height"android:layout_height="match_parent"android:layout_gravity="center"android:thumb="@null"android:splitTrack="false"android:progressDrawable="@drawable/volume_row_seekbar"android:background="@null"android:layoutDirection="ltr"android:rotation="270" /><include layout="@layout/volume_dnd_icon"/></FrameLayout><com.android.keyguard.AlphaOptimizedImageButtonandroid:visibility="gone"android:id="@+id/volume_row_icon"style="@style/VolumeButtons"android:layout_width="@dimen/volume_dialog_tap_target_size"android:layout_height="@dimen/volume_dialog_tap_target_size"android:background="@drawable/ripple_drawable_20dp"android:layout_marginBottom="@dimen/volume_dialog_row_margin_bottom"android:tint="@color/accent_tint_color_selector"android:soundEffectsEnabled="false" /></LinearLayout></FrameLayout>

对SeekBar音量条进行修改增加样式

android:thumb="@drawable/ic_launcher_round"
android:progressDrawable="@drawable/volume_row_seekbar"

其中icon progress 就是一张ic_launcher_round图片
而volume_row_seekbar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@android:id/background"android:gravity="center_vertical|fill_horizontal"><shape android:shape="rectangle"><size android:height="@dimen/seek_bar_height" /><solid android:color="#eeeeee" /><corners android:radius="@dimen/seek_bar_corner_radius" /></shape></item><item android:id="@android:id/progress"android:gravity="center_vertical|fill_horizontal"><scale android:scaleWidth="100%"><shape android:shape="rectangle"><size android:height="@dimen/seek_bar_height" /><solid android:color="#0091ff" /><corners android:radius="@dimen/seek_bar_corner_radius" /></shape></scale></item>
</layer-list>

五、待续

http://www.lryc.cn/news/429548.html

相关文章:

  • 电商场景的视频生成的prompt测评集合
  • day34
  • 无缝融入,即刻智能[三]:Dify-LLM平台知识库构建(多路召回、精排重排),43K+星标见证专属智能方案
  • AWS服务WAF
  • 二叉树中的奇偶树问题
  • GD - EmbeddedBuilder - 用DMA进行串口发送接收,支持接收不定长包
  • 英语中apartment(公寓)(美式)、house(房子)、flat(公寓)(英式)、villa(别墅)、room(房间)区别
  • 黑马头条vue2.0项目实战(十一)——功能优化(组件缓存、响应拦截器、路由跳转与权限管理)
  • 《AI视频类工具之一——​ 即创》
  • CSS的:host伪类:精确定位于Web组件的指南
  • 安卓sdk manager下载安装
  • CV学习笔记3-图像特征提取
  • Git使用方法(三)---简洁版上传git代码
  • 8.21-部署eleme项目
  • 多目标跟踪之ByteTrack论文(翻译+精读)
  • 【实践】Java开发常用工具类或中间件
  • Vue2移动端(H5项目)项目封装车牌选择组件
  • 四川财谷通信息技术有限公司抖音小店的优势
  • 2025届八股文:计算机网络高频重点面试题
  • 嵌入式和单片机有什么区别?
  • JSON.stringify 和 JSON.parse
  • APP架构设计_2.用MVVM架构实现一个具体业务
  • 安恒信息总裁宋端智,辞职了!活捉一枚新鲜出炉的餐饮人!
  • 《javaEE篇》--定时器
  • OpenLayers3, 缩放、平移、复位操作
  • 进程与线程(7)
  • 传知代码-自动化细胞核分割与特征分析(论文复现)
  • Vue UI - 可视化的Vue项目管理器
  • 团队管理之敏捷开发
  • Hive3:表的常用修改语句