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) {//松开的时候调用
            }
        });
相关推荐
程序员码歌3 小时前
【零代码AI编程实战】AI灯塔导航-总结篇
android·前端·后端
书弋江山4 小时前
flutter 跨平台编码库 protobuf 工具使用
android·flutter
来来走走6 小时前
Flutter开发 webview_flutter的基本使用
android·flutter
Jerry说前后端7 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
louisgeek8 小时前
Android OkHttp Interceptor
android
大王派来巡山的小旋风8 小时前
Kotlin基本用法三
android·kotlin
Jerry说前后端9 小时前
Android 移动端 UI 设计:前端常用设计原则总结
android·前端·ui
bytebeats9 小时前
Jetpack Compose 1.9: 核心新特性简介
android·android jetpack
Icey_World9 小时前
Mysql笔记-错误条件\处理程序
android
大王派来巡山的小旋风10 小时前
Kotlin基本用法之集合(一)
android·程序员·kotlin