dimanche 19 avril 2015

About the "onDoubleTap" function in android developer sample "InteractiveChart",

I want to learn how to handle the image and gesture function in android. So I read the sample "InteractiveChart" under "Animating a Scroll Gesture" section in Android developer website. While I read about "onDoubleTap" method in InteractiveLineGraphView.java.



@Override
public boolean onDoubleTap(MotionEvent e) {
mZoomer.forceFinished(true);
if (hitTest(e.getX(), e.getY(), mZoomFocalPoint)) {
mZoomer.startZoom(ZOOM_AMOUNT);
}
ViewCompat.postInvalidateOnAnimation(InteractiveLineGraphView.this);
return true;
}


I checked the code of Zoomer. It mainly calls DecelerateInterpolator method and set some variables. I wonder how can "Zoomer" achieve the double tap zoom function. Does "DecelerateInterpolator" do the work? Or I just missed something?



public class Zoomer {
/**
* The interpolator, used for making zooms animate 'naturally.'
*/
private Interpolator mInterpolator;

/**
* The total animation duration for a zoom.
*/
private int mAnimationDurationMillis;

/**
* Whether or not the current zoom has finished.
*/
private boolean mFinished = true;

/**
* The current zoom value; computed by {@link #computeZoom()}.
*/
private float mCurrentZoom;

/**
* The time the zoom started, computed using {@link android.os.SystemClock#elapsedRealtime()}.
*/
private long mStartRTC;

/**
* The destination zoom factor.
*/
private float mEndZoom;

public Zoomer(Context context) {
mInterpolator = new DecelerateInterpolator();
mAnimationDurationMillis = context.getResources().getInteger(
android.R.integer.config_shortAnimTime);
}

/**
* Forces the zoom finished state to the given value. Unlike {@link #abortAnimation()}, the
* current zoom value isn't set to the ending value.
*
* @see android.widget.Scroller#forceFinished(boolean)
*/
public void forceFinished(boolean finished) {
mFinished = finished;
}

/**
* Aborts the animation, setting the current zoom value to the ending value.
*
* @see android.widget.Scroller#abortAnimation()
*/
public void abortAnimation() {
mFinished = true;
mCurrentZoom = mEndZoom;
}

/**
* Starts a zoom from 1.0 to (1.0 + endZoom). That is, to zoom from 100% to 125%, endZoom should
* by 0.25f.
*
* @see android.widget.Scroller#startScroll(int, int, int, int)
*/
public void startZoom(float endZoom) {
mStartRTC = SystemClock.elapsedRealtime();
mEndZoom = endZoom;

mFinished = false;
mCurrentZoom = 1f;
}

/**
* Computes the current zoom level, returning true if the zoom is still active and false if the
* zoom has finished.
*
* @see android.widget.Scroller#computeScrollOffset()
*/
public boolean computeZoom() {
if (mFinished) {
return false;
}

long tRTC = SystemClock.elapsedRealtime() - mStartRTC;
if (tRTC >= mAnimationDurationMillis) {
mFinished = true;
mCurrentZoom = mEndZoom;
return false;
}

float t = tRTC * 1f / mAnimationDurationMillis;
mCurrentZoom = mEndZoom * mInterpolator.getInterpolation(t);
return true;
}

/**
* Returns the current zoom level.
*
* @see android.widget.Scroller#getCurrX()
*/
public float getCurrZoom() {
return mCurrentZoom;
}


}


Can someone also recommend some great sample about image and gesture handling? From basic to advanced.....Thanks a lot.


Aucun commentaire:

Enregistrer un commentaire