Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. package com.androidmess.becultural.common;/*
  2. * Copyright 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. import android.content.Context;
  18. import android.os.Build;
  19. import android.support.design.widget.CoordinatorLayout;
  20. import android.support.design.widget.FloatingActionButton;
  21. import android.support.v4.view.ViewCompat;
  22. import android.support.v4.view.ViewPropertyAnimatorListener;
  23. import android.support.v4.view.animation.FastOutSlowInInterpolator;
  24. import android.util.AttributeSet;
  25. import android.view.View;
  26. import android.view.animation.Animation;
  27. import android.view.animation.AnimationUtils;
  28. import android.view.animation.Interpolator;
  29.  
  30. import com.androidmess.becultural.R;
  31.  
  32. public class ScrollFabBehaviour extends FloatingActionButton.Behavior {
  33. private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
  34. private boolean mIsAnimatingOut = false;
  35.  
  36. public ScrollFabBehaviour(Context context, AttributeSet attrs) {
  37. super();
  38. }
  39.  
  40. @Override
  41. public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
  42. final View directTargetChild, final View target, final int nestedScrollAxes) {
  43. // Ensure we react to vertical scrolling
  44. return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
  45. || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
  46. }
  47.  
  48. @Override
  49. public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
  50. final View target, final int dxConsumed, final int dyConsumed,
  51. final int dxUnconsumed, final int dyUnconsumed) {
  52. super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
  53. if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
  54. // User scrolled down and the FAB is currently visible -> hide the FAB
  55. animateOut(child);
  56. } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
  57. // User scrolled up and the FAB is currently not visible -> show the FAB
  58. animateIn(child);
  59. }
  60. }
  61.  
  62. // Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits
  63. private void animateOut(final FloatingActionButton button) {
  64. if (Build.VERSION.SDK_INT >= 14) {
  65. ViewCompat.animate(button).scaleX(0.0F).scaleY(0.0F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer()
  66. .setListener(new ViewPropertyAnimatorListener() {
  67. public void onAnimationStart(View view) {
  68. ScrollFabBehaviour.this.mIsAnimatingOut = true;
  69. }
  70.  
  71. public void onAnimationCancel(View view) {
  72. ScrollFabBehaviour.this.mIsAnimatingOut = false;
  73. }
  74.  
  75. public void onAnimationEnd(View view) {
  76. ScrollFabBehaviour.this.mIsAnimatingOut = false;
  77. view.setVisibility(View.GONE);
  78. }
  79. }).start();
  80. } else {
  81. Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_out);
  82. anim.setInterpolator(INTERPOLATOR);
  83. anim.setDuration(200L);
  84. anim.setAnimationListener(new Animation.AnimationListener() {
  85. public void onAnimationStart(Animation animation) {
  86. ScrollFabBehaviour.this.mIsAnimatingOut = true;
  87. }
  88.  
  89. public void onAnimationEnd(Animation animation) {
  90. ScrollFabBehaviour.this.mIsAnimatingOut = false;
  91. button.setVisibility(View.GONE);
  92. }
  93.  
  94. @Override
  95. public void onAnimationRepeat(final Animation animation) {
  96. }
  97. });
  98. button.startAnimation(anim);
  99. }
  100. }
  101.  
  102. // Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters
  103. private void animateIn(FloatingActionButton button) {
  104. button.setVisibility(View.VISIBLE);
  105. if (Build.VERSION.SDK_INT >= 14) {
  106. ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
  107. .setInterpolator(INTERPOLATOR).withLayer().setListener(null)
  108. .start();
  109. } else {
  110. Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_in);
  111. anim.setDuration(200L);
  112. anim.setInterpolator(INTERPOLATOR);
  113. button.startAnimation(anim);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement