android屏幕颜色过滤调节(可用于护眼模式)
16lz
2021-01-26
实现思路
1.android 创建 透明 dailog 对dailog 进行 背景 颜色的改变 达到遮罩 下方内容的效果
2.颜色的 过滤调节 算法 就是对 分别对 rgb三原色 的增加与减少
3.改变颜色核心代码:
ImageView iv_main=dialog.findViewById(R.id.ll_main); iv_main.setBackgroundColor(Color.argb(alapha,red,green,blue));
实现的界面图
完整acitivty代码:
public class ReadActivity extends AppCompatActivity{ SeekBar sb_ld,sb_r,sb_b,sb_g; ImageView iv_main; Dialog dialog; //红蓝绿 三原色的初始值 private int blue=100; private int red=100; private int green=100; private int ld=0; private int alapha=100; private int blueProgress=0; private int redProgress=0; private int greenProgress=0; private int ldProgress=0; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.read); initData();// permission(); initListner(); } private void initListner() { Button bt_test=findViewById(R.id.bt_test); bt_test.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openAleterWindow(); } }); } public void initData(){ //每当打开acitivy 对 sharedprefernces 初始化 SharedPreferences myPreference=getSharedPreferences("myPreference", Context.MODE_PRIVATE); SharedPreferences.Editor editor = myPreference.edit(); editor.putInt("ld", 0); editor.putInt("red", 100); editor.putInt("blue", 100); editor.putInt("green", 100); editor.commit(); } public void getData(){ //获取 存储 sharePrefrence 保存的三原色值 SharedPreferences preferences=getSharedPreferences("myPreference", Context.MODE_PRIVATE); ld=preferences.getInt("ld",0); red=preferences.getInt("red",100); green=preferences.getInt("green",100); blue=preferences.getInt("blue",100); Log.e("this","get"+ld+red+green+blue); sb_ld.setProgress(ld); sb_r.setProgress(100-red); sb_g.setProgress(100-green); sb_b.setProgress(100-blue); iv_main.setBackgroundColor(Color.argb(alapha,red,green,blue)); changeAppBrightness(ld); Log.e("this","getR"+red); } private void openAleterWindow() { //打开 dailog 窗口 对 dailog 初始化 dialog=new Dialog(this,R.style.dialog_translucent); dialog.setContentView(R.layout.dailog); // 全屏 WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; dialog.getWindow().setAttributes(lp); dialog.show(); final ImageView iv_guanbi=dialog.findViewById(R.id.iv_guanbi); iv_guanbi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //保存 配置 SharedPreferences myPreference=getSharedPreferences("myPreference", Context.MODE_PRIVATE); SharedPreferences.Editor editor = myPreference.edit(); if(ld>=0&&ld<=100&&red>=0&&red<=100&&blue>=0&blue<=100&&green>=0&&green<=100) { editor.putInt("ld", ld); editor.putInt("red", red); editor.putInt("blue", blue); editor.putInt("green", green); editor.commit(); } Log.e("this","data"+ld+red+blue+green); dialog.dismiss(); } }); sb_ld=dialog.findViewById(R.id.sb_ld); sb_r=dialog.findViewById(R.id.sb_r); sb_b=dialog.findViewById(R.id.sb_b); sb_g=dialog.findViewById(R.id.sb_g); iv_main=dialog.findViewById(R.id.ll_main); getData(); sb_ld.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { int add=progress-ldProgress; ld=ld+add; ldProgress=progress; Log.e("this","ld:"+ld); changeAppBrightness(ld); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); sb_r.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { int add=progress-redProgress; red=red-add; redProgress=progress; Log.e("this","red:"+red); iv_main.setBackgroundColor(Color.argb(alapha,red,green,blue)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); sb_b.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {// View view=LayoutInflater.from(ReadActivity.this).inflate(R.layout.dailog,null);// view.setBackgroundColor(Color.argb(1,255,0,0));// dialog.setContentView(view); int add=progress-blueProgress; blue=blue-add; blueProgress=progress; Log.e("this","blue:"+blue); iv_main.setBackgroundColor(Color.argb(alapha,red,green,blue)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); sb_g.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { int add=progress-greenProgress; green=green-add; greenProgress=progress; Log.e("this","green:"+green); iv_main.setBackgroundColor(Color.argb(alapha,red,green,blue)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } public void permission(){ if (Build.VERSION.SDK_INT >= 23) { if(!Settings.canDrawOverlays(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); startActivity(intent); } }} public void changeAppBrightness(int brightness) { //改变系统屏幕亮度 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); getWindow().setAttributes(lp); }}
dailog.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv_guanbi" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/guanbi" /> <ImageView android:id="@+id/ll_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_ld" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="亮度调节:" /> <SeekBar android:id="@+id/sb_ld" android:layout_width="match_parent" android:layout_height="wrap_content" /> LinearLayout> <LinearLayout android:id="@+id/ll_r" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="红光调节:" /> <SeekBar android:id="@+id/sb_r" android:layout_width="match_parent" android:layout_height="wrap_content" /> LinearLayout> <LinearLayout android:id="@+id/ll_b" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="蓝光调节:" /> <SeekBar android:id="@+id/sb_b" android:layout_width="match_parent" android:layout_height="wrap_content" /> LinearLayout> <LinearLayout android:id="@+id/ll_g" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿光调节:" /> <SeekBar android:id="@+id/sb_g" android:layout_width="match_parent" android:layout_height="wrap_content" /> LinearLayout> LinearLayout>RelativeLayout>
read.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/bt_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调节" />RelativeLayout>
更多相关文章
- Android(安卓)颜色渲染(五) LinearGradient线性渲染
- Android(安卓)自定义 View - 颜色选取器(水平、竖直)
- Android中TextView文字设置不同的颜色
- LinearLayout半透明效果
- Android(安卓)开源组件PagerBottomTabStrip 快速构建底部导航栏
- Android数据手册:Android颜色码制表
- 布局添加下划线
- Android资源文件-Shape
- RichTextUtil Android(安卓)文字高亮可点击工具类分享和示例