Android 开发 调节声音 SeekBar自定义样式

  • 效果图

  • xml布局 @mipmap/seekbar图片随意一张图都可以,这里我的图就不贴出来了

js 复制代码
				 <SeekBar
                    android:id="@+id/seekBar"
                    android:layout_marginLeft="8dp"
                    android:layout_width="377dp"
                    android:layout_height="8dp"
                    android:layout_gravity="center"
                    android:layout_centerHorizontal="true"
                    android:progressDrawable="@drawable/seek_bar_voice_bg"
                    android:thumb="@mipmap/seekbar" />
  • seek_bar_voice_bg.xml
javascript 复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <solid android:color="#D4E3F4" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#D4E3F4" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <solid android:color="#0A68F5" />
            </shape>
        </clip>
    </item>
</layer-list>
  • 逻辑实现
javascript 复制代码
 SeekBar seekBar=findViewById(R.id.seekBar);
 AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        //获取系统最大音量
        int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        seekBar.setMax(maxVolume);
        //获取当前音量
        int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        seekBar.setProgress(currentVolume);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {//滑动时候调用
                if(fromUser){
                    //设置系统音量
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                    int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
                    seekBar.setProgress(currentVolume);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {//摁下的时候调用
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {//松开的时候调用
            }
        });
相关推荐
慕伏白2 小时前
【慕伏白】Android Studio 无线调试配置
android·ide·android studio
低调小一2 小时前
Kuikly 小白拆解系列 · 第1篇|两棵树直调(Kotlin 构建与原生承载)
android·开发语言·kotlin
跟着珅聪学java2 小时前
spring boot 整合 activiti 教程
android·java·spring
川石课堂软件测试4 小时前
全链路Controller压测负载均衡
android·运维·开发语言·python·mysql·adb·负载均衡
2501_915921435 小时前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915921435 小时前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview
倔强的石头1065 小时前
【Linux指南】Linux命令行进度条实现原理解析
android·linux
yeziyfx6 小时前
Android Studio制作.9图(点9图/9Patch图)
android·ide·android studio
Chejdj7 小时前
StateFlow、SharedFlow 和LiveData区别
android·面试
蓝不蓝编程7 小时前
AI编程作品:Android 极简倒计时应用
android·ai编程