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) {//松开的时候调用
            }
        });
相关推荐
阿pin4 小时前
Android随笔-kotlin withContext
android·kotlin·withcontext
树码小子5 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
Kslient5 小时前
HeaderBehavior
android
Android-Flutter6 小时前
android 动画详解
android·kotlin
小孔龙11 小时前
GraphicBuffer 跨进程共享
android
行业研究员12 小时前
Android内置GPS与腾讯地图定位技术对比分析
android·android定位·腾讯地图定位
Android-Flutter12 小时前
android WMS 详解(二)
android·kotlin
游戏开发爱好者813 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者814 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
阿pin15 小时前
Android随笔-kotlin 高阶函数
android·kotlin·高阶函数