Android Open Source - jpstrack.android Button Snazzler






From Project

Back to project page jpstrack.android.

License

The source code is released under:

/* * Copyright (c) 2006-2014 Ian F. Darwin, http://darwinsys.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following co...

If you think the Android project jpstrack.android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jpstrack.android;
/*from   w w  w  .  j av  a 2s . c  o m*/
import java.lang.reflect.Method;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;

public class ButtonSnazzler implements OnTouchListener {
  
  static final String TAG = MainActivity.TAG;

  static final int API_LEVEL = Integer.parseInt(android.os.Build.VERSION.SDK);

  static final DecelerateInterpolator sShrinker = new DecelerateInterpolator();

  static final OvershootInterpolator sExpander = new OvershootInterpolator();

  /**
   * Make a button shrink a bit when pressed, and pop back to
   * bigger-than-normal then normal when released. From a demo by Chet Haase,
   * DevBytes: Anticipation and Overshoot - Part 1 at
   * https://developer.android.com/develop/index.html
   */

  @Override
  public boolean onTouch(View v, MotionEvent me) {
    //Log.d(TAG, "ButtonSnazzler.onTouch(): API Level = " + API_LEVEL);
    if (API_LEVEL < 11) { // Honeycomb
      return false;
    }
    //Log.d(TAG, "ButtonSnazzler.onTouch(): Version OK");
    
    if (!(v instanceof Button)) {
      return false;
    }

    // v.animate().setDuration(200); // faster than default

    if (me.getAction() == MotionEvent.ACTION_DOWN) {
      // v.animate().setInterpolator(sExpander).scaleX(.7f).scaleY(.7f);
      animate(v, sShrinker, 0.7f, 150);
    } else if (me.getAction() == MotionEvent.ACTION_UP) {
      // v.animate().setInterpolator(sExpander).scaleX(1.scaleY(1);
      animate(v, sExpander, 1f, 200);
    }

    return false;
  }

  private void animate(View v, Interpolator inter, float scale, int duration) {
    Log.d(TAG, "ButtonSnazzler.animate()");
    try {
      Method animate = View.class.getMethod("animate", (Class[])null);
      if (animate == null) {
        return;
      }

      Object viewPropertyAnimator = animate.invoke(v, (Object[])null);
      Class<? extends Object> viewPropertyAnimatorClazz = viewPropertyAnimator.getClass();

      Method setInterpolator = viewPropertyAnimatorClazz.getMethod(
          "setInterpolator", new Class[] { Interpolator.class });
      setInterpolator.invoke(viewPropertyAnimator, new Object[] { inter });

      @SuppressWarnings("unchecked")
      Class<? extends Object>[] floatArray = new Class[] { float.class };
      float[] scaleArray = new float[] { scale };
      Method scaleX = viewPropertyAnimatorClazz.getMethod("scaleX", floatArray);
      scaleX.invoke(viewPropertyAnimator, scaleArray);

      Method scaleY = viewPropertyAnimatorClazz.getMethod("scaleY", floatArray);
      scaleY.invoke(viewPropertyAnimator, scaleArray);

      Method setDuration = viewPropertyAnimatorClazz.getMethod(
          "setDuration", new Class[] { int.class });
      setDuration.invoke(viewPropertyAnimator, new int[] { duration });

    } catch (Exception e) {
      Log.e(TAG, "animate: caught exception " + e, e);
    }
  }
}




Java Source Code List

jpstrack.android.ActionBarActivity.java
jpstrack.android.ActionBarHelperBase.java
jpstrack.android.ActionBarHelperHoneycomb.java
jpstrack.android.ActionBarHelperICS.java
jpstrack.android.ActionBarHelper.java
jpstrack.android.AndroidApplication.java
jpstrack.android.ButtonSnazzler.java
jpstrack.android.MainActivity.java
jpstrack.android.OnboardingActivity.java
jpstrack.android.SettingsActivity.java
jpstrack.android.ShowWelcomePage.java
jpstrack.android.SimpleMenuItem.java
jpstrack.android.SimpleMenu.java
jpstrack.android.TextNoteActivity.java
jpstrack.android.ThreadUtils.java
jpstrack.android.TrackerName.java
jpstrack.android.VoiceNoteActivity.java