Skip to content

Instantly share code, notes, and snippets.

@kiwiandroiddev
Created August 15, 2016 10:32
Show Gist options
  • Select an option

  • Save kiwiandroiddev/6eb8a73db308011bcd62ba0d58a5264b to your computer and use it in GitHub Desktop.

Select an option

Save kiwiandroiddev/6eb8a73db308011bcd62ba0d58a5264b to your computer and use it in GitHub Desktop.
Custom Android Button that makes use of Facebook's Rebound library to animate changes to its enabled state.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import com.facebook.rebound.BaseSpringSystem;
import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.facebook.rebound.SpringUtil;
/**
* A button whose scale increases with a bouncy effect when enabled, and shrinks when disabled.
*
* Created by matt on 15/08/16.
*/
public class BouncyButton extends Button {
private static final double TENSION = 40.0d;
private static final double FRICTION = 3.0d;
private static final double START_SCALE = 0.8d;
private static final double END_SCALE = 1.0d;
private final BounceSpringListener springListener = new BounceSpringListener();
private Spring scaleSpring;
public BouncyButton(Context context, AttributeSet attrs) {
super(context, attrs);
BaseSpringSystem springSystem = SpringSystem.create();
scaleSpring = springSystem.createSpring();
scaleSpring.setSpringConfig(new SpringConfig(TENSION, FRICTION));
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
scaleSpring.addListener(springListener);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
scaleSpring.removeListener(springListener);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (enabled) {
scaleSpring.setEndValue(1);
} else {
scaleSpring.setEndValue(0);
}
}
private class BounceSpringListener extends SimpleSpringListener {
@Override
public void onSpringUpdate(Spring spring) {
float mappedValue = (float) SpringUtil.mapValueFromRangeToRange(
spring.getCurrentValue(), 0, 1, START_SCALE, END_SCALE);
setScaleX(mappedValue);
setScaleY(mappedValue);
}
}
}
@pcg92
Copy link

pcg92 commented Jun 19, 2017

        if (enabled) {
            scaleSpring.setEndValue(1);
        } else {
            scaleSpring.setEndValue(0);
        }

nullpointer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment