android下拉栏添加媒体音量调节

发布于:2025-06-19 ⋅ 阅读:(18) ⋅ 点赞:(0)

参考下拉栏的屏幕亮度调节,添加媒体音量调节。
平台信息:
MSM8909.LA.3.1.2_AOSP
PLATFORM_VERSION=9
BUILD_ID=PKQ1.190903.001

效果图

在这里插入图片描述

布局文件与音量图标

Index: frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml
===================================================================
--- frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml	(不存在的)
+++ frameworks/base/packages/SystemUI/res/layout/quick_settings_media_volume_dialog.xml	(版本 12429)
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed 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 at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed 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 and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:systemui="http://schemas.android.com/apk/res-auto"
+    android:layout_height="wrap_content"
+    android:layout_width="match_parent"
+    android:layout_gravity="center_vertical"
+    android:paddingLeft="16dp"
+    android:paddingRight="16dp">
+
+    <ImageView
+        android:id="@+id/media_volume_icon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:layout_marginEnd="8dp"
+        android:src="@drawable/ic_media_stream"
+        android:contentDescription="@null"
+        android:visibility="visible" />
+
+    <SeekBar
+        android:id="@+id/media_volume_seekbar"
+        android:layout_width="0dp"
+        android:layout_height="48dp"
+        android:layout_gravity="center_vertical"
+        android:layout_weight="1" />
+
+</LinearLayout>
Index: frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml
===================================================================
--- frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml	(不存在的)
+++ frameworks/base/packages/SystemUI/res/drawable/ic_media_stream.xml	(版本 12429)
@@ -0,0 +1,25 @@
+<!--
+    Copyright (C) 2017 The Android Open Source Project
+
+    Licensed 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 at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed 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 and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0"
+        android:tint="?android:attr/colorControlNormal">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,3l0.01,10.55c-0.59,-0.34 -1.27,-0.55 -2,-0.55C7.79,13 6,14.79 6,17c0,2.21 1.79,4 4.01,4S14,19.21 14,17V7h4V3H12zM10.01,19c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C12.01,18.1 11.11,19 10.01,19z"/>
+</vector>

实现

Index: frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
===================================================================
--- frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java	(版本 12424)
+++ frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java	(版本 12429)
@@ -20,10 +20,14 @@
 import static com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState;
 
 import android.annotation.Nullable;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.media.AudioManager;
 import android.metrics.LogMaker;
 import android.os.Handler;
 import android.os.Message;
@@ -32,6 +36,8 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
+import android.widget.SeekBar;
+import android.widget.SeekBar.OnSeekBarChangeListener;
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -62,7 +68,7 @@
 
     protected final Context mContext;
     protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
-    protected final View mBrightnessView;
+    protected final View mBrightnessView, mMediaVolumeView;
     private final H mHandler = new H();
     private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
     private final QSTileRevealController mQsTileRevealController;
@@ -87,6 +93,9 @@
     private BrightnessMirrorController mBrightnessMirrorController;
     private View mDivider;
 
+    private AudioManager mAudioManager;
+    private SeekBar mMediaVolumeSeekBar;
+
     public QSPanel(Context context) {
         this(context, null);
     }
@@ -101,6 +110,12 @@
             R.layout.quick_settings_brightness_dialog, this, false);
         addView(mBrightnessView);
 
+        mMediaVolumeView = LayoutInflater.from(mContext).inflate(
+            R.layout.quick_settings_media_volume_dialog, this, false);
+        addView(mMediaVolumeView);
+
         mTileLayout = (QSTileLayout) LayoutInflater.from(mContext).inflate(
                 R.layout.qs_paged_tile_layout, this, false);
         mTileLayout.setListening(mListening);
@@ -123,9 +138,42 @@
                 findViewById(R.id.brightness_icon),
                 findViewById(R.id.brightness_slider));
 
+        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+        mMediaVolumeSeekBar = findViewById(R.id.media_volume_seekbar);
+        mMediaVolumeSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
+        mMediaVolumeSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
+        mMediaVolumeSeekBar.setOnSeekBarChangeListener(mSeekListener);
+        IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
+        mContext.registerReceiver(mMediaVolumeReceiver, filter);
+
         updateResources();
     }
 
+    private final OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
+        @Override
+        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
+        }
+
+        @Override
+        public void onStartTrackingTouch(SeekBar seekBar) { }
+
+        @Override
+        public void onStopTrackingTouch(SeekBar seekBar) { }
+    };
+
+    private final BroadcastReceiver mMediaVolumeReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int currentMediaVolumeValue = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+            mMediaVolumeSeekBar.setProgress(currentMediaVolumeValue);
+        }
+    };
+
     protected void addDivider() {
         mDivider = LayoutInflater.from(mContext).inflate(R.layout.qs_divider, this, false);
         mDivider.setBackgroundColor(Utils.applyAlpha(mDivider.getAlpha(),


网站公告

今日签到

点亮在社区的每一天
去签到