Dantotsu/app/src/main/java/ani/dantotsu/others/Xpandable.kt
TwistedUmbrellaX 85ef4b3c12
Add transparency options to subtitle (#281)
* feat: add state llistener to Xpandable

* feat: improve app restart process

* feat: support subtitle transparency
2024-03-23 19:12:22 -05:00

81 lines
No EOL
2.3 KiB
Kotlin

package ani.dantotsu.others
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.core.content.withStyledAttributes
import androidx.core.view.children
import ani.dantotsu.R
class Xpandable @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : LinearLayout(context, attrs) {
var expanded: Boolean = false
private var listener: OnChangeListener? = null
init {
context.withStyledAttributes(attrs, R.styleable.Xpandable) {
expanded = getBoolean(R.styleable.Xpandable_isExpanded, expanded)
}
}
override fun onAttachedToWindow() {
getChildAt(0)!!.setOnClickListener {
if (expanded) hideAll() else showAll()
postDelayed({
expanded = !expanded
}, 300)
}
if (!expanded) children.forEach {
if (it != getChildAt(0)) {
it.visibility = GONE
}
}
super.onAttachedToWindow()
}
private fun hideAll() {
children.forEach {
if (it != getChildAt(0)) {
ObjectAnimator.ofFloat(it, "scaleY", 1f, 0.5f).setDuration(200).start()
ObjectAnimator.ofFloat(it, "translationY", 0f, -32f).setDuration(200).start()
ObjectAnimator.ofFloat(it, "alpha", 1f, 0f).setDuration(200).start()
it.postDelayed({
it.visibility = GONE
}, 300)
}
}
postDelayed({
listener?.onRetract()
}, 300)
}
private fun showAll() {
children.forEach {
if (it != getChildAt(0)) {
it.visibility = VISIBLE
ObjectAnimator.ofFloat(it, "scaleY", 0.5f, 1f).setDuration(200).start()
ObjectAnimator.ofFloat(it, "translationY", -32f, 0f).setDuration(200).start()
ObjectAnimator.ofFloat(it, "alpha", 0f, 1f).setDuration(200).start()
}
}
postDelayed({
listener?.onExpand()
}, 300)
}
@Suppress("unused")
fun setOnChangeListener(listener: OnChangeListener) {
this.listener = listener
}
interface OnChangeListener {
fun onExpand()
fun onRetract()
}
}