Android自定义音量控制图标

设置音量控制数组

/**
* 使用时四舍五入
 */
public static double[] SOUND_VALUE_ARRAY = { 0, 0.75, 1.50, 2.25, 3.00,
        3.75, 4.50, 5.25, 6.00, 6.75, 7.50, 8.25, 9.00, 9.75, 10.50, 11.25,
        12.00, 12.75, 13.50, 14.25, 15.00 };
/**
* 将音量自定义为0-100,增长步长为5
* 这是对应的每个音量的图片
 */         
private final int[] SOUND_IMAGE_ARRAY = new int[] { R.drawable.osd_music_0,
        R.drawable.osd_music_5, R.drawable.osd_music_10,
        R.drawable.osd_music_15, R.drawable.osd_music_20,
        R.drawable.osd_music_25, R.drawable.osd_music_30,
        R.drawable.osd_music_35, R.drawable.osd_music_40,
        R.drawable.osd_music_45, R.drawable.osd_music_50,
        R.drawable.osd_music_55, R.drawable.osd_music_60,
        R.drawable.osd_music_65, R.drawable.osd_music_70,
        R.drawable.osd_music_75, R.drawable.osd_music_80,
        R.drawable.osd_music_85, R.drawable.osd_music_90,
        R.drawable.osd_music_95, R.drawable.osd_music_100 };

音量控制初始化定义

/**
* 使用时四舍五入
 */
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

音量减函数

public void volumDown(){
    if (mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) > 0) {
        if (Math.round((float)mAudioManager.getStreamVolume(
           AudioManager.STREAM_MUSIC) * 4 / 3) > 0) {
            controlImageView.setBackgroundResource(
            SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3 - 1)]);
            } else {
                 controlImageView.setBackgroundResource(
                 SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]);
            }

        if ((int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]) > 0) {
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                (int) Math.round(SOUND_VALUE_ARRAY[(int) (Math.floor((float)mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3) - 1)]),0);
            } else {
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                (int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]),0);
            }

        } else {
            controlImageView.setBackgroundResource(SOUND_IMAGE_ARRAY[0]);
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0,0);
        }
        controlImageView.setVisibility(View.VISIBLE);
        volHandler.sendEmptyMessageDelayed(VOL_CONTROL, 2000);

    }

音量加函数

public void volumPu(){
        if (mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM) < mAudioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {
            if (Math.round((float)mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3) <  
                 SOUND_IMAGE_ARRAY.length&&mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC)!=15) {
            controlImageView
                        .setBackgroundResource(SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager
                                .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3 + 1)]);
            } else {
                controlImageView
                        .setBackgroundResource(SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager
                                .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]);
            }
            if ((int) Math
                    .round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager
                            .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]) < 
                            SOUND_VALUE_ARRAY.length&&mAudioManager
                            .getStreamVolume(AudioManager.STREAM_MUSIC)!=15) {
                mAudioManager
                        .setStreamVolume(
                                AudioManager.STREAM_MUSIC,
                                (int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager
                                        .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3) + 1]),
                                0);
            } else {
                mAudioManager
                        .setStreamVolume(
                                AudioManager.STREAM_MUSIC,
                                (int) Math.round(SOUND_VALUE_ARRAY[Math.round((float)mAudioManager
                                        .getStreamVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]),
                                0);
            }
        } else {
            controlImageView
                    .setBackgroundResource(SOUND_IMAGE_ARRAY[Math.round((float)mAudioManager
                            .getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 4 / 3)]);
            mAudioManager
                    .setStreamVolume(
                            AudioManager.STREAM_MUSIC,
                            mAudioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                            0);
        }
        controlImageView.setVisibility(View.VISIBLE);
        volHandler.sendEmptyMessageDelayed(VOL_CONTROL, 2000);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值