【Android】系统音量及屏幕亮度调节

屏幕亮度调节

/**
 * 滑动改变亮度,不需要权限
 *
 * @param percent 亮度百分比,值为0.0----1.0
 */
private void onBrightnessSlide(float percent) {
    if (mBrightness < 0) { // mBrightness是当前屏幕的亮度
        mBrightness = getWindow().getAttributes().screenBrightness;
        if (mBrightness <= 0.00f)
            mBrightness = 0.50f;
        if (mBrightness < 0.01f)
            mBrightness = 0.01f;
        // 显示
        mOperationBg.setImageResource(R.drawable.video_brightness_bg);
        mVolumeBrightnessLayout.setVisibility(View.VISIBLE);
    }
    WindowManager.LayoutParams lpa = getWindow().getAttributes();
    lpa.screenBrightness = mBrightness + percent;
    if (lpa.screenBrightness > 1.0f)
        lpa.screenBrightness = 1.0f;
    else if (lpa.screenBrightness < 0.01f)
        lpa.screenBrightness = 0.01f;
    getWindow().setAttributes(lpa);

    ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();
    //这部分是改变图片上面的当前亮度的进度的
    lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness);
    mOperationPercent.setLayoutParams(lp);
}

音量调节

/**
 * 音量调节,不需要权限
 */
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private AudioManager mAudioManager;
    private int currentVolume;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        // 最大音量
        int maxVolume = mAudioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        Log.i(TAG, "最大音量:" + maxVolume);
        currentVolume = mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.i(TAG, "当前音量:" + currentVolume);
    }

    public void up(View view) {
        // 音量增大
        // AudioManager.STREAM_SYSTEM
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);
        currentVolume = mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.i(TAG, "当前音量:" + currentVolume);
    }

    public void down(View view) {
        // 音量减小
        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP);
        currentVolume = mAudioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.i(TAG, "当前音量:" + currentVolume);
    }
}

欢迎androider扫描以下二维码关注微信公众号:爱安卓 ,或者搜索微信 : loveandroid321关注
【Android】系统音量及屏幕亮度调节_第1张图片

你可能感兴趣的:(【Android】系统音量及屏幕亮度调节)