1/* 2 * Copyright (C) 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 17package android.support.design.widget; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.support.annotation.Nullable; 22import android.support.v4.view.ViewCompat; 23import android.view.View; 24 25class FloatingActionButtonIcs extends FloatingActionButtonEclairMr1 { 26 27 private boolean mIsHiding; 28 29 FloatingActionButtonIcs(VisibilityAwareImageButton view, 30 ShadowViewDelegate shadowViewDelegate) { 31 super(view, shadowViewDelegate); 32 } 33 34 @Override 35 boolean requirePreDrawListener() { 36 return true; 37 } 38 39 @Override 40 void onPreDraw() { 41 updateFromViewRotation(mView.getRotation()); 42 } 43 44 @Override 45 void hide(@Nullable final InternalVisibilityChangedListener listener, final boolean fromUser) { 46 if (mIsHiding || mView.getVisibility() != View.VISIBLE) { 47 // A hide animation is in progress, or we're already hidden. Skip the call 48 if (listener != null) { 49 listener.onHidden(); 50 } 51 return; 52 } 53 54 if (!ViewCompat.isLaidOut(mView) || mView.isInEditMode()) { 55 // If the view isn't laid out, or we're in the editor, don't run the animation 56 mView.internalSetVisibility(View.GONE, fromUser); 57 if (listener != null) { 58 listener.onHidden(); 59 } 60 } else { 61 mView.animate().cancel(); 62 mView.animate() 63 .scaleX(0f) 64 .scaleY(0f) 65 .alpha(0f) 66 .setDuration(SHOW_HIDE_ANIM_DURATION) 67 .setInterpolator(AnimationUtils.FAST_OUT_LINEAR_IN_INTERPOLATOR) 68 .setListener(new AnimatorListenerAdapter() { 69 private boolean mCancelled; 70 71 @Override 72 public void onAnimationStart(Animator animation) { 73 mIsHiding = true; 74 mCancelled = false; 75 mView.internalSetVisibility(View.VISIBLE, fromUser); 76 } 77 78 @Override 79 public void onAnimationCancel(Animator animation) { 80 mIsHiding = false; 81 mCancelled = true; 82 } 83 84 @Override 85 public void onAnimationEnd(Animator animation) { 86 mIsHiding = false; 87 if (!mCancelled) { 88 mView.internalSetVisibility(View.GONE, fromUser); 89 if (listener != null) { 90 listener.onHidden(); 91 } 92 } 93 } 94 }); 95 } 96 } 97 98 @Override 99 void show(@Nullable final InternalVisibilityChangedListener listener, final boolean fromUser) { 100 if (mIsHiding || mView.getVisibility() != View.VISIBLE) { 101 if (ViewCompat.isLaidOut(mView) && !mView.isInEditMode()) { 102 mView.animate().cancel(); 103 if (mView.getVisibility() != View.VISIBLE) { 104 // If the view isn't visible currently, we'll animate it from a single pixel 105 mView.setAlpha(0f); 106 mView.setScaleY(0f); 107 mView.setScaleX(0f); 108 } 109 mView.animate() 110 .scaleX(1f) 111 .scaleY(1f) 112 .alpha(1f) 113 .setDuration(SHOW_HIDE_ANIM_DURATION) 114 .setInterpolator(AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR) 115 .setListener(new AnimatorListenerAdapter() { 116 @Override 117 public void onAnimationStart(Animator animation) { 118 mView.internalSetVisibility(View.VISIBLE, fromUser); 119 } 120 121 @Override 122 public void onAnimationEnd(Animator animation) { 123 if (listener != null) { 124 listener.onShown(); 125 } 126 } 127 }); 128 } else { 129 mView.internalSetVisibility(View.VISIBLE, fromUser); 130 mView.setAlpha(1f); 131 mView.setScaleY(1f); 132 mView.setScaleX(1f); 133 if (listener != null) { 134 listener.onShown(); 135 } 136 } 137 } 138 } 139 140 private void updateFromViewRotation(float rotation) { 141 // Offset any View rotation 142 if (mShadowDrawable != null) { 143 mShadowDrawable.setRotation(-rotation); 144 } 145 if (mBorderDrawable != null) { 146 mBorderDrawable.setRotation(-rotation); 147 } 148 } 149} 150