Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanielGrech/5771792 to your computer and use it in GitHub Desktop.
Save DanielGrech/5771792 to your computer and use it in GitHub Desktop.
Create's a 'bounce' animation as shown in the Google IO 2013 session 'A Moving Experience' (https://developers.google.com/events/io/sessions/326431311)
/*
<main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|top">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Click Me!"/>
</LinearLayout>
*/
public class MainActivity extends Activity implements View.OnClickListener {
private static final long ANIM_DURATION = 300;
private static final Interpolator DECEL_INTERPOLATOR = new DecelerateInterpolator();
private static final Interpolator ACEL_INTER = new AccelerateInterpolator();
private ViewGroup mContainer;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContainer = (ViewGroup) findViewById(R.id.container);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
final ObjectAnimator moveDownAnimator = getMoveDownAnimator(v);
final ObjectAnimator stretchAnimator = getStretchAndSquashAnimator(v);
final ObjectAnimator upAnimator = getUpAnimator(v);
final AnimatorSet set = new AnimatorSet();
set.playSequentially(moveDownAnimator, stretchAnimator, upAnimator);
set.start();
}
private ObjectAnimator getUpAnimator(View v) {
final PropertyValuesHolder pvhTY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 0);
final PropertyValuesHolder pvhSX = PropertyValuesHolder.ofFloat(View.SCALE_X, 1f);
final PropertyValuesHolder pvhSY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f);
ObjectAnimator upAnim = ObjectAnimator.ofPropertyValuesHolder(v, pvhTY, pvhSX, pvhSY);
upAnim.setInterpolator(DECEL_INTERPOLATOR);
upAnim.setDuration(ANIM_DURATION * 2);
return upAnim;
}
private ObjectAnimator getStretchAndSquashAnimator(View v) {
final PropertyValuesHolder pvhSX = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.5f);
final PropertyValuesHolder pvhSY = PropertyValuesHolder.ofFloat(View.SCALE_Y, .5f);
ObjectAnimator stretchAnim = ObjectAnimator.ofPropertyValuesHolder(v, pvhSX, pvhSY);
stretchAnim.setRepeatCount(1);
stretchAnim.setRepeatMode(ValueAnimator.REVERSE);
stretchAnim.setInterpolator(DECEL_INTERPOLATOR);
stretchAnim.setDuration(ANIM_DURATION);
return stretchAnim;
}
private ObjectAnimator getMoveDownAnimator(View v) {
final PropertyValuesHolder pvhTY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y,
mContainer.getHeight() - v.getHeight());
final PropertyValuesHolder pvhSX = PropertyValuesHolder.ofFloat(View.SCALE_X, .7f);
final PropertyValuesHolder pvhSY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator downAnim = ObjectAnimator.ofPropertyValuesHolder(v, pvhTY, pvhSX, pvhSY);
downAnim.setInterpolator(ACEL_INTER);
downAnim.setDuration(ANIM_DURATION * 2);
return downAnim;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment