Skip to content

Instantly share code, notes, and snippets.

@jerrellmardis
Created April 10, 2013 22:37

Revisions

  1. jerrellmardis created this gist Apr 10, 2013.
    63 changes: 63 additions & 0 deletions ZoomFragment.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    public class ZoomFragment implements OnTouchListener {

    private static final float MIN_SCALE = 0.95f;

    private float mLastScaleFactor = 0;
    private float mTouchY;

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_layout, container, false);
    mView.setOnTouchListener(this);
    return mView;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    mLastScaleFactor = 0;
    mTouchY = event.getY();
    break;
    case MotionEvent.ACTION_MOVE:
    int[] viewCoords = new int[2];
    v.getLocationOnScreen(viewCoords);

    int touchY = (int) event.getY();
    float yDiff = Math.abs(mTouchY - touchY);
    float dragHeightPercentage = yDiff / (float) getView().getHeight();

    mLastScaleFactor = 1 - ((1 - MIN_SCALE) * dragHeightPercentage);

    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(v, "scaleX", mLastScaleFactor);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(v, "scaleY", mLastScaleFactor);

    scaleDownX.setDuration(0);
    scaleDownY.setDuration(0);

    AnimatorSet scaleDown = new AnimatorSet();
    scaleDown.play(scaleDownX).with(scaleDownY);
    scaleDown.start();
    break;
    case MotionEvent.ACTION_UP:
    ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(v, "scaleX", 1f);
    ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(v, "scaleY", 1f);

    scaleUpX.setDuration(250);
    scaleUpY.setDuration(250);

    AnimatorSet scaleUp = new AnimatorSet();
    scaleUp.play(scaleUpX).with(scaleUpY);
    scaleUp.start();

    mLastScaleFactor = 0;
    mTouchY = 0;
    break;
    default:
    break;
    }

    return true;
    }

    }