码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-05-12 23:44:09      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

屏幕亮度调节

/**
 * 滑动改变亮度,不需要权限
 *
 * @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】系统音量及屏幕亮度调节

标签:

原文地址:http://blog.csdn.net/chengjun77/article/details/51348111

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!