From 29e9008980372d99c3253e1f8adea6fa8a71c83c Mon Sep 17 00:00:00 2001 From: h4h13 Date: Thu, 21 Nov 2019 21:34:43 +0530 Subject: [PATCH] Fix Android 5 progress drawable color --- .../name/monkey/retromusic/util/ViewUtil.kt | 153 ++++++++++-------- .../main/res/drawable/color_progress_seek.xml | 17 +- 2 files changed, 89 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/util/ViewUtil.kt b/app/src/main/java/code/name/monkey/retromusic/util/ViewUtil.kt index 5d0c2539..fd72748a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/ViewUtil.kt +++ b/app/src/main/java/code/name/monkey/retromusic/util/ViewUtil.kt @@ -14,96 +14,119 @@ package code.name.monkey.retromusic.util -import android.animation.Animator -import android.animation.ArgbEvaluator -import android.animation.ObjectAnimator +import android.animation.* import android.content.Context -import android.content.res.ColorStateList -import android.content.res.Resources -import android.graphics.Color -import android.graphics.PorterDuff +import android.content.res.* +import android.graphics.* import android.graphics.drawable.LayerDrawable import android.os.Build import android.view.View import android.view.animation.PathInterpolator -import android.widget.ProgressBar -import android.widget.SeekBar +import android.widget.* import androidx.annotation.ColorInt import androidx.core.view.ViewCompat import code.name.monkey.appthemehelper.ThemeStore -import code.name.monkey.appthemehelper.util.ATHUtil -import code.name.monkey.appthemehelper.util.ColorUtil -import code.name.monkey.appthemehelper.util.MaterialValueHelper +import code.name.monkey.appthemehelper.util.* import code.name.monkey.retromusic.R import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView - object ViewUtil { - const val RETRO_MUSIC_ANIM_TIME = 1000 + const val RETRO_MUSIC_ANIM_TIME = 1000 - fun setProgressDrawable(progressSlider: SeekBar, newColor: Int, thumbTint: Boolean = false) { + fun setProgressDrawable(progressSlider: SeekBar, newColor: Int, thumbTint: Boolean = false) { - if (thumbTint) { - progressSlider.thumbTintList = ColorStateList.valueOf(newColor) - } - progressSlider.progressTintList = ColorStateList.valueOf(newColor) - } + 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) { + fun setProgressDrawable(progressSlider: ProgressBar, newColor: Int) { - val ld = progressSlider.progressDrawable as LayerDrawable + val ld = progressSlider.progressDrawable as LayerDrawable - val progress = ld.findDrawableByLayerId(android.R.id.progress) - progress.setColorFilter(newColor, PorterDuff.Mode.SRC_IN) + val progress = ld.findDrawableByLayerId(android.R.id.progress) + progress.setColorFilter(newColor, PorterDuff.Mode.SRC_IN) - val background = ld.findDrawableByLayerId(android.R.id.background) - val primaryColor = ATHUtil.resolveColor(progressSlider.context, R.attr.colorPrimary) - background.setColorFilter(MaterialValueHelper.getPrimaryDisabledTextColor(progressSlider.context, ColorUtil.isColorLight(primaryColor)), PorterDuff.Mode.SRC_IN) + val background = ld.findDrawableByLayerId(android.R.id.background) + val primaryColor = ATHUtil.resolveColor(progressSlider.context, R.attr.colorPrimary) + background.setColorFilter( + MaterialValueHelper.getPrimaryDisabledTextColor( + progressSlider.context, ColorUtil.isColorLight( + primaryColor + ) + ), PorterDuff.Mode.SRC_IN + ) - val secondaryProgress = ld.findDrawableByLayerId(android.R.id.secondaryProgress) - secondaryProgress?.setColorFilter(ColorUtil.withAlpha(newColor, 0.65f), PorterDuff.Mode.SRC_IN) - } + val secondaryProgress = ld.findDrawableByLayerId(android.R.id.secondaryProgress) + secondaryProgress?.setColorFilter( + ColorUtil.withAlpha(newColor, 0.65f), PorterDuff.Mode.SRC_IN + ) + } - private fun createColorAnimator(target: Any, propertyName: String, @ColorInt startColor: Int, @ColorInt endColor: Int): Animator { - val animator: ObjectAnimator - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - animator = ObjectAnimator.ofArgb(target, propertyName, startColor, endColor) - } else { - animator = ObjectAnimator.ofInt(target, propertyName, startColor, endColor) - animator.setEvaluator(ArgbEvaluator()) - } + private fun createColorAnimator( + target: Any, propertyName: String, @ColorInt startColor: Int, @ColorInt endColor: Int + ): Animator { + val animator: ObjectAnimator + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + animator = ObjectAnimator.ofArgb(target, propertyName, startColor, endColor) + } else { + animator = ObjectAnimator.ofInt(target, propertyName, startColor, endColor) + animator.setEvaluator(ArgbEvaluator()) + } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - animator.interpolator = PathInterpolator(0.4f, 0f, 1f, 1f) - } - animator.duration = RETRO_MUSIC_ANIM_TIME.toLong() - return animator - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + animator.interpolator = PathInterpolator(0.4f, 0f, 1f, 1f) + } + animator.duration = RETRO_MUSIC_ANIM_TIME.toLong() + return animator + } - 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 + 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 - } + return x in left..right && y >= top && y <= bottom + } - fun setUpFastScrollRecyclerViewColor(context: Context, - recyclerView: FastScrollRecyclerView, accentColor: Int = ThemeStore.accentColor(context)) { - recyclerView.setPopupBgColor(accentColor) - recyclerView.setPopupTextColor(MaterialValueHelper.getPrimaryTextColor(context, ColorUtil.isColorLight(accentColor))) - recyclerView.setThumbColor(accentColor) - recyclerView.setTrackColor(Color.TRANSPARENT) - recyclerView.setTrackColor(ColorUtil.withAlpha(ATHUtil.resolveColor(context, R.attr.colorControlNormal), 0.12f)) + fun setUpFastScrollRecyclerViewColor( + context: Context, + recyclerView: FastScrollRecyclerView, + accentColor: Int = ThemeStore.accentColor(context) + ) { + recyclerView.setPopupBgColor(accentColor) + recyclerView.setPopupTextColor( + MaterialValueHelper.getPrimaryTextColor( + context, ColorUtil.isColorLight( + accentColor + ) + ) + ) + recyclerView.setThumbColor(accentColor) + recyclerView.setTrackColor(Color.TRANSPARENT) + recyclerView.setTrackColor( + ColorUtil.withAlpha( + ATHUtil.resolveColor( + context, R.attr.colorControlNormal + ), 0.12f + ) + ) - } + } - fun convertDpToPixel(dp: Float, resources: Resources): Float { - val metrics = resources.displayMetrics - return dp * metrics.density - } + fun convertDpToPixel(dp: Float, resources: Resources): Float { + val metrics = resources.displayMetrics + return dp * metrics.density + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/color_progress_seek.xml b/app/src/main/res/drawable/color_progress_seek.xml index f62de234..3c6c3e96 100755 --- a/app/src/main/res/drawable/color_progress_seek.xml +++ b/app/src/main/res/drawable/color_progress_seek.xml @@ -40,19 +40,4 @@ - + \ No newline at end of file