improve top cover image loading, avoid blinking at fails

This commit is contained in:
tibbi 2021-02-26 20:32:58 +01:00
parent 8a8d441194
commit 9c56ec370b

View file

@ -1,7 +1,7 @@
package com.simplemobiletools.musicplayer.activities
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
@ -21,6 +21,7 @@ import com.bumptech.glide.request.target.Target
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.sendIntent
@ -145,7 +146,7 @@ class TrackActivity : SimpleActivity() {
Glide.with(this)
.load(track?.coverArt)
.apply(options)
.into(findViewById(R.id.next_track_image))
.into(next_track_image)
}
private fun setupButtons() {
@ -166,34 +167,42 @@ class TrackActivity : SimpleActivity() {
}
private fun setupTopArt(coverArt: String) {
val drawable = resources.getDrawable(R.drawable.ic_headset)
var wantedHeight = resources.getDimension(R.dimen.top_art_height).toInt()
wantedHeight = Math.min(wantedHeight, realScreenSize.y / 2)
val placeholder = getResizedDrawable(drawable, wantedHeight)
placeholder.applyColorFilter(config.textColor)
ensureBackgroundThread {
val wantedWidth = realScreenSize.x
val options = RequestOptions().centerCrop()
val wantedWidth = realScreenSize.x
val options = RequestOptions()
.error(placeholder)
.centerCrop()
try {
// change cover image manually only once loaded successfully to avoid blinking at fails and placeholders
Glide.with(this)
.load(coverArt)
.apply(options)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
val drawable = resources.getDrawable(R.drawable.ic_headset)
val placeholder = getResizedDrawable(drawable, wantedHeight)
placeholder.applyColorFilter(config.textColor)
activity_track_image.setImageDrawable(placeholder)
return true
}
Glide.with(this)
.load(coverArt)
.apply(options)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean) = false
override fun onResourceReady(resource: Drawable, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
val coverHeight = resource.intrinsicHeight
if (coverHeight > 0 && activity_track_image.height != coverHeight) {
activity_track_image.layoutParams.height = coverHeight
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
val coverHeight = resource?.intrinsicHeight ?: 0
if (coverHeight > 0 && activity_track_image.height != coverHeight) {
activity_track_image.layoutParams.height = coverHeight
}
return false
}
})
.override(wantedWidth, wantedHeight)
.into(findViewById(R.id.activity_track_image))
activity_track_image.setImageDrawable(resource)
return false
}
})
.into(wantedWidth, wantedHeight)
.get()
} catch (e: Exception) {
}
}
}
private fun toggleShuffle() {