style: use material play/pause icons
The old play/pause icons that I used to use were really bad and didn't work right on many display densities. Give up and just use the material ones.
This commit is contained in:
@@ -19,14 +19,9 @@
|
||||
package org.oxycblt.auxio.playback
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Animatable2
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Build
|
||||
import android.util.AttributeSet
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.widget.AppCompatImageButton
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.resolveAnimDrawable
|
||||
|
||||
/**
|
||||
* Custom [AppCompatImageButton] that handles the animated play/pause icons.
|
||||
@@ -38,58 +33,15 @@ class PlayPauseButton @JvmOverloads constructor(
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = -1
|
||||
) : AppCompatImageButton(context, attrs, defStyleAttr) {
|
||||
private val iconPauseToPlay = R.drawable.ic_pause_to_play.resolveAnimDrawable(context)
|
||||
private val iconPlayToPause = R.drawable.ic_play_to_pause.resolveAnimDrawable(context)
|
||||
|
||||
init {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
fixSeams()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the play/pause icon to reflect [isPlaying]
|
||||
* @param animate Whether the icon change should be animated or not.
|
||||
*/
|
||||
fun setPlaying(isPlaying: Boolean, animate: Boolean) {
|
||||
if (isPlaying) {
|
||||
if (animate) {
|
||||
setImageDrawable(iconPlayToPause)
|
||||
iconPlayToPause.start()
|
||||
} else {
|
||||
setImageResource(R.drawable.ic_pause_large)
|
||||
}
|
||||
setImageResource(R.drawable.ic_pause)
|
||||
} else {
|
||||
if (animate) {
|
||||
setImageDrawable(iconPauseToPlay)
|
||||
iconPauseToPlay.start()
|
||||
} else {
|
||||
setImageResource(R.drawable.ic_play_large)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hack that fixes an issue where a seam would display in the middle of the play button,
|
||||
* probably as a result of floating point precision errors. Thanks IEEE 754.
|
||||
*/
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
private fun fixSeams() {
|
||||
iconPauseToPlay.registerAnimationCallback(object : Animatable2.AnimationCallback() {
|
||||
override fun onAnimationEnd(drawable: Drawable?) {
|
||||
// ic_play_large is a unified vector, compared to the two paths on the
|
||||
// animated vector. So switch to that when the animation completes to prevent the
|
||||
// seam from displaying.
|
||||
setImageResource(R.drawable.ic_play_large)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow()
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
iconPauseToPlay.clearAnimationCallbacks()
|
||||
setImageResource(R.drawable.ic_play)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user