Skip to content

Instantly share code, notes, and snippets.

@karllindmark
Last active August 29, 2015 14:26
Show Gist options
  • Save karllindmark/36ecbf02d1da6e309c11 to your computer and use it in GitHub Desktop.
Save karllindmark/36ecbf02d1da6e309c11 to your computer and use it in GitHub Desktop.
An extension of FloatingActionButton with a simple show/hide animation.
/**
* Copyright 2015 Karl Lindmark <karl@ninetwozero.com>
*
* 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.
*/
public class AnimatedFloatingActionButton extends FloatingActionButton {
private static final float MIN_SCALE = 0.0f;
private static final float MAX_SCALE = 1.0f;
private static final float ROTATION_OFFSET = 90;
private static final long START_DELAY = 400L;
private static final long ANIMATION_DURATION = 300L;
public AnimatedFloatingActionButton(Context context) {
super(context);
}
public AnimatedFloatingActionButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AnimatedFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void showWithAnimation() {
// Make sure the relevant values are "reset"
setRotation(-ROTATION_OFFSET);
setScaleX(MIN_SCALE);
setScaleY(MIN_SCALE);
animate().scaleX(MAX_SCALE)
.scaleY(MAX_SCALE)
.rotationBy(ROTATION_OFFSET)
.setStartDelay(START_DELAY)
.setDuration(ANIMATION_DURATION)
.start();
}
public void hideWithAnimation() {
animate().scaleX(MIN_SCALE)
.scaleY(MIN_SCALE)
.rotationBy(-ROTATION_OFFSET)
.setDuration(ANIMATION_DURATION)
.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment