From 144d315e27ea85f97e0e990f945c722e5f0b2354 Mon Sep 17 00:00:00 2001 From: NoodleMage Date: Mon, 22 Feb 2016 22:36:44 +0100 Subject: [PATCH] rewrote ScrollAwareFABBehavior.java to Kotlin. Can now implement FABAnimationBase to create different FAB animations --- .../tachiyomi/ui/base/fab/FABAnimationBase.kt | 31 +++++++ .../ui/base/fab/FABAnimationUpDown.kt | 54 +++++++++++ .../ui/base/fab/ScrollAwareFABBehavior.java | 91 ------------------- .../res/layout/activity_edit_categories.xml | 2 +- 4 files changed, 86 insertions(+), 92 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationBase.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationUpDown.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/ScrollAwareFABBehavior.java diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationBase.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationBase.kt new file mode 100644 index 000000000..c32fc9909 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationBase.kt @@ -0,0 +1,31 @@ +package eu.kanade.tachiyomi.ui.base.fab + +import android.support.design.widget.CoordinatorLayout +import android.support.design.widget.FloatingActionButton +import android.support.v4.view.ViewCompat +import android.view.View + +open class FABAnimationBase() : FloatingActionButton.Behavior() +{ + open val mIsAnimatingOut = false; + + override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout?, child: FloatingActionButton?, directTargetChild: View?, target: View?, nestedScrollAxes: Int): Boolean { + // Ensure we react to vertical scrolling + return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || + super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes) + } + + override fun onNestedScroll(coordinatorLayout: CoordinatorLayout?, child: FloatingActionButton?, target: View?, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) { + super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed) + if (dyConsumed > 0 && !this.mIsAnimatingOut && child!!.visibility == View.VISIBLE) { + // User scrolled down and the FAB is currently visible -> hide the FAB + animateOut(child) + } else if (dyConsumed < 0 && child!!.visibility != View.VISIBLE) { + // User scrolled up and the FAB is currently not visible -> show the FAB + animateIn(child) + } + } + + open fun animateOut(button : FloatingActionButton) {} + open fun animateIn(button : FloatingActionButton) {} +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationUpDown.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationUpDown.kt new file mode 100644 index 000000000..347f9792c --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/FABAnimationUpDown.kt @@ -0,0 +1,54 @@ +package eu.kanade.tachiyomi.ui.base.fab + +import android.content.Context +import android.support.design.widget.FloatingActionButton +import android.support.v4.view.animation.FastOutSlowInInterpolator +import android.util.AttributeSet +import android.view.View +import android.view.animation.Animation +import android.view.animation.AnimationUtils +import eu.kanade.tachiyomi.R + +class FABAnimationUpDown() : FABAnimationBase() +{ + override var mIsAnimatingOut: Boolean = false + get() = super.mIsAnimatingOut + + private val INTERPOLATOR = FastOutSlowInInterpolator() + + /** + * Needed to prevent NoSuchMethodException + */ + constructor(ctx: Context, attrs: AttributeSet) : this() { } + + override fun animateOut(button: FloatingActionButton) { + super.animateIn(button) + val anim = AnimationUtils.loadAnimation(button.context, R.anim.fab_hide_to_bottom) + anim.interpolator = INTERPOLATOR + anim.duration = 200L + anim.setAnimationListener(object : Animation.AnimationListener { + override fun onAnimationStart(animation: Animation) { + mIsAnimatingOut = true + } + + override fun onAnimationEnd(animation: Animation) { + mIsAnimatingOut = false + button.visibility = View.GONE + } + + override fun onAnimationRepeat(animation: Animation) { + } + }) + button.startAnimation(anim) + + } + + override fun animateIn(button: FloatingActionButton) { + super.animateOut(button) + button.visibility = View.VISIBLE + val anim = AnimationUtils.loadAnimation(button.context, R.anim.fab_show_from_bottom) + anim.duration = 200L + anim.interpolator = INTERPOLATOR + button.startAnimation(anim) + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/ScrollAwareFABBehavior.java b/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/ScrollAwareFABBehavior.java deleted file mode 100644 index cbcd0100c..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/fab/ScrollAwareFABBehavior.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package eu.kanade.tachiyomi.ui.base.fab; - -import android.content.Context; -import android.support.design.widget.CoordinatorLayout; -import android.support.design.widget.FloatingActionButton; -import android.support.v4.view.ViewCompat; -import android.support.v4.view.animation.FastOutSlowInInterpolator; -import android.util.AttributeSet; -import android.view.View; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; -import android.view.animation.Interpolator; - -import eu.kanade.tachiyomi.R; - -public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior { - private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); - private boolean mIsAnimatingOut = false; - - public ScrollAwareFABBehavior(Context context, AttributeSet attrs) { - super(); - } - - @Override - public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, - final View directTargetChild, final View target, final int nestedScrollAxes) { - // Ensure we react to vertical scrolling - return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL - || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); - } - - @Override - public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, - final View target, final int dxConsumed, final int dyConsumed, - final int dxUnconsumed, final int dyUnconsumed) { - super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); - if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) { - // User scrolled down and the FAB is currently visible -> hide the FAB - animateOut(child); - } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { - // User scrolled up and the FAB is currently not visible -> show the FAB - animateIn(child); - } - } - - // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits - private void animateOut(final FloatingActionButton button) { - Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_hide_to_bottom); - anim.setInterpolator(INTERPOLATOR); - anim.setDuration(200L); - anim.setAnimationListener(new Animation.AnimationListener() { - public void onAnimationStart(Animation animation) { - ScrollAwareFABBehavior.this.mIsAnimatingOut = true; - } - - public void onAnimationEnd(Animation animation) { - ScrollAwareFABBehavior.this.mIsAnimatingOut = false; - button.setVisibility(View.GONE); - } - - @Override - public void onAnimationRepeat(final Animation animation) { - } - }); - button.startAnimation(anim); - } - - // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters - private void animateIn(FloatingActionButton button) { - button.setVisibility(View.VISIBLE); - Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_show_from_bottom); - anim.setDuration(200L); - anim.setInterpolator(INTERPOLATOR); - button.startAnimation(anim); - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_edit_categories.xml b/app/src/main/res/layout/activity_edit_categories.xml index 4e8b4af20..d9799cdff 100644 --- a/app/src/main/res/layout/activity_edit_categories.xml +++ b/app/src/main/res/layout/activity_edit_categories.xml @@ -27,6 +27,6 @@ app:backgroundTint="@color/colorPrimary" app:layout_anchor="@id/categories_list" app:layout_anchorGravity="bottom|right|end" - app:layout_behavior="eu.kanade.tachiyomi.ui.base.fab.ScrollAwareFABBehavior"/> + app:layout_behavior="eu.kanade.tachiyomi.ui.base.fab.FABAnimationUpDown"/> \ No newline at end of file