PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/util/ViewUtil.kt

87 lines
3.5 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-01-28 10:43:44 +00:00
package code.name.monkey.retromusic.util
2019-12-01 11:28:57 +00:00
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.PorterDuff
2019-01-28 10:45:51 +00:00
import android.graphics.drawable.LayerDrawable
2019-01-28 10:43:44 +00:00
import android.os.Build
import android.view.View
2019-12-01 11:28:57 +00:00
import android.widget.ProgressBar
import android.widget.SeekBar
2020-02-21 18:18:11 +00:00
import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat.SRC_IN
2019-01-28 10:43:44 +00:00
import androidx.core.view.ViewCompat
2019-12-01 11:28:57 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.MaterialValueHelper
2019-01-28 10:43:44 +00:00
object ViewUtil {
2019-12-01 11:28:57 +00:00
const val RETRO_MUSIC_ANIM_TIME = 1000
fun setProgressDrawable(progressSlider: SeekBar, newColor: Int, thumbTint: Boolean = false) {
if (thumbTint) {
progressSlider.thumbTintList = ColorStateList.valueOf(newColor)
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
val layerDrawable = progressSlider.progressDrawable as LayerDrawable
val progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress)
progressDrawable.setColorFilter(newColor, PorterDuff.Mode.SRC_IN)
} else {
progressSlider.progressTintList = ColorStateList.valueOf(newColor)
}
}
fun setProgressDrawable(progressSlider: ProgressBar, newColor: Int) {
2020-02-21 18:18:11 +00:00
val layerDrawable = progressSlider.progressDrawable as LayerDrawable
2019-12-01 11:28:57 +00:00
2020-02-21 18:18:11 +00:00
val progress = layerDrawable.findDrawableByLayerId(android.R.id.progress)
progress.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(newColor, SRC_IN)
2019-12-01 11:28:57 +00:00
2020-02-21 18:18:11 +00:00
val background = layerDrawable.findDrawableByLayerId(android.R.id.background)
2019-12-01 11:28:57 +00:00
val primaryColor = ATHUtil.resolveColor(progressSlider.context, android.R.attr.windowBackground)
2020-02-21 18:18:11 +00:00
background.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
MaterialValueHelper.getPrimaryDisabledTextColor(
progressSlider.context,
ColorUtil.isColorLight(primaryColor)
), SRC_IN
2019-12-01 11:28:57 +00:00
)
2020-02-21 18:18:11 +00:00
val secondaryProgress = layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress)
secondaryProgress?.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(ColorUtil.withAlpha(newColor, 0.65f), SRC_IN)
2019-12-01 11:28:57 +00:00
}
fun hitTest(v: View, x: Int, y: Int): Boolean {
val tx = (ViewCompat.getTranslationX(v) + 0.5f).toInt()
val ty = (ViewCompat.getTranslationY(v) + 0.5f).toInt()
val left = v.left + tx
val right = v.right + tx
val top = v.top + ty
val bottom = v.bottom + ty
return x in left..right && y >= top && y <= bottom
}
fun convertDpToPixel(dp: Float, resources: Resources): Float {
val metrics = resources.displayMetrics
return dp * metrics.density
}
2019-01-28 10:43:44 +00:00
}