This blog i started for sharing information for android developers.I just love to share my knowledge in my spare time and would appreciate any questions or feedback.

Wednesday, December 17, 2014

Double-tap to zoom and pinch to zoom on ImageView in android

This article will helps you to integrate pinch zoom feature and Single &double Tap functionality without using any third party library in android app.

Let's start with below steps :

Create new project in Eclipse IDE from File ⇒ New ⇒ Android Application Project and fill all the required details. I kept my package name as com.gaddamsraj.blogspot and main activity name asmainTouchActivity.class.

} else { mScaleType = type; if (onDrawReady) { // // If the image is already rendered, scaleType has been called // programmatically // and the TouchImageView should be updated with the new // scaleType. // setZoom(this); } } }

/** * Get the current zoom. This is the zoom relative to the initial scale, not * the original resource. * * @return current zoom multiplier. */ public float getCurrentZoom() { return normalizedScale; }

/** * Set zoom to the specified scale. Image will be centered around the point * (focusX, focusY). These floats range from 0 to 1 and denote the focus * point as a fraction from the left and top of the view. For example, the * top left corner of the image would be (0, 0). And the bottom right corner * would be (1, 1). * * @param scale * @param focusX * @param focusY */ public void setZoom(float scale, float focusX, float focusY) { setZoom(scale, focusX, focusY, mScaleType); }

/** * Set zoom to the specified scale. Image will be centered around the point * (focusX, focusY). These floats range from 0 to 1 and denote the focus * point as a fraction from the left and top of the view. For example, the * top left corner of the image would be (0, 0). And the bottom right corner * would be (1, 1). * * @param scale * @param focusX * @param focusY * @param scaleType */ public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) { // // setZoom can be called before the image is on the screen, but at this // point, // image and view sizes have not yet been calculated in onMeasure. Thus, // we should // delay calling setZoom until the view has been measured. // if (!onDrawReady) { delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType); return; }

/** * Return the point at the center of the zoomed image. The PointF * coordinates range in value between 0 and 1 and the focus point is denoted * as a fraction from the left and top of the view. For example, the top * left corner of the image would be (0, 0). And the bottom right corner * would be (1, 1). * * @return PointF representing the scroll position of the zoomed image. */ public PointF getScrollPosition() { Drawable drawable = getDrawable(); if (drawable == null) { return null; } int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight();

/** * Set the focus point of the zoomed image. The focus points are denoted as * a fraction from the left and top of the view. The focus points can range * in value between 0 and 1. * * @param focusX * @param focusY */ public void setScrollPosition(float focusX, float focusY) { setZoom(normalizedScale, focusX, focusY); }

/** * When transitioning from zooming from focus to zoom from center (or vice * versa) the image can become unaligned within the view. This is apparent * when zooming quickly. When the content size is less than the view size, * the content will often be centered incorrectly within the view. * fixScaleTrans first calls fixTrans() and then makes sure the image is * centered correctly within the view. */ private void fixScaleTrans() { fixTrans(); matrix.getValues(m); if (getImageWidth() < viewWidth) { m[Matrix.MTRANS_X] = (viewWidth - getImageWidth()) / 2; }

/** * If the normalizedScale is equal to 1, then the image is made to fit the * screen. Otherwise, it is made to fit the screen according to the * dimensions of the previous image matrix. This allows the image to * maintain its zoom after rotation. */ private void fitImageToView() { Drawable drawable = getDrawable(); if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) { return; } if (matrix == null || prevMatrix == null) { return; }

} else { // // These values should never be 0 or we will set viewWidth and // viewHeight // to NaN in translateMatrixAfterRotate. To avoid this, call // savePreviousImageValues // to set them equal to the current values. // if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) { savePreviousImageValues(); }

} else { // // Find the area of the image which was previously centered in the // view. Determine its distance // from the left/top side of the view as a fraction of the entire // image's width/height. Use that percentage // to calculate the trans in the new view width/height. // float percentage = (Math.abs(trans) + (0.5f * prevViewSize)) / prevImageSize; m[axis] = -((percentage * imageSize) - (viewSize * 0.5f)); } }

mScaleDetector.onTouchEvent(event); mGestureDetector.onTouchEvent(event); boolean result = mScaleDetector.onTouchEvent(event); // result is always true here, so we need another way to check scalling gesture progress. boolean isScaling = result = mScaleDetector.isInProgress(); if (!isScaling) { // if no scaling is performed check for other gestures (fling, long tab, etc.) mGestureDetector.onTouchEvent(event); } PointF curr = new PointF(event.getX(), event.getY());