通过Button按钮调节亮度
2016-07-07 18:34
351 查看
最近项目中需要一个通过Button按钮来调节亮度的功能,而在网上呢几乎都是直接滑动进度条来实现的。因此写这个帖子,一是为了自己做个笔记;二是为了给大家提供一个资源。稍微有点混乱,刚开始写。大家见谅。
好了,先放上布局文件,我们慢慢开始了解。
布局文件没什么好说的,是个程序员都能看得懂。讲解一下的地方就是,进度条我是用SeekBar+ImageView实现的,用ImageView作为SeekBar的背景。但是有个问题,就是SeekBar他的左右两边他获取不到,我是让美工把左右两边给做透明,还有一种比较笨的办法就是你可以让他偏移一定的距离。
好了接下来是正文,我们来看看Activity的代码:
大体的思路和代码呢,都在这里了。每个人都有自己不同的理解,但是万变不离其宗,只要最终的功能实现了,那就是好代码。(为了给自己找个借口)如果大家有什么好的意见或者建议,多多提出。本人会多加改进,以后自己有什么好玩的也会放出来,和大家一起分享。
好了,先放上布局文件,我们慢慢开始了解。
<?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); } } }); } }
大体的思路和代码呢,都在这里了。每个人都有自己不同的理解,但是万变不离其宗,只要最终的功能实现了,那就是好代码。(为了给自己找个借口)如果大家有什么好的意见或者建议,多多提出。本人会多加改进,以后自己有什么好玩的也会放出来,和大家一起分享。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析