fling and clamp for touch events
I have an activity as below:
public class PanScrollActivity extends Activity {
.....
....}
and inside the activity i have 2 functions- fling() & clamp(), which i am
not really understanding. can anyone help me:
/** Utility method to initialize the Scroller and start redrawing */
public void fling(int velocityX, int velocityY)
{
if (getChildCount() > 0)
{ int height = getHeight() - getPaddingBottom() - getPaddingTop();
int width = getWidth() - getPaddingLeft() - getPaddingRight();
int bottom = getChildAt(0).getHeight();
int right = getChildAt(0).getWidth();
mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY,0,
Math.max(0,right - width), 0,Math.max(0, bottom - height));
invalidate();
}
}
/** Utility method to assist in doing bounds checking */
private int clamp(int n, int my, int child)
{
if (my >= child || n < 0)
{
// The child is beyond one of the parent bounds or is smaller than the
parent and can't scroll
return 0;
}
if ((my + n) > child)
{
// Requested scroll is beyond right bound of child
return child - my;
}
fling was used previously inside the below method:
@Override
public boolean onTouchEvent(MotionEvent event)
{
mVelocityTracker.addMovement(event);
switch (event.getAction())
{
case MotionEvent.ACTION DOWN:
return true;
case MotionEvent.ACTION MOVE:
final float x = event.getX();
final float y = event.getY();
float deltaY = mLastTouchY - y;
float deltaX = mLastTouchX - x;
// Check for slop on direct events
if ((Math.abs(deltaY) > mTouchSlop || Math.abs(deltaX) >
mTouchSlop) && !mDragging)
{
mDragging = true;
}
if (mDragging)
{
// Scroll the view
scrollBy((int) deltaX, (int) deltaY);
// Update the last touch event mLastTouchX = x;
mLastTouchY = y;
}
break;
case MotionEvent.ACTION CANCEL:
mDragging = false;
// Stop any flinging in progress
if (!mScroller.isFinished())
{
mScroller.abortAnimation();
}
break;
case MotionEvent.ACTION UP:
mDragging = false;
// Compute the current velocity and start a fling if it is
above the minimum threshold.
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
int velocityX = (int) mVelocityTracker.getXVelocity();
int velocityY = (int) mVelocityTracker.getYVelocity();
if (Math.abs(velocityX) > mMinimumVelocity||
Math.abs(velocityY) > mMinimumVelocity)
{
fling(-velocityX, -velocityY);
}
break;
}
return super.onTouchEvent(event);
}
and clamp inside the below method:
@Override
public void computeScroll()
{
if (mScroller.computeScrollOffset())
{
// This is called at drawing time by ViewGroup. We use this
method to keep the fling animation going through to completion.
int oldX = getScrollX();
int oldY = getScrollY();
int x = mScroller.getCurrX();
int y = mScroller.getCurrY();
if (getChildCount() > 0)
{
View child = getChildAt(0);
x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(),
child.getWidth());
y = clamp(y,getHeight() - getPaddingBottom() - getPaddingTop(),
child.getHeight());
if (x != oldX || y != oldY)
{
scrollTo(x, y);
}
}
No comments:
Post a Comment