1.用一个dailog当遮罩层,将它设置为全屏,通过 WindowManager.layoutParam lp 的属性 设置为 顶层 不影响下层触碰
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.flags =WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; //设置不影响下层的触碰
lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; //设置顶层
这样设置的话,就会实现 一个遮罩层 跨 app 都进行遮罩,只要 app进程在,遮罩就在。
2.设置 dailog的样式
dialog=new Dialog(this,R.style.dialog_translucent);
<style name="dialog_translucent" parent="@android:style/Theme.Dialog">
- "android:windowFrame"
>@null
- "android:windowIsFloating">true
- "android:windowIsTranslucent">false
- "android:windowNoTitle">true
- "android:windowBackground">@android:color/transparent
- "android:backgroundDimEnabled">false
- "android:backgroundDimAmount">0.6
- "android:windowContentOverlay">@null
style>
3.护眼app的颜色调节 蓝光过滤(护眼模式) 就是对遮罩层 的蓝原色进行减少 通过下面关键语句进行 遮罩层颜色的设置
iv_main.setBackgroundColor(Color.argb(alapha,red,green,blue));
代码分别设置了 蓝绿光 以及亮度调节 蓝绿光过滤调节是分别对 rgb 三原色的减少 亮度调节是对 alapha 数值的增大 将亮度变小
4.完整代码如下 自行粘贴 运行
ReadActivity.java
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 alapha=100;
private int blueProgress=0;
private int redProgress=0;
private int greenProgress=0;
private int alaphaProgress=0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read);
initView();
openAleterWindow();
initData();
//// permission();
initListner();
}
private void initView() {
sb_ld=findViewById(R.id.sb_ld);
sb_r=findViewById(R.id.sb_r);
sb_b=findViewById(R.id.sb_b);
sb_g=findViewById(R.id.sb_g);
}
private void initListner() {
sb_ld.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int add=progress-alaphaProgress;
alapha=alapha+add;
alaphaProgress=progress;
Log.e("this","alapha"+alapha);
iv_main.setBackgroundColor(Color.argb(alapha, red, green, blue));
}
@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) {
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 initData(){ //每当打开acitivy 对 sharedprefernces 初始化
SharedPreferences myPreference=getSharedPreferences("myPreference", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPreference.edit();
editor.putInt("alapha",100);
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);
alapha=preferences.getInt("alapha",100);
red=preferences.getInt("red",100);
green=preferences.getInt("green",100);
blue=preferences.getInt("blue",100);
sb_ld.setProgress(alapha-100);
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);
}
public void saveData(){
SharedPreferences myPreference=getSharedPreferences("myPreference", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPreference.edit();
if(alapha>=0&&alapha<=255&&red>=0&&red<=100&&blue>=0&blue<=100&&green>=0&&green<=100) {
editor.putInt("alapha", alapha);
editor.putInt("red", red);
editor.putInt("blue", blue);
editor.putInt("green", green);
editor.commit();
}
}
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;
lp.flags =WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; //设置不影响下层的触碰
if(Build.VERSION.SDK_INT>=26){
lp.type=WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else{
lp.type=WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
}
dialog.getWindow().setAttributes(lp);
dialog.show();
iv_main=dialog.findViewById(R.id.ll_main);
getData();
}
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();
Float ld=Float.valueOf(brightness) * (1f / 100f);
Log.e("this","ld:"+ld);
lp.screenBrightness = ld;
getWindow().setAttributes(lp);
}
}
read.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:background="@color/colorPrimary"
>
<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>
dailog.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
RelativeLayout>
5.写的很仓促,有不完善的地方,欢迎博客下方留言