feat: 防止背景颜色透明,出现重影,花屏

pull/254/head
gedoor 4 years ago
parent a7a9f2e61c
commit ce5f8bc7b8
  1. 45
      app/src/main/java/io/legado/app/App.kt
  2. 39
      app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt

@ -20,6 +20,7 @@ import io.legado.app.help.ActivityHelp
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.CrashHandler import io.legado.app.help.CrashHandler
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.ThemeStore
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
@ -83,30 +84,38 @@ class App : Application() {
.apply() .apply()
} }
AppConfig.isNightTheme -> { AppConfig.isNightTheme -> {
val primary =
getPrefInt(PreferKey.cNPrimary, getCompatColor(R.color.md_blue_grey_600))
val accent =
getPrefInt(PreferKey.cNAccent, getCompatColor(R.color.md_deep_orange_800))
val background =
getPrefInt(PreferKey.cNBackground, getCompatColor(R.color.shine_color))
val bBackground =
getPrefInt(PreferKey.cNBBackground, getCompatColor(R.color.md_grey_850))
ThemeStore.editTheme(this) ThemeStore.editTheme(this)
.coloredNavigationBar(true) .coloredNavigationBar(true)
.primaryColor( .primaryColor(ColorUtils.withAlpha(primary, 1f))
getPrefInt(PreferKey.cNPrimary, getCompatColor(R.color.md_blue_grey_600)) .accentColor(ColorUtils.withAlpha(accent, 1f))
).accentColor( .backgroundColor(ColorUtils.withAlpha(background, 1f))
getPrefInt(PreferKey.cNAccent, getCompatColor(R.color.md_deep_orange_800)) .bottomBackground(ColorUtils.withAlpha(bBackground, 1f))
).backgroundColor( .apply()
getPrefInt(PreferKey.cNBackground, getCompatColor(R.color.shine_color))
).bottomBackground(
getPrefInt(PreferKey.cNBBackground, getCompatColor(R.color.md_grey_850))
).apply()
} }
else -> { else -> {
val primary =
getPrefInt(PreferKey.cPrimary, getCompatColor(R.color.md_indigo_800))
val accent =
getPrefInt(PreferKey.cAccent, getCompatColor(R.color.md_red_600))
val background =
getPrefInt(PreferKey.cBackground, getCompatColor(R.color.md_grey_100))
val bBackground =
getPrefInt(PreferKey.cBBackground, getCompatColor(R.color.md_grey_200))
ThemeStore.editTheme(this) ThemeStore.editTheme(this)
.coloredNavigationBar(true) .coloredNavigationBar(true)
.primaryColor( .primaryColor(ColorUtils.withAlpha(primary, 1f))
getPrefInt(PreferKey.cPrimary, getCompatColor(R.color.md_indigo_800)) .accentColor(ColorUtils.withAlpha(accent, 1f))
).accentColor( .backgroundColor(ColorUtils.withAlpha(background, 1f))
getPrefInt(PreferKey.cAccent, getCompatColor(R.color.md_red_600)) .bottomBackground(ColorUtils.withAlpha(bBackground, 1f))
).backgroundColor( .apply()
getPrefInt(PreferKey.cBackground, getCompatColor(R.color.md_grey_100))
).bottomBackground(
getPrefInt(PreferKey.cBBackground, getCompatColor(R.color.md_grey_200))
).apply()
} }
} }
} }

@ -14,6 +14,7 @@ import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import com.jaredrummler.android.colorpicker.* import com.jaredrummler.android.colorpicker.*
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ColorUtils
class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs),
ColorPickerDialogListener { ColorPickerDialogListener {
@ -22,8 +23,9 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
private val sizeLarge = 1 private val sizeLarge = 1
private var onShowDialogListener: OnShowDialogListener? = null private var onShowDialogListener: OnShowDialogListener? = null
private var color = Color.BLACK private var mColor = Color.BLACK
private var showDialog: Boolean = false private var showDialog: Boolean = false
@ColorPickerDialog.DialogType @ColorPickerDialog.DialogType
private var dialogType: Int = 0 private var dialogType: Int = 0
private var colorShape: Int = 0 private var colorShape: Int = 0
@ -42,7 +44,8 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
val a = context.obtainStyledAttributes(attrs, R.styleable.ColorPreference) val a = context.obtainStyledAttributes(attrs, R.styleable.ColorPreference)
showDialog = a.getBoolean(R.styleable.ColorPreference_cpv_showDialog, true) showDialog = a.getBoolean(R.styleable.ColorPreference_cpv_showDialog, true)
dialogType = a.getInt(R.styleable.ColorPreference_cpv_dialogType, ColorPickerDialog.TYPE_PRESETS) dialogType =
a.getInt(R.styleable.ColorPreference_cpv_dialogType, ColorPickerDialog.TYPE_PRESETS)
colorShape = a.getInt(R.styleable.ColorPreference_cpv_colorShape, ColorShape.CIRCLE) colorShape = a.getInt(R.styleable.ColorPreference_cpv_colorShape, ColorShape.CIRCLE)
allowPresets = a.getBoolean(R.styleable.ColorPreference_cpv_allowPresets, true) allowPresets = a.getBoolean(R.styleable.ColorPreference_cpv_allowPresets, true)
allowCustom = a.getBoolean(R.styleable.ColorPreference_cpv_allowCustom, true) allowCustom = a.getBoolean(R.styleable.ColorPreference_cpv_allowCustom, true)
@ -50,7 +53,8 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
showColorShades = a.getBoolean(R.styleable.ColorPreference_cpv_showColorShades, true) showColorShades = a.getBoolean(R.styleable.ColorPreference_cpv_showColorShades, true)
previewSize = a.getInt(R.styleable.ColorPreference_cpv_previewSize, sizeNormal) previewSize = a.getInt(R.styleable.ColorPreference_cpv_previewSize, sizeNormal)
val presetsResId = a.getResourceId(R.styleable.ColorPreference_cpv_colorPresets, 0) val presetsResId = a.getResourceId(R.styleable.ColorPreference_cpv_colorPresets, 0)
dialogTitle = a.getResourceId(R.styleable.ColorPreference_cpv_dialogTitle, R.string.cpv_default_title) dialogTitle =
a.getResourceId(R.styleable.ColorPreference_cpv_dialogTitle, R.string.cpv_default_title)
presets = if (presetsResId != 0) { presets = if (presetsResId != 0) {
context.resources.getIntArray(presetsResId) context.resources.getIntArray(presetsResId)
} else { } else {
@ -67,7 +71,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
override fun onClick() { override fun onClick() {
super.onClick() super.onClick()
if (onShowDialogListener != null) { if (onShowDialogListener != null) {
onShowDialogListener!!.onShowColorPickerDialog(title as String, color) onShowDialogListener!!.onShowColorPickerDialog(title as String, mColor)
} else if (showDialog) { } else if (showDialog) {
val dialog = ColorPickerDialogCompat.newBuilder() val dialog = ColorPickerDialogCompat.newBuilder()
.setDialogType(dialogType) .setDialogType(dialogType)
@ -78,7 +82,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
.setAllowCustom(allowCustom) .setAllowCustom(allowCustom)
.setShowAlphaSlider(showAlphaSlider) .setShowAlphaSlider(showAlphaSlider)
.setShowColorShades(showColorShades) .setShowColorShades(showColorShades)
.setColor(color) .setColor(mColor)
.create() .create()
dialog.setColorPickerDialogListener(this) dialog.setColorPickerDialogListener(this)
getActivity().supportFragmentManager getActivity().supportFragmentManager
@ -111,10 +115,12 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
} }
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
val v = io.legado.app.ui.widget.prefs.Preference.bindView<ColorPanelView>(context, holder, icon, title, summary, widgetLayoutResource, val v = io.legado.app.ui.widget.prefs.Preference.bindView<ColorPanelView>(
io.legado.app.R.id.cpv_preference_preview_color_panel, 30, 30) context, holder, icon, title, summary, widgetLayoutResource,
io.legado.app.R.id.cpv_preference_preview_color_panel, 30, 30
)
if (v is ColorPanelView) { if (v is ColorPanelView) {
v.color = color v.color = mColor
} }
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
} }
@ -122,10 +128,10 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
override fun onSetInitialValue(defaultValue: Any?) { override fun onSetInitialValue(defaultValue: Any?) {
super.onSetInitialValue(defaultValue) super.onSetInitialValue(defaultValue)
if (defaultValue is Int) { if (defaultValue is Int) {
color = (defaultValue as Int?)!! mColor = if (!showAlphaSlider) ColorUtils.withAlpha(defaultValue, 1f) else defaultValue
persistInt(color) persistInt(mColor)
} else { } else {
color = getPersistedInt(-0x1000000) mColor = getPersistedInt(-0x1000000)
} }
} }
@ -147,8 +153,8 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
* @param color The newly selected color * @param color The newly selected color
*/ */
fun saveValue(@ColorInt color: Int) { fun saveValue(@ColorInt color: Int) {
this.color = color mColor = if (showAlphaSlider) color else ColorUtils.withAlpha(color, 1f)
persistInt(this.color) persistInt(mColor)
notifyChanged() notifyChanged()
callChangeListener(color) callChangeListener(color)
} }
@ -231,17 +237,23 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
class Builder internal constructor() { class Builder internal constructor() {
internal var colorPickerDialogListener: ColorPickerDialogListener? = null internal var colorPickerDialogListener: ColorPickerDialogListener? = null
@StringRes @StringRes
internal var dialogTitle = R.string.cpv_default_title internal var dialogTitle = R.string.cpv_default_title
@StringRes @StringRes
internal var presetsButtonText = R.string.cpv_presets internal var presetsButtonText = R.string.cpv_presets
@StringRes @StringRes
internal var customButtonText = R.string.cpv_custom internal var customButtonText = R.string.cpv_custom
@StringRes @StringRes
internal var selectedButtonText = R.string.cpv_select internal var selectedButtonText = R.string.cpv_select
@DialogType @DialogType
internal var dialogType = TYPE_PRESETS internal var dialogType = TYPE_PRESETS
internal var presets = MATERIAL_COLORS internal var presets = MATERIAL_COLORS
@ColorInt @ColorInt
internal var color = Color.BLACK internal var color = Color.BLACK
internal var dialogId = 0 internal var dialogId = 0
@ -249,6 +261,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
internal var allowPresets = true internal var allowPresets = true
internal var allowCustom = true internal var allowCustom = true
internal var showColorShades = true internal var showColorShades = true
@ColorShape @ColorShape
internal var colorShape = ColorShape.CIRCLE internal var colorShape = ColorShape.CIRCLE

Loading…
Cancel
Save