add crossfade view
This commit is contained in:
parent
c991526f35
commit
5014c5aa88
|
|
@ -0,0 +1,44 @@
|
|||
package ru.touchin.roboswag.views
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ViewAnimator
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.core.content.withStyledAttributes
|
||||
import androidx.core.view.children
|
||||
|
||||
class CrossfadeView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null
|
||||
) : ViewAnimator(context, attrs) {
|
||||
|
||||
@IdRes
|
||||
private var defaultChild: Int = 0
|
||||
|
||||
init {
|
||||
setInAnimation(context, R.anim.fade_in_animation)
|
||||
setOutAnimation(context, R.anim.fade_out_animation)
|
||||
|
||||
context.withStyledAttributes(attrs, R.styleable.CrossfadeView, 0) {
|
||||
defaultChild = getResourceId(R.styleable.CrossfadeView_defaultChild, 0)
|
||||
}
|
||||
}
|
||||
|
||||
fun showChild(@IdRes childId: Int) {
|
||||
children.forEachIndexed { index, view ->
|
||||
if (view.id == childId && displayedChild != index) {
|
||||
displayedChild = index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams?) {
|
||||
super.addView(child, index, params)
|
||||
if (child.id == defaultChild) {
|
||||
showChild(defaultChild)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<alpha
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:duration="300"
|
||||
android:fromAlpha="0.0"
|
||||
android:toAlpha="1.0"/>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<alpha
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:duration="300"
|
||||
android:fromAlpha="1.0"
|
||||
android:toAlpha="0.0"/>
|
||||
|
|
@ -56,4 +56,8 @@
|
|||
<attr name="isUnderlineText" format="boolean"/>
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="CrossfadeView">
|
||||
<attr name="defaultChild" format="reference"/>
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Reference in New Issue