Android 小說閱讀護眼模式
實現方案:採用全局dialog 覆蓋APP 懸浮在 其他APP之上,給dialog設置護眼色值
自定義護眼模式dialog
public class EyeProtectionDialog extends Dialog {
ImageView iv;
public EyeProtectionDialog(@NonNull Context context) {
super(context, R.style.eye_style);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
window.getDecorView().setPadding(0, 0, 0, 0);
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| 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;
}
window.setAttributes(lp);
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eye_layout);
iv = findViewById(R.id.iv_eye);
}
@Override
public void show() {
super.show();
if (iv != null) {
iv.setBackgroundColor(getColor(30));
}
}
/**
* 設置護眼色
* @param blueFilterPercent
* @return
*/
public @ColorInt
int getColor(int blueFilterPercent) {
int realFilter = blueFilterPercent;
if (realFilter < 10) {
realFilter = 10;
} else if (realFilter > 80) {
realFilter = 80;
}
int a = (int) (realFilter / 80f * 180);
int r = (int) (200 - (realFilter / 80f) * 190);
int g = (int) (180 - (realFilter / 80f) * 170);
int b = (int) (60 - realFilter / 80f * 60);
return Color.argb(a, r, g, b);
}
}
dialog-style
<style name="eye_style" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item><!-- 邊框 -->
<item name="android:windowIsFloating">true</item><!-- 是否懸浮在activity上 -->
<item name="android:windowIsTranslucent">false</item><!-- 半透明 -->
<item name="android:windowNoTitle">true</item><!-- 無標題 -->
<item name="android:windowBackground">@android:color/transparent</item><!-- 背景透明 -->
<item name="android:backgroundDimEnabled">false</item><!-- 模糊 -->
<item name="android:backgroundDimAmount">0.6</item><!-- 灰度 -->
<item name="android:windowContentOverlay">@null</item><!-- 對話框是否有遮蓋 -->
</style>
eye_layout
<?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_eye"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
</RelativeLayout>
權限以及展示
public void permission() {
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(getContext())) {
Uri packageURI = Uri.parse("package:" + ReaderApplication.getInstance().getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, packageURI);
startActivity(intent);
} else {
EyeProtectionDialog eyeProtectionDialog = new EyeProtectionDialog(getContext());
eyeProtectionDialog.show();
}
} else {
EyeProtectionDialog eyeProtectionDialog = new EyeProtectionDialog(getContext());
eyeProtectionDialog.show();
}
}