Created
April 10, 2013 22:37
-
-
Save jerrellmardis/5359084 to your computer and use it in GitHub Desktop.
This demonstrates how to implement the onTouch(...) callback method within a Fragment in order to scale/zoom out the Fragment's root view when a user drags their finger up or down. Note: You can use the nineoldandroids lib to get this to work on pre-Honeycomb devices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
@ward459 i know this is old but it might help someone who comes back here, but maybe create a baseZoomFragment
public class BaseZoomFragment extends android.support.v4.app.Fragment implements OnTouchListener
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's been a long time since you wrote this "how to," so the API has probably change since. I'm struggling with this now and can't get onTouch to catch events in a fragment. I noticed in your example that your class doesn't extend Fragment (in my case android.support.v4.app.Fragment). Because I'm extending Fragment, I had to set the listener in my onViewCreated() method: view.setOnTouchListener(this). I don't have any syntax or runtime errors, the touch events simply don't fire. Do you have any words of wisdom? Thanks.