Не прерывать анимации нижних иконок и переключать экраны сразу
This commit is contained in:
1 parent
24031af17a
commit
fd533b81d6
1 file changed
+39
-10
@@ -1,6 +1,7 @@
|
||||
package com.example.helponizer
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.drawable.Animatable2
|
||||
import android.graphics.drawable.AnimatedVectorDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
@@ -11,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.WindowInsetsControllerCompat
|
||||
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
|
||||
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||
|
||||
@@ -56,17 +58,18 @@ open class BaseActivity : AppCompatActivity() {
|
||||
bottomNav.selectedItemId = selectedId
|
||||
|
||||
bottomNav.setOnItemSelectedListener { item ->
|
||||
animateNavIcon(bottomNav, item.itemId)
|
||||
if (item.itemId == currentScreenId) {
|
||||
animateNavIcon(bottomNav, item.itemId)
|
||||
false
|
||||
} else {
|
||||
navigateFromBottomNav(item.itemId)
|
||||
navigateFromBottomNav(bottomNav, item.itemId)
|
||||
}
|
||||
}
|
||||
bottomNav.setOnItemReselectedListener { item ->
|
||||
animateNavIcon(bottomNav, item.itemId)
|
||||
if (item.itemId != currentScreenId) {
|
||||
navigateFromBottomNav(item.itemId)
|
||||
if (item.itemId == currentScreenId) {
|
||||
animateNavIcon(bottomNav, item.itemId)
|
||||
} else {
|
||||
navigateFromBottomNav(bottomNav, item.itemId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,23 +90,49 @@ open class BaseActivity : AppCompatActivity() {
|
||||
ViewCompat.requestApplyInsets(bottomNav)
|
||||
}
|
||||
|
||||
private fun animateNavIcon(bottomNav: BottomNavigationView, itemId: Int) {
|
||||
private fun animateNavIcon(
|
||||
bottomNav: BottomNavigationView,
|
||||
itemId: Int,
|
||||
finishOnEnd: Boolean = false
|
||||
) {
|
||||
bottomNav.post {
|
||||
val itemView = bottomNav.findViewById<View>(itemId)
|
||||
val image = findImageView(itemView) ?: return@post
|
||||
when (val drawable = image.drawable) {
|
||||
is AnimatedVectorDrawable -> {
|
||||
drawable.stop()
|
||||
if (finishOnEnd) drawable.registerAnimationCallback(frameworkFinishOnEnd(bottomNav))
|
||||
drawable.start()
|
||||
}
|
||||
is AnimatedVectorDrawableCompat -> {
|
||||
drawable.stop()
|
||||
if (finishOnEnd) drawable.registerAnimationCallback(compatFinishOnEnd(bottomNav))
|
||||
drawable.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun frameworkFinishOnEnd(bottomNav: BottomNavigationView): Animatable2.AnimationCallback {
|
||||
return object : Animatable2.AnimationCallback() {
|
||||
override fun onAnimationEnd(drawable: android.graphics.drawable.Drawable?) {
|
||||
finishAfterAnimation(bottomNav)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun compatFinishOnEnd(bottomNav: BottomNavigationView): Animatable2Compat.AnimationCallback {
|
||||
return object : Animatable2Compat.AnimationCallback() {
|
||||
override fun onAnimationEnd(drawable: android.graphics.drawable.Drawable?) {
|
||||
finishAfterAnimation(bottomNav)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun finishAfterAnimation(bottomNav: BottomNavigationView) {
|
||||
bottomNav.post {
|
||||
if (!isFinishing) finish()
|
||||
}
|
||||
}
|
||||
|
||||
private fun findImageView(view: View?): ImageView? {
|
||||
if (view == null) return null
|
||||
if (view is ImageView) return view
|
||||
@@ -116,7 +145,7 @@ open class BaseActivity : AppCompatActivity() {
|
||||
return null
|
||||
}
|
||||
|
||||
private fun navigateFromBottomNav(itemId: Int): Boolean {
|
||||
private fun navigateFromBottomNav(bottomNav: BottomNavigationView, itemId: Int): Boolean {
|
||||
val target = when (itemId) {
|
||||
R.id.nav_home -> MainActivity::class.java
|
||||
R.id.nav_schedule -> Raspisanie::class.java
|
||||
@@ -125,8 +154,8 @@ open class BaseActivity : AppCompatActivity() {
|
||||
R.id.nav_party -> funny::class.java
|
||||
else -> return false
|
||||
}
|
||||
animateNavIcon(bottomNav, itemId, finishOnEnd = true)
|
||||
startActivityWithTransition(Intent(this, target))
|
||||
finish()
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user