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) {//松开的时候调用
            }
        });
相关推荐
fatiaozhang95273 小时前
中兴B860AV5.2-U_原机安卓4.4.2系统专用_晶晨S905L3SB处理器_线刷固件包
android·电视盒子·刷机固件·机顶盒刷机·中兴b860av5.2-u
儿歌八万首3 小时前
Android 自定义 View 实战:打造一个跟随滑动的丝滑指示器
android·kotlin
我有与与症3 小时前
Kuikly 实战:手把手撸一个跨平台 AI 聊天助手 (ChatDemo)
android
恋猫de小郭3 小时前
Flutter UI 设计库解耦重构进度,官方解答未来如何适配
android·前端·flutter
apihz4 小时前
全球IP归属地查询免费API详细指南
android·服务器·网络·网络协议·tcp/ip
hgz07104 小时前
Linux环境下MySQL 5.7安装与配置完全指南
android·adb
Just_Paranoid4 小时前
【Android UI】Android 添加圆角背景和点击效果
android·ui·shape·button·textview·ripple
梁同学与Android4 小时前
Android ---【经验篇】阿里云 CentOS 服务器环境搭建 + SpringBoot项目部署(二)
android·spring boot·后端
风往哪边走4 小时前
自定义简易日历
android