From 48e0e427eb2c8dad36b2fcf93ac020fc6bf39555 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 16 Apr 2020 23:14:35 +0530 Subject: ui: animate fab scale rather than translation Suggested-by: Jason A. Donenfeld Signed-off-by: Harsh Shandilya --- .../android/fragment/TunnelListFragment.kt | 48 +++++++++++++--------- ui/src/main/res/anim/scale_down.xml | 12 ++++++ ui/src/main/res/anim/scale_up.xml | 12 ++++++ 3 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 ui/src/main/res/anim/scale_down.xml create mode 100644 ui/src/main/res/anim/scale_up.xml (limited to 'ui/src') diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt index a4980779..7af5e06b 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt @@ -4,7 +4,6 @@ */ package com.wireguard.android.fragment -import android.animation.ObjectAnimator import android.annotation.SuppressLint import android.app.Activity import android.content.Intent @@ -18,6 +17,8 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.view.ViewGroup +import android.view.animation.Animation +import android.view.animation.AnimationUtils import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import com.google.android.material.snackbar.Snackbar @@ -306,7 +307,6 @@ class TunnelListFragment : BaseFragment() { private inner class ActionModeListener : ActionMode.Callback { val checkedItems: MutableCollection = HashSet() private var resources: Resources? = null - private var initialFabTranslation = 0f fun getCheckedItems(): ArrayList { return ArrayList(checkedItems) @@ -316,7 +316,11 @@ class TunnelListFragment : BaseFragment() { return when (item.itemId) { R.id.menu_action_delete -> { val copyCheckedItems = HashSet(checkedItems) - binding?.createFab?.translationY = initialFabTranslation + binding?.createFab?.apply { + visibility = View.VISIBLE + scaleX = 1f + scaleY = 1f + } Application.getTunnelManager().tunnels.thenAccept { tunnels -> val tunnelsToDelete = ArrayList() for (position in copyCheckedItems) tunnelsToDelete.add(tunnels[position]) @@ -346,13 +350,7 @@ class TunnelListFragment : BaseFragment() { if (activity != null) { resources = activity!!.resources } - binding?.createFab?.let { - initialFabTranslation = it.translationY - ObjectAnimator.ofFloat(it, View.TRANSLATION_Y, 400f).apply { - duration = 400 - start() - } - } + animateFab(binding?.createFab, false) mode.menuInflater.inflate(R.menu.tunnel_list_action_mode, menu) binding?.tunnelList?.adapter?.notifyDataSetChanged() return true @@ -361,15 +359,7 @@ class TunnelListFragment : BaseFragment() { override fun onDestroyActionMode(mode: ActionMode) { actionMode = null resources = null - binding?.createFab?.let { - if (it.translationY != initialFabTranslation) { - ObjectAnimator.ofFloat(it, View.TRANSLATION_Y, initialFabTranslation).apply { - duration = 400 - start() - } - } - } - + animateFab(binding?.createFab, true) checkedItems.clear() binding!!.tunnelList.adapter!!.notifyDataSetChanged() } @@ -410,6 +400,26 @@ class TunnelListFragment : BaseFragment() { mode.title = resources!!.getQuantityString(R.plurals.delete_title, count, count) } } + + private fun animateFab(view: View?, show: Boolean) { + view ?: return + val animation = AnimationUtils.loadAnimation( + context, if (show) R.anim.scale_up else R.anim.scale_down + ) + animation.setAnimationListener(object : Animation.AnimationListener { + override fun onAnimationRepeat(animation: Animation?) { + } + + override fun onAnimationEnd(animation: Animation?) { + if (!show) view.visibility = View.GONE + } + + override fun onAnimationStart(animation: Animation?) { + if (show) view.visibility = View.VISIBLE + } + }) + view.startAnimation(animation) + } } companion object { diff --git a/ui/src/main/res/anim/scale_down.xml b/ui/src/main/res/anim/scale_down.xml new file mode 100644 index 00000000..2b5f1b74 --- /dev/null +++ b/ui/src/main/res/anim/scale_down.xml @@ -0,0 +1,12 @@ + + + + diff --git a/ui/src/main/res/anim/scale_up.xml b/ui/src/main/res/anim/scale_up.xml new file mode 100644 index 00000000..65a00c07 --- /dev/null +++ b/ui/src/main/res/anim/scale_up.xml @@ -0,0 +1,12 @@ + + + + -- cgit v1.2.3