通过Button按钮调节亮度

最近项目中需要一个通过Button按钮来调节亮度的功能,而在网上呢几乎都是直接滑动进度条来实现的。因此写这个帖子,一是为了自己做个笔记;二是为了给大家提供一个资源。稍微有点混乱,刚开始写。大家见谅。

好了,先放上布局文件,我们慢慢开始了解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    tools:context="com.example.oxygenaddingrf.SettingActivity">


    /*brightness*/
    <ImageButton
        android:id="@+id/img_btn_brightness_reduce"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="320dp"
        android:layout_marginLeft="110dp"
        android:background="@drawable/btn_setting_reduce"/>

    <RelativeLayout
        android:id="@+id/layout_brightness"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="330dp"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@id/img_btn_volume_reduce">
        <ImageView
            android:layout_width="470dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/pian_16"/>
        <SeekBar
            android:id="@+id/sb_brightness"
            android:layout_width="495dp"
            android:layout_height="wrap_content"
            android:progressDrawable="@drawable/seekbar_progress"
            android:thumb="@drawable/button_07"/>
    </RelativeLayout>

    <ImageButton
        android:id="@+id/img_btn_brightness_plus"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="320dp"
        android:layout_toRightOf="@id/layout_brightness"
        android:background="@drawable/btn_setting_plus"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="380dp"
        android:textColor="#ffffff"
        android:textSize="22px"
        android:text="@string/brightness"/>

</RelativeLayout>

布局文件没什么好说的,是个程序员都能看得懂。讲解一下的地方就是,进度条我是用SeekBar+ImageView实现的,用ImageView作为SeekBar的背景。但是有个问题,就是SeekBar他的左右两边他获取不到,我是让美工把左右两边给做透明,还有一种比较笨的办法就是你可以让他偏移一定的距离。

好了接下来是正文,我们来看看Activity的代码:

package com.example.brightness;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

public class SettingActivity extends Activity {

    /*亮度加减按钮*/
    private ImageButton img_btn_brightness_plus,img_btn_brightness_reduce;
    /*亮度SeekBar*/


    private int normal;
    /*最大进度条值*/
    private int maxProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* 去掉标题 */
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_setting);

        getView();
        updateBrightness();//亮度

        // 进度条绑定最大亮度,255是最大亮度
        sb_brightness.setMax(255);
        // 取得当前亮度
        int normal = Settings.System.getInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS, 255);
        // 进度条绑定当前亮度
        sb_brightness.setProgress(normal);

    }
    /**
     * 获取所有控件
     * */
    private void getView(){

        /*获取亮度加按钮*/
        img_btn_brightness_plus = (ImageButton) findViewById(R.id.img_btn_brightness_plus);
        /*获取亮度减按钮*/
        img_btn_brightness_reduce = (ImageButton) findViewById(R.id.img_btn_brightness_reduce);
        /*获取亮度SeekBar*/
        sb_brightness = (SeekBar) findViewById(R.id.sb_brightness);
    }
    /**
     * 亮度
     * */
    private void updateBrightness(){
        sb_brightness.setEnabled(false);
        final int i = 17;
        img_btn_brightness_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (sb_brightness.getProgress() <= 255) {
                    sb_brightness.setProgress(sb_brightness.getProgress()+i);
                   // 取得当前进度
                    int tmpInt = sb_brightness.getProgress();

                    // 当进度小于80时,设置成80,防止太黑看不见的后果。
                    if (tmpInt < 80) {
                        tmpInt = 80;
                    }

                    // 根据当前进度改变亮度
                    Settings.System.putInt(getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS, tmpInt);
                    tmpInt = Settings.System.getInt(getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS, -1);
                    WindowManager.LayoutParams wl = getWindow().getAttributes();

                    float tmpFloat = (float) tmpInt / 255;
                    if (tmpFloat > 0 && tmpFloat <= 1) {
                        wl.screenBrightness = tmpFloat;
                    }
                    getWindow().setAttributes(wl);

                }
            }
        });
        img_btn_brightness_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sb_brightness.getProgress() >= 17){
                    sb_brightness.setProgress(sb_brightness.getProgress()-i);
                    // 取得当前进度
                    int tmpInt = sb_brightness.getProgress();
                    // 当进度小于80时,设置成80,防止太黑看不见的后果。
                    if (tmpInt < 80) {
                        tmpInt = 80;
                    }

                    // 根据当前进度改变亮度
                    Settings.System.putInt(getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS, tmpInt);
                    tmpInt = Settings.System.getInt(getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS, -1);
                    WindowManager.LayoutParams wl = getWindow().getAttributes();

                    float tmpFloat = (float) tmpInt / 255;
                    if (tmpFloat > 0 && tmpFloat <= 1) {
                        wl.screenBrightness = tmpFloat;
                    }
                    getWindow().setAttributes(wl);

                }
            }
        });
    }
}

大体的思路和代码呢,都在这里了。每个人都有自己不同的理解,但是万变不离其宗,只要最终的功能实现了,那就是好代码。(为了给自己找个借口)如果大家有什么好的意见或者建议,多多提出。本人会多加改进,以后自己有什么好玩的也会放出来,和大家一起分享。

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值