PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/views/ColorIconsImageView.kt

62 lines
2.4 KiB
Kotlin
Raw Normal View History

2019-03-03 09:29:03 +00:00
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
2019-02-17 18:03:12 +00:00
package code.name.monkey.retromusic.views
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
2020-04-15 08:49:28 +00:00
import androidx.core.content.ContextCompat
2019-10-09 14:34:01 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
2020-04-15 08:49:28 +00:00
import code.name.monkey.appthemehelper.util.ColorUtil
2019-02-17 18:03:12 +00:00
import code.name.monkey.retromusic.R
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2019-10-10 18:02:59 +00:00
import code.name.monkey.retromusic.util.RetroColorUtil
2019-02-17 18:03:12 +00:00
2019-10-10 17:53:41 +00:00
2020-02-25 13:15:23 +00:00
class ColorIconsImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = -1
) : AppCompatImageView(context, attrs, defStyleAttr) {
2019-02-17 18:03:12 +00:00
2020-02-25 13:15:23 +00:00
init {
2019-02-17 18:03:12 +00:00
// Load the styled attributes and set their properties
2020-02-25 13:15:23 +00:00
val attributes =
context.obtainStyledAttributes(attrs, R.styleable.ColorIconsImageView, 0, 0)
val color =
2020-08-21 14:19:15 +00:00
attributes.getColor(R.styleable.ColorIconsImageView_iconBackgroundColor, Color.RED)
2019-10-10 17:53:41 +00:00
setIconBackgroundColor(color)
2019-02-17 18:03:12 +00:00
attributes.recycle()
}
2019-12-10 17:36:20 +00:00
fun setIconBackgroundColor(color: Int) {
2020-04-15 08:49:28 +00:00
background = ContextCompat.getDrawable(context, R.drawable.color_circle_gradient)
2020-06-06 18:57:28 +00:00
if (ATHUtil.isWindowBackgroundDark(context) && PreferenceUtil.isDesaturatedColor) {
2019-10-10 18:02:59 +00:00
val desaturatedColor = RetroColorUtil.desaturateColor(color, 0.4f)
2019-10-10 17:53:41 +00:00
backgroundTintList = ColorStateList.valueOf(desaturatedColor)
2020-02-25 13:15:23 +00:00
imageTintList =
ColorStateList.valueOf(ATHUtil.resolveColor(context, R.attr.colorSurface))
2019-10-09 14:34:01 +00:00
} else {
2020-04-15 08:49:28 +00:00
backgroundTintList = ColorStateList.valueOf(ColorUtil.adjustAlpha(color, 0.22f))
imageTintList = ColorStateList.valueOf(ColorUtil.withAlpha(color, 0.75f))
2019-10-09 14:34:01 +00:00
}
2019-02-17 18:03:12 +00:00
requestLayout()
invalidate()
}
}