如何重置缩放图像为原始图像 [英] How to reset the zoomed image into original image

查看:311
本文介绍了如何重置缩放图像为原始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 TouchImageView 可在类的https:/ /github.com/MikeOrtiz/TouchImageView

当我从缩放图像的下一步行动,并回到previous图像仍然具有缩放。

我要缩放图像复位到正常位置时,轻扫previous图片

和通过引用这个问题<一个得到的答案href=\"http://stackoverflow.com/questions/17022637/how-to-reset-an-image-to-the-original-position\">How将图像恢复到原来的位置但我不知道确切位置必须把这种方法在这个库所以请你帮我引导?

先谢谢了。

 公共类TouchImageView扩展了ImageView的{私有静态最后弦乐DEBUG =DEBUG//
// SuperMin和无敌乘数。确定图像多少可以
//缩放低于或高于变焦边界,动画回之前
//最小/最大变焦的边界。
//
私有静态最终浮动SUPER_MIN_MULTIPLIER = .75f;
私有静态最终浮动SUPER_MAX_MULTIPLIER = 1.25f;//
//图像的缩放范围从minScale到maxScale,其中minScale == 1
//当图像被拉伸以适应视图。
//
私人浮动normalizedScale;//
//矩阵应用于图像。 MSCALE_X和MSCALE_Y应该永远是平等的。
// MTRANS_X和MTRANS_Y是所使用的其他值。 prevMatrix是矩阵
//保存之前的屏幕旋转。
//
私人矩阵的矩阵,prevMatrix;私人布尔onTouch = FALSE;私有静态枚举国家{NONE,拖拽,缩放,一扔,ANIMATE_ZOOM};
私有状态状态;私人浮动minScale;
私人浮动maxScale;
私人浮动superMinScale;
私人浮动superMaxScale;
私人浮动[]米;私人上下文的背景下;
私人一扔一扔;私人ScaleType mScaleType;私人布尔onDrawReady;
私人ZoomVariables delayedZoomVariables;//
//观点和previous视图大小的尺寸(即旋转之前)
//
私人诠释viewWidth,viewHeight,prevViewWidth,prevViewHeight;//
//当它被拉伸以适应视图像大小。之前和旋转之后。
//
私人浮动matchViewWidth,matchViewHeight,prevMatchViewWidth,prevMatchViewHeight;私人ScaleGestureDetector mScaleDetector;
私人GestureDetector mGestureDetector;公共TouchImageView(上下文的背景下){
    超级(上下文);
    sharedConstructing(上下文);
}公共TouchImageView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    sharedConstructing(上下文);
}公共TouchImageView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    sharedConstructing(上下文);
}OnScaleChangeListener _scaleChangeListener; // codeD由湿婆//接口对象用于检测ImageView的单,双水龙头
公共接口OnScaleChangeListener {
    公共无效onSingleTabfromTouchImage(布尔isSingleTabClick);
    公共无效onDoubleTapfromTouchImage(布尔isDoubleTapClick);
}公共同步无效setOnScaleChangeListener(OnScaleChangeListener监听){
    _scaleChangeListener =侦听器;
}私人无效sharedConstructing(上下文的背景下){
    super.setClickable(真);
    this.context =背景;
    mScaleDetector =新ScaleGestureDetector(上下文,新ScaleListener());
    mGestureDetector =新GestureDetector(上下文,新GestureListener());
    矩阵=新的Matrix();
    prevMatrix =新的Matrix();
    M =新的浮动[9];
    normalizedScale = 1;
    如果(mScaleType == NULL){
        mScaleType = ScaleType.FIT_CENTER;
    }
    minScale = 1;
    maxScale = 3;
    superMinScale = SUPER_MIN_MULTIPLIER * minScale;
    superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
    setImageMatrix(矩阵);
    setScaleType(ScaleType.MATRIX);
    的setState(State.NONE);
    setOnTouchListener(新TouchImageViewListener());
    onDrawReady = FALSE;
}@覆盖
公共无效setImageResource(INT渣油){
    super.setImageResource(渣油);
    节省previousImageValues​​();
    fitImageToView();
}@覆盖
公共无效setImageBitmap(位图BM){
    super.setImageBitmap(BM);
    节省previousImageValues​​();
    fitImageToView();
}@覆盖
公共无效setImageDrawable(绘制对象绘制){
    super.setImageDrawable(绘制);
    节省previousImageValues​​();
    fitImageToView();
}@覆盖
公共无效setImageURI(URI URI){
    super.setImageURI(URI);
    节省previousImageValues​​();
    fitImageToView();
}@覆盖
公共无效setScaleType(ScaleType型){
    如果(类型== || ScaleType.FIT_START型== ScaleType.FIT_END){
        抛出新UnsupportedOperationException异常(TouchImageView不支持FIT_START或FIT_END);
    }
    如果(类型== ScaleType.MATRIX){
        super.setScaleType(ScaleType.MATRIX);    }其他{
        mScaleType =类型;
    }
}@覆盖
公共ScaleType getScaleType(){
    返回mScaleType;
}/ **
 *如果图像是最初的,未缩放状态返回false。假的,否则。
 *如果图像被放大返回:真
 * /
公共布尔isZoomed(){    如果(normalizedScale→1)
        返回true;
    其他
        返回false;
    //返回normalizedScale = 1!;
}/ **
 *返回缩放图像的位图,因为它出现在视图中。这实质上
 *充当视图屏幕截图,并最终位图的尺寸被限制为
 *视图本身的分辨率。
 * @返回缩放图像的位图
 * /
公共位图getZoomedImage(){
    位图位图= Bitmap.createBitmap(viewWidth,viewHeight,Bitmap.Config.ARGB_8888);
    帆布帆布=新的Canvas(位图);
    布局(0,0,viewWidth,viewHeight);
    画(油画);
    返回位图;
}/ **
 *返回缩放图像的位图。这个方法是从getZoomedImage不同(),因为
 *它直接切割图像从可绘​​制源,因此,不受分辨率的限制
 *视图。不支持具有FIT_XY。
 * @返回缩放图像的位图
 * /
公共位图getZoomedImageFromSource(){
    如果(mScaleType == ScaleType.FIT_XY){
        抛出新UnsupportedOperationException异常(getZoomedImageFromSource()与FIT_XY不支持);
    }
    位图的位图=((BitmapDrawable)getDrawable())getBitmap();
    矩形R = getZoomedRect();
    如果(r.width()&下; = 0 || r.height()&下; = 0){
        返回null;
    }
    返回Bitmap.createBitmap(位图,r.left,r.top,r.width(),r.height());
}/ **
 *返回一个矩形重新$ P $的绘制源的坐标系中psenting的变焦影像。
 * @返回RECT重新presenting放大图像
 * /
公共矩形getZoomedRect(){
    如果(mScaleType == ScaleType.FIT_XY){
        抛出新UnsupportedOperationException异常(getZoomedRect()与FIT_XY不支持);
    }
    的PointF左上= getDrawablePointFromTouchPoint(0,0);
    的PointF bottomRight = getDrawablePointFromTouchPoint(viewWidth,viewHeight);
    返回新的矩形((INT)topLeft.x,(INT)topLeft.y,(INT)bottomRight.x,(INT)bottomRight.y);
}/ **
 *保存当前矩阵,视图尺寸
 *在prevMatrix和prevView变量。
 * /
私人无效节省previousImageValues​​(){
    如果(矩阵!= NULL){
        matrix.getValues​​(米);
        prevMatrix.setValues​​(米);
        prevMatchViewHeight = matchViewHeight;
        prevMatchViewWidth = matchViewWidth;
        prevViewHeight = viewHeight;
        prevViewWidth = viewWidth;
    }
}@覆盖
公共Parcelable的onSaveInstanceState(){
    束束=新包();
    bundle.putParcelable(instanceState,super.onSaveInstanceState());
    bundle.putFloat(saveScale,normalizedScale);
    bundle.putFloat(matchViewHeight,matchViewHeight);
    bundle.putFloat(matchViewWidth,matchViewWidth);
    bundle.putInt(viewWidth,viewWidth);
    bundle.putInt(viewHeight,viewHeight);
    matrix.getValues​​(米);
    bundle.putFloatArray(基质,米);
    返回束;
}@覆盖
公共无效onRestoreInstanceState(Parcelable状态){
    如果(州的instanceof捆绑){
        束束=(包)状态;
        normalizedScale = bundle.getFloat(saveScale);
        M = bundle.getFloatArray(基质);
        prevMatrix.setValues​​(米);
        prevMatchViewHeight = bundle.getFloat(matchViewHeight);
        prevMatchViewWidth = bundle.getFloat(matchViewWidth);
        prevViewHeight = bundle.getInt(viewHeight);
        prevViewWidth = bundle.getInt(viewWidth);
        super.onRestoreInstanceState(bundle.getParcelable(instanceState));
        返回;
    }    super.onRestoreInstanceState(州);
}@覆盖
保护无效的onDraw(帆布油画){
    onDrawReady = TRUE;
    如果(delayedZoomVariables!= NULL){
        setZoom(delayedZoomVariables.scale,delayedZoomVariables.focusX,delayedZoomVariables.focusY,delayedZoomVariables.scaleType);
        delayedZoomVariables = NULL;
    }
    super.onDraw(画布);
}@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
    super.onConfigurationChanged(NEWCONFIG);
    //保存previousImageValues​​();
    resetZoom();
}/ **
 *获取最大变焦倍数。
 返回:最大变焦倍数。
 * /
公众持股量getMaxZoom(){
    返回maxScale;
}/ **
 *设置最大变焦倍数。默认值:3。
 * @参数最大最大变焦倍数。
 * /
公共无效setMaxZoom(浮点最大值){
    maxScale = MAX;
    superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
}/ **
 *获取最小变焦倍数。
 * @返回分钟变焦倍数。
 * /
公众持股量getMinZoom(){
    返回minScale;
}/ **
 *获取当前的变焦。这是相对于初始的放大
 *尺度,而不是原来的资源。
 返回:当前的变焦倍数。
 * /
公众持股量getCurrentZoom(){
    返回normalizedScale;
}/ **
 *设置最小变焦倍数。默认值:1。
 * @参数分分钟的变焦倍数。
 * /
公共无效setMinZoom(浮动分钟){
    minScale =分钟;
    superMinScale = SUPER_MIN_MULTIPLIER * minScale;
}/ **
 *重置缩放和平移到初始状态。
 * /
公共无效resetZoom(){
    normalizedScale = 1;
    fitImageToView();
}/ **
 *设置缩放到指定的规模。图像将被默认为中心。
 * @参数规模
 * /
公共无效setZoom(浮动比例){
    setZoom(规模,0.5F,0.5F);
}/ **
 *设置缩放到指定的规模。图片将围绕该点为中心
 *(focusX,focusY)。这些浮标范围从0到1并且表示焦点
 *从视图的左侧和顶部的馏分。例如,左上
 *图像的角将是(0,0)。和右下角将是(1,1)。
 * @参数规模
 * @参数focusX
 * @参数focusY
 * /
公共无效setZoom(浮点规模,浮focusX,浮focusY){
    setZoom(规模,focusX,focusY,mScaleType);
}/ **
 *设置缩放到指定的规模。图片将围绕该点为中心
 *(focusX,focusY)。这些浮标范围从0到1并且表示焦点
 *从视图的左侧和顶部的馏分。例如,左上
 *图像的角将是(0,0)。和右下角将是(1,1)。
 * @参数规模
 * @参数focusX
 * @参数focusY
 * @参数scaleType
 * /
公共无效setZoom(浮点规模,浮focusX,浮focusY,ScaleType scaleType){
    //
    // setZoom可以被称为前的图像是在屏幕上,但在这一点上,
    //图像和视图尺寸尚未在onMeasure计算。因此,我们应该
    //延迟直到视图已测呼吁setZoom。
    //
    如果(!onDrawReady){
        delayedZoomVariables =新ZoomVariables(规模,focusX,focusY,scaleType);
        返回;
    }    setScaleType(scaleType);
    resetZoom();
    scaleImage(规模,viewWidth / 2,viewHeight / 2,假);
    matrix.getValues​​(米);
    米[Matrix.MTRANS_X] = - ((focusX * getImageWidth()) - (viewWidth * 0.5F));
    米[Matrix.MTRANS_Y] = - ((focusY * getImageHeight()) - (viewHeight * 0.5F));
    matrix.setValues​​(米);
    fixTrans();
    setImageMatrix(矩阵);
}/ **
 *设置变焦参数等于另一个TouchImageView。包括规模,位置,
 *和ScaleType。
 * @参数TouchImageView
 * /
公共无效setZoom(TouchImageView IMG){
    的PointF中心= img.getCenterOfZoomedImage();
    setZoom(img.getCurrentZoom(),center.x,center.y,img.getScaleType());
}/ **
 *有关的观点(即触​​摸事件)给定点,返回
 *相对于原可绘的坐标系点。
 * @参数点¯x
 * @参数ÿ
 *相对于原来绘制的坐标系@返回的PointF。
 * /
公众的PointF getDrawablePointFromTouchPoint(浮法X,浮法Y){
    返回transformCoordTouchToBitmap(X,Y,真);
}/ **
 *有关的观点(即触​​摸事件)给定点,返回
 *相对于原可绘的坐标系点。
 * @参数p
 *相对于原来绘制的坐标系@返回的PointF。
 * /
公众的PointF getDrawablePointFromTouchPoint(的PointF P){
    返回transformCoordTouchToBitmap(p.x,p.y,真正的);
}/ **
 *返回点缩放图像的中心。该坐标的PointF范围
 *在0和1和焦点之间的值被表示为从左侧的一小部分
 *和视图的顶部。例如,该图像的左上角是(0,0)。
 *与右下角将是(1,1)。
 影像的放大* @返回的PointF重新presenting中心
 * /
公众的PointF getCenterOfZoomedImage(){
    可绘制可绘制= getDrawable();
    如果(绘制== NULL){
        返回null;
    }
    INT drawableWidth = drawable.getIntrinsicWidth();
    INT drawableHeight = drawable.getIntrinsicHeight();    的PointF点= getDrawablePointFromTouchPoint(viewWidth / 2,viewHeight / 2);
    point.x / = drawableWidth;
    point.y / = drawableHeight;
    返回点;
}/ **
 *执行边界检查和修复图像矩阵,如果它
 *为出界。
 * /
私人无效fixTrans(){
    matrix.getValues​​(米);
    浮transX = M [Matrix.MTRANS_X]
    浮transY = M [Matrix.MTRANS_Y]    浮fixTransX = getFixTrans(transX,viewWidth,getImageWidth());
    浮fixTransY = getFixTrans(transY,viewHeight,getImageHeight());    如果(fixTransX!= 0 || fixTransY!= 0){
        matrix.postTranslate(fixTransX,fixTransY);
    }
}/ **
 *当从焦点变焦从中心到放大转换(反之亦然)
 *图像可以成为在视图中未对齐。放大时,这是显而易见
 *快。当该含量大小小于视图尺寸,内容常常将
 *可以在视图中不正确居中。 fixScaleTrans首先调用fixTrans()和
 *然后确保图像在视图中居中。
 * /
私人无效fixScaleTrans(){
    fixTrans();
    matrix.getValues​​(米);
    如果(getImageWidth()&下; viewWidth){
        米[Matrix.MTRANS_X] =(viewWidth - getImageWidth())/ 2;
    }    如果(getImageHeight()&下; viewHeight){
        米[Matrix.MTRANS_Y] =(viewHeight - getImageHeight())/ 2;
    }
    matrix.setValues​​(米);
}私人浮动getFixTrans(反式浮动,浮动viewSize,浮contentSize){
    浮minTrans,MAXTRANS;    如果(contentSize&LT; = viewSize){
        minTrans = 0;
        MAXTRANS = viewSize - contentSize;    }其他{
        minTrans = viewSize - contentSize;
        MAXTRANS = 0;
    }    如果(反式LT; minTrans)
        返回 - 反+ minTrans;
    如果(反式GT; MAXTRANS)
        返回 - 反+ MAXTRANS;
    返回0;
}私人浮动getFixDragTrans(浮动三角洲,浮viewSize,浮contentSize){
    如果(contentSize&LT; = viewSize){
        返回0;
    }
    返回三角洲;
}私人浮动getImageWidth(){
    返回matchViewWidth * normalizedScale;
}私人浮动getImageHeight(){
    返回matchViewHeight * normalizedScale;
}@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    可绘制可绘制= getDrawable();
    如果(绘== NULL || drawable.getIntrinsicWidth()== 0 || drawable.getIntrinsicHeight()== 0){
        setMeasuredDimension(0,0);
        返回;
    }  / *如果(onTouch){
        // resetZoom();
        onTouch = FALSE;
    }
    其他
        resetZoom(); * /    INT drawableWidth = drawable.getIntrinsicWidth();
    INT drawableHeight = drawable.getIntrinsicHeight();
    INT widthSize = MeasureSpec.getSize(widthMeasureSpec);
    INT widthMode = MeasureSpec.getMode(widthMeasureSpec);
    INT heightSize = MeasureSpec.getSize(heightMeasureSpec);
    INT heightMode = MeasureSpec.getMode(heightMeasureSpec);
    viewWidth = setViewSize(widthMode,widthSize,drawableWidth);
    viewHeight = setViewSize(heightMode,heightSize,drawableHeight);    //
    //设置视图尺寸
    //
    setMeasuredDimension(viewWidth,viewHeight);    //
    //视野范围内的飞度内容
    //
    fitImageToView();
}/ **
 *如果normalizedScale等于1,则该图像是由以适应屏幕。除此以外,
 *它是由根据本previous图像矩阵的维数,以适应屏幕。这个
 *可以保持旋转后的缩放图像。
 * /
私人无效fitImageToView(){
    可绘制可绘制= getDrawable();
    如果(绘== NULL || drawable.getIntrinsicWidth()== 0 || drawable.getIntrinsicHeight()== 0){
        返回;
    }
    如果(矩阵== NULL || prevMatrix == NULL){
        返回;
    }    INT drawableWidth = drawable.getIntrinsicWidth();
    INT drawableHeight = drawable.getIntrinsicHeight();    //
    //为视图缩放图像
    //
    漂浮的scaleX =(浮点)viewWidth / drawableWidth;
    漂浮的scaleY =(浮点)viewHeight / drawableHeight;    开关(mScaleType){
    案例中心:
        将scaleX =的scaleY = 1;
        打破;    案例CENTER_CROP:
        将scaleX = =的scaleY Math.max(将scaleX,的scaleY);
        打破;    案例CENTER_INSIDE:
        将scaleX = =的scaleY Math.min(1,Math.min(将scaleX,的scaleY));    案例FIT_CENTER:
        将scaleX = =的scaleY Math.min(将scaleX,的scaleY);
        打破;    案例FIT_XY:
        打破;    默认:
        //
        // FIT_START和FIT_END不支持
        //
        抛出新UnsupportedOperationException异常(TouchImageView不支持FIT_START或FIT_END);    }    //
    //中心图像
    //
    浮redundantXSpace = viewWidth - (将scaleX * drawableWidth);
    浮redundantYSpace = viewHeight - (*的scaleY drawableHeight);
    matchViewWidth = viewWidth - redundantXSpace;
    matchViewHeight = viewHeight - redundantYSpace;
    如果(!isZoomed()){
        //
        //伸展和中心图像以适合视图
        //
        matrix.setScale(将scaleX,的scaleY);
        matrix.postTranslate(redundantXSpace / 2,redundantYSpace / 2);
        normalizedScale = 1;    }其他{
        //
        //这些值不应该是0,否则我们将设置viewWidth和viewHeight
        //为NaN在translateMatrixAfterRotate。为了避免这种情况,请拨打节省previousImageValues
        //设置它们等于当前值。
        //        如果(onTouch){
            如果(matchViewWidth&GT; drawableWidth){
                prevMatchViewHeight = 0;
                prevMatchViewWidth = 0;
                节省previousImageValues​​();
            }
            onTouch = FALSE;
        }
        如果(prevMatchViewWidth == 0 || prevMatchViewHeight == 0){
            //保存previousImageValues​​();
            normalizedScale = 1;
        }        prevMatrix.getValues​​(米);        //
        //旋转后重缩放矩阵
        //
        米[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale;
        米[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale;        //
        // TransX和TransY从previous矩阵
        //
        浮transX = M [Matrix.MTRANS_X]
        浮transY = M [Matrix.MTRANS_Y]        //
        //宽
        //
        浮prevActualWidth = prevMatchViewWidth * normalizedScale;
        浮ActualWidth的= getImageWidth();
        translateMatrixAfterRotate(Matr​​ix.MTRANS_X,transX,prevActualWidth,ActualWidth的,prevViewWidth,viewWidth,drawableWidth);        //
        //高度
        //
        浮prevActualHeight = prevMatchViewHeight * normalizedScale;
        浮动的ActualHeight = getImageHeight();
        translateMatrixAfterRotate(Matr​​ix.MTRANS_Y,transY,prevActualHeight,的ActualHeight,prevViewHeight,viewHeight,drawableHeight);        //
        //设置矩阵调整后的规模和翻译的值。
        //
        matrix.setValues​​(米);
    }
    fixTrans();
    setImageMatrix(矩阵);
}/ **
 *基于布局PARAMS Set视图尺寸
 *
 * @参数模式
 * @参数尺寸
 * @参数drawableWidth
 * @返回
 * /
私人诠释setViewSize(INT模式,诠释大小,诠释drawableWidth){
    INT viewSize;
    开关(模式){
    案例MeasureSpec.EXACTLY:
        viewSize =大小;
        打破;    案例MeasureSpec.AT_MOST:
        viewSize = Math.min(drawableWidth,大小);
        打破;    案例MeasureSpec.UNSPECIFIED:
        viewSize = drawableWidth;
        打破;    默认:
        viewSize =大小;
        打破;
    }
    返回viewSize;
}/ **
 *手势侦听器检测到一个单一的点击或长按并传递上
 *到视图的侦听器。
 * @author奥尔蒂斯
 *
 * /
私有类GestureListener扩展GestureDetector.SimpleOnGestureListener {    @覆盖
    公共布尔onSingleTapConfirmed(MotionEvent五)
    {
        onTouch = TRUE;
        _scaleChangeListener.onSingleTabfromTouchImage(真); //设置单一的水龙头由湿婆// codeD布尔。
        节省previousImageValues​​();
        返回false;
    }    @覆盖
    公共无效onLong preSS(MotionEvent五)
    {
        performLongClick();
    }    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮动velocityY)
    {
        如果(一扔!= NULL){
            //
            //如果一个previous抛仍然是活动的,它应该被取消使得两个甩
            //不simultaenously运行。
            //
            fling.cancelFling();
        }
        =一扔一扔新((INT)velocityX,(INT)velocityY);
        compatPostOnAnimation(一扔);
        返回super.onFling(E1,E2,velocityX,velocityY);
    }    @覆盖
    公共布尔onDoubleTap(MotionEvent五){
        布尔消耗= FALSE;        如果(状态== State.NONE){
            浮动targetZoom =(normalizedScale == minScale)? maxScale:minScale;
            DoubleTapZoom doubleTap =新DoubleTapZoom(targetZoom,e.getX(),e.​​getY(),FALSE);
            compatPostOnAnimation(doubleTap);
            消费= TRUE;
        }
        返回消耗;
    }
}/ **
 *负责所有触摸事件。处理拖动的繁重和发送
 *触摸事件检测器规模和手势检测。
 * @author奥尔蒂斯
 *
 * /
私有类TouchImageViewListener实现OnTouchListener {    //
    //记住最后拖动点位置
    //
    私人的PointF最后=新的PointF();    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        mScaleDetector.onTouchEvent(事件);
        mGestureDetector.onTouchEvent(事件);
        的PointF CURR =新的PointF(event.getX(),event.getY());        如果(状态== || State.NONE状态== || State.DRAG状态== State.FLING){
            开关(event.getAction()){
                案例MotionEvent.ACTION_DOWN:
                    last.set(CURR);
                    如果(一扔!= NULL)
                        fling.cancelFling();
                    的setState(State.DRAG);
                    打破;                案例MotionEvent.ACTION_MOVE:
                    如果(状态== State.DRAG){
                        浮DELTAX = curr.x - last.x;
                        浮DELTAY = curr.y - last.y;
                        浮fixTransX = getFixDragTrans(DELTAX,viewWidth,getImageWidth());
                        浮fixTransY = getFixDragTrans(DELTAY,viewHeight,getImageHeight());
                        matrix.postTranslate(fixTransX,fixTransY);
                        fixTrans();
                        last.set(curr.x,curr.y);
                    }
                    打破;                案例MotionEvent.ACTION_UP:
                案例MotionEvent.ACTION_POINTER_UP:
                    的setState(State.NONE);
                    打破;
            }
        }        setImageMatrix(矩阵);
        //
        //表示事件已处理
        //
        返回true;
    }
}/ **
 * ScaleListener检测用户双指缩放和尺度的形象。
 * @author奥尔蒂斯
 *
 * /
私有类ScaleListener扩展ScaleGestureDetector.SimpleOnScaleGestureListener {
    @覆盖
    公共布尔onScaleBegin(ScaleGestureDetector检测器){
        的setState(State.ZOOM);
        返回true;
    }    @覆盖
    公共布尔onScale(ScaleGestureDetector检测器){
        scaleImage(detector.getScaleFactor(),detector.getFocusX(),detector.getFocusY(),TRUE);
        返回true;
    }    @覆盖
    公共无效onScaleEnd(ScaleGestureDetector检测器){
        super.onScaleEnd(探测器);
        的setState(State.NONE);
        布尔animateToZoomBoundary = FALSE;
        浮targetZoom = normalizedScale;
        如果(normalizedScale&GT; maxScale){
            targetZoom = maxScale;
            animateToZoomBoundary = TRUE;        }否则如果(normalizedScale&LT; minScale){
            targetZoom = minScale;
            animateToZoomBoundary = TRUE;
        }        如果(animateToZoomBoundary){
            DoubleTapZoom doubleTap =新DoubleTapZoom(targetZoom,viewWidth / 2,viewHeight / 2,真正的);
            compatPostOnAnimation(doubleTap);
        }
    }
}私人无效scaleImage(浮动deltaScale,浮focusX,浮focusY,布尔stretchImageToSuper){    浮lowerScale,upperScale;
    如果(stretchImageToSuper){
        lowerScale = superMinScale;
        upperScale = superMaxScale;    }其他{
        lowerScale = minScale;
        upperScale = maxScale;
    }    浮origScale = normalizedScale;
    normalizedScale * = deltaScale;
    如果(normalizedScale&GT; upperScale){
        normalizedScale = upperScale;
        deltaScale = upperScale / origScale;
    }否则如果(normalizedScale&LT; lowerScale){
        normalizedScale = lowerScale;
        deltaScale = lowerScale / origScale;
    }    matrix.postScale(deltaScale,deltaScale,focusX,focusY);
    fixScaleTrans();
}


解决方案

所以对你的问题的解决方案是,在你的交换机的情况下的 TouchImageView

 开关(event.getAction()){}

我们需要添加一个条件

 情况下MotionEvent.ACTION_CANCEL:
     resetZoom();
     打破;

这是为我工作,我相信这一个会为你工作,以及。

I am using the TouchImageView class available at https://github.com/MikeOrtiz/TouchImageView.

when i move from the zoomed image to next and get back to previous image it still have zooming.

I want to reset the zooming image into normal position when swipe to previous image

and got answer by refer this question How to reset an image to the original position but I don't know exactly where have to put the this method in this library so can you please help me to guide?

Thanks in advance.

public class TouchImageView extends ImageView {

private static final String DEBUG = "DEBUG";

//
// SuperMin and SuperMax multipliers. Determine how much the image can be
// zoomed below or above the zoom boundaries, before animating back to the
// min/max zoom boundary.
//
private static final float SUPER_MIN_MULTIPLIER = .75f;
private static final float SUPER_MAX_MULTIPLIER = 1.25f;

//
// Scale of image ranges from minScale to maxScale, where minScale == 1
// when the image is stretched to fit view.
//
private float normalizedScale;

//
// Matrix applied to image. MSCALE_X and MSCALE_Y should always be equal.
// MTRANS_X and MTRANS_Y are the other values used. prevMatrix is the matrix
// saved prior to the screen rotating.
//
private Matrix matrix, prevMatrix;

private boolean onTouch = false;

private static enum State { NONE, DRAG, ZOOM, FLING, ANIMATE_ZOOM };
private State state;

private float minScale;
private float maxScale;
private float superMinScale;
private float superMaxScale;
private float[] m;

private Context context;
private Fling fling;

private ScaleType mScaleType;

private boolean onDrawReady;
private ZoomVariables delayedZoomVariables;

//
// Size of view and previous view size (ie before rotation)
//
private int viewWidth, viewHeight, prevViewWidth, prevViewHeight;

//
// Size of image when it is stretched to fit view. Before and After rotation.
//
private float matchViewWidth, matchViewHeight, prevMatchViewWidth, prevMatchViewHeight;

private ScaleGestureDetector mScaleDetector;
private GestureDetector mGestureDetector;

public TouchImageView(Context context) {
    super(context);
    sharedConstructing(context);
}

public TouchImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    sharedConstructing(context);
}

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    sharedConstructing(context);
}

OnScaleChangeListener _scaleChangeListener;             // Coded by siva // interface object for detecting the single and double tap on imageview 


public interface OnScaleChangeListener {
    public void onSingleTabfromTouchImage(boolean isSingleTabClick);
    public void onDoubleTapfromTouchImage(boolean isDoubleTapClick);
}

public synchronized void setOnScaleChangeListener(OnScaleChangeListener listener){
    _scaleChangeListener = listener;
}

private void sharedConstructing(Context context) {
    super.setClickable(true);
    this.context = context;
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    mGestureDetector = new GestureDetector(context, new GestureListener());
    matrix = new Matrix();
    prevMatrix = new Matrix();
    m = new float[9];
    normalizedScale = 1;
    if (mScaleType == null) {
        mScaleType = ScaleType.FIT_CENTER;
    }
    minScale = 1;
    maxScale = 3;
    superMinScale = SUPER_MIN_MULTIPLIER * minScale;
    superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
    setImageMatrix(matrix);
    setScaleType(ScaleType.MATRIX);
    setState(State.NONE);
    setOnTouchListener(new TouchImageViewListener());
    onDrawReady = false;
}

@Override
public void setImageResource(int resId) {
    super.setImageResource(resId);
    savePreviousImageValues();
    fitImageToView();
}

@Override
public void setImageBitmap(Bitmap bm) {
    super.setImageBitmap(bm);
    savePreviousImageValues();
    fitImageToView();
}

@Override
public void setImageDrawable(Drawable drawable) {
    super.setImageDrawable(drawable);
    savePreviousImageValues();
    fitImageToView();
}

@Override
public void setImageURI(Uri uri) {
    super.setImageURI(uri);
    savePreviousImageValues();
    fitImageToView();
}

@Override
public void setScaleType(ScaleType type) {
    if (type == ScaleType.FIT_START || type == ScaleType.FIT_END) {
        throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END");
    }
    if (type == ScaleType.MATRIX) {
        super.setScaleType(ScaleType.MATRIX);

    } else {
        mScaleType = type;
    }
}

@Override
public ScaleType getScaleType() {
    return mScaleType;
}

/**
 * Returns false if image is in initial, unzoomed state. False, otherwise.
 * @return true if image is zoomed
 */
public boolean isZoomed() {

    if(normalizedScale > 1)
        return true;
    else
        return false;
    //return normalizedScale != 1;
}

/**
 * Return a bitmap of the zoomed image as it appears within the view. This essentially
 * acts as a "screenshot" of the view and the size of the final bitmap is limited to the
 * resolution of the view itself.
 * @return bitmap of zoomed image
 */
public Bitmap getZoomedImage() {
    Bitmap bitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);                
    Canvas canvas = new Canvas(bitmap);
    layout(0, 0, viewWidth, viewHeight);
    draw(canvas);
    return bitmap;
}

/**
 * Return a bitmap of the zoomed image. This method is different from getZoomedImage() because
 * it cuts the image directly from the drawable source, and thus, is not limited by the resolution
 * of the view. Not supported with FIT_XY.
 * @return bitmap of zoomed image
 */
public Bitmap getZoomedImageFromSource() {
    if (mScaleType == ScaleType.FIT_XY) {
        throw new UnsupportedOperationException("getZoomedImageFromSource() not supported with FIT_XY");
    }
    Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
    Rect r = getZoomedRect();
    if (r.width() <= 0 || r.height() <= 0) {
        return null;
    }
    return Bitmap.createBitmap(bitmap, r.left, r.top, r.width(), r.height());
}

/**
 * Return a Rect representing the zoomed image in the coordinate system of the drawable source.
 * @return rect representing zoomed image
 */
public Rect getZoomedRect() {
    if (mScaleType == ScaleType.FIT_XY) {
        throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY");
    }
    PointF topLeft = getDrawablePointFromTouchPoint(0,0);
    PointF bottomRight = getDrawablePointFromTouchPoint(viewWidth, viewHeight);
    return new Rect((int) topLeft.x, (int) topLeft.y,(int) bottomRight.x, (int) bottomRight.y);
}

/**
 * Save the current matrix and view dimensions
 * in the prevMatrix and prevView variables.
 */
private void savePreviousImageValues() {
    if (matrix != null) {
        matrix.getValues(m);
        prevMatrix.setValues(m);
        prevMatchViewHeight = matchViewHeight;
        prevMatchViewWidth = matchViewWidth;
        prevViewHeight = viewHeight;
        prevViewWidth = viewWidth;
    }
}

@Override
public Parcelable onSaveInstanceState() {
    Bundle bundle = new Bundle();
    bundle.putParcelable("instanceState", super.onSaveInstanceState());
    bundle.putFloat("saveScale", normalizedScale);
    bundle.putFloat("matchViewHeight", matchViewHeight);
    bundle.putFloat("matchViewWidth", matchViewWidth);
    bundle.putInt("viewWidth", viewWidth);
    bundle.putInt("viewHeight", viewHeight);
    matrix.getValues(m);
    bundle.putFloatArray("matrix", m);
    return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
    if (state instanceof Bundle) {
        Bundle bundle = (Bundle) state;
        normalizedScale = bundle.getFloat("saveScale");
        m = bundle.getFloatArray("matrix");
        prevMatrix.setValues(m);
        prevMatchViewHeight = bundle.getFloat("matchViewHeight");
        prevMatchViewWidth = bundle.getFloat("matchViewWidth");
        prevViewHeight = bundle.getInt("viewHeight");
        prevViewWidth = bundle.getInt("viewWidth");
        super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
        return;
    }

    super.onRestoreInstanceState(state);
}

@Override
protected void onDraw(Canvas canvas) {
    onDrawReady = true;
    if (delayedZoomVariables != null) {
        setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType);
        delayedZoomVariables = null;
    }
    super.onDraw(canvas);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //savePreviousImageValues();
    resetZoom();
}

/**
 * Get the max zoom multiplier.
 * @return max zoom multiplier.
 */
public float getMaxZoom() {
    return maxScale;
}

/**
 * Set the max zoom multiplier. Default value: 3.
 * @param max max zoom multiplier.
 */
public void setMaxZoom(float max) {
    maxScale = max;
    superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
}

/**
 * Get the min zoom multiplier.
 * @return min zoom multiplier.
 */
public float getMinZoom() {
    return minScale;
}

/**
 * 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 the min zoom multiplier. Default value: 1.
 * @param min min zoom multiplier.
 */
public void setMinZoom(float min) {
    minScale = min;
    superMinScale = SUPER_MIN_MULTIPLIER * minScale;
}

/**
 * Reset zoom and translation to initial state.
 */
public void resetZoom() {
    normalizedScale = 1;
    fitImageToView();
}

/**
 * Set zoom to the specified scale. Image will be centered by default.
 * @param scale
 */
public void setZoom(float scale) {
    setZoom(scale, 0.5f, 0.5f);
}

/**
 * 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;
    }

    setScaleType(scaleType);
    resetZoom();
    scaleImage(scale, viewWidth / 2, viewHeight / 2, false);
    matrix.getValues(m);
    m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f));
    m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f));
    matrix.setValues(m);
    fixTrans();
    setImageMatrix(matrix);
}

/**
 * Set zoom parameters equal to another TouchImageView. Including scale, position,
 * and ScaleType.
 * @param TouchImageView
 */
public void setZoom(TouchImageView img) {
    PointF center = img.getCenterOfZoomedImage();
    setZoom(img.getCurrentZoom(), center.x, center.y, img.getScaleType());
}

/**
 * For a given point on the view (ie, a touch event), returns the
 * point relative to the original drawable's coordinate system.
 * @param x
 * @param y
 * @return PointF relative to original drawable's coordinate system.
 */
public PointF getDrawablePointFromTouchPoint(float x, float y) {
    return transformCoordTouchToBitmap(x, y, true);
}

/**
 * For a given point on the view (ie, a touch event), returns the
 * point relative to the original drawable's coordinate system.
 * @param p
 * @return PointF relative to original drawable's coordinate system.
 */
public PointF getDrawablePointFromTouchPoint(PointF p) {
    return transformCoordTouchToBitmap(p.x, p.y, true);
}

/**
 * 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 center of zoomed image
 */
public PointF getCenterOfZoomedImage() {
    Drawable drawable = getDrawable();
    if (drawable == null) {
        return null;
    }
    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();

    PointF point = getDrawablePointFromTouchPoint(viewWidth / 2, viewHeight / 2);
    point.x /= drawableWidth;
    point.y /= drawableHeight;
    return point;
}

/**
 * Performs boundary checking and fixes the image matrix if it 
 * is out of bounds.
 */
private void fixTrans() {
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X];
    float transY = m[Matrix.MTRANS_Y];

    float fixTransX = getFixTrans(transX, viewWidth, getImageWidth());
    float fixTransY = getFixTrans(transY, viewHeight, getImageHeight());

    if (fixTransX != 0 || fixTransY != 0) {
        matrix.postTranslate(fixTransX, fixTransY);
    }
}

/**
 * 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 (getImageHeight() < viewHeight) {
        m[Matrix.MTRANS_Y] = (viewHeight - getImageHeight()) / 2;
    }
    matrix.setValues(m);
}

private float getFixTrans(float trans, float viewSize, float contentSize) {
    float minTrans, maxTrans;

    if (contentSize <= viewSize) {
        minTrans = 0;
        maxTrans = viewSize - contentSize;

    } else {
        minTrans = viewSize - contentSize;
        maxTrans = 0;
    }

    if (trans < minTrans)
        return -trans + minTrans;
    if (trans > maxTrans)
        return -trans + maxTrans;
    return 0;
}

private float getFixDragTrans(float delta, float viewSize, float contentSize) {
    if (contentSize <= viewSize) {
        return 0;
    }
    return delta;
}

private float getImageWidth() {
    return matchViewWidth * normalizedScale;
}

private float getImageHeight() {
    return matchViewHeight * normalizedScale;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable drawable = getDrawable();
    if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
        setMeasuredDimension(0, 0);
        return;
    }

  /*  if(onTouch){
        //resetZoom();
        onTouch  = false;
    }
    else
        resetZoom();*/

    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    viewWidth = setViewSize(widthMode, widthSize, drawableWidth);
    viewHeight = setViewSize(heightMode, heightSize, drawableHeight);

    //
    // Set view dimensions
    //
    setMeasuredDimension(viewWidth, viewHeight);

    //
    // Fit content within view
    //
    fitImageToView();
}

/**
 * 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;
    }

    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();

    //
    // Scale image for view
    //
    float scaleX = (float) viewWidth / drawableWidth;
    float scaleY = (float) viewHeight / drawableHeight;

    switch (mScaleType) {
    case CENTER:
        scaleX = scaleY = 1;
        break;

    case CENTER_CROP:
        scaleX = scaleY = Math.max(scaleX, scaleY);
        break;

    case CENTER_INSIDE:
        scaleX = scaleY = Math.min(1, Math.min(scaleX, scaleY));

    case FIT_CENTER:
        scaleX = scaleY = Math.min(scaleX, scaleY);
        break;

    case FIT_XY:
        break;

    default:
        //
        // FIT_START and FIT_END not supported
        //
        throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END");

    }

    //
    // Center the image
    //
    float redundantXSpace = viewWidth - (scaleX * drawableWidth);
    float redundantYSpace = viewHeight - (scaleY * drawableHeight);
    matchViewWidth = viewWidth - redundantXSpace;
    matchViewHeight = viewHeight - redundantYSpace;
    if (!isZoomed()) {
        //
        // Stretch and center image to fit view
        //
        matrix.setScale(scaleX, scaleY);
        matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2);
        normalizedScale = 1;

    } 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(onTouch){
            if(matchViewWidth > drawableWidth){
                prevMatchViewHeight = 0;
                prevMatchViewWidth = 0;
                savePreviousImageValues();
            }
            onTouch = false;
        }
        if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) {
            //savePreviousImageValues();
            normalizedScale = 1;
        }

        prevMatrix.getValues(m);

        //
        // Rescale Matrix after rotation
        //
        m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale;
        m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale;

        //
        // TransX and TransY from previous matrix
        //
        float transX = m[Matrix.MTRANS_X];
        float transY = m[Matrix.MTRANS_Y];

        //
        // Width
        //
        float prevActualWidth = prevMatchViewWidth * normalizedScale;
        float actualWidth = getImageWidth();
        translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth);

        //
        // Height
        //
        float prevActualHeight = prevMatchViewHeight * normalizedScale;
        float actualHeight = getImageHeight();
        translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight);

        //
        // Set the matrix to the adjusted scale and translate values.
        //
        matrix.setValues(m);
    }
    fixTrans();
    setImageMatrix(matrix);
}

/**
 * Set view dimensions based on layout params
 * 
 * @param mode 
 * @param size
 * @param drawableWidth
 * @return
 */
private int setViewSize(int mode, int size, int drawableWidth) {
    int viewSize;
    switch (mode) {
    case MeasureSpec.EXACTLY:
        viewSize = size;
        break;

    case MeasureSpec.AT_MOST:
        viewSize = Math.min(drawableWidth, size);
        break;

    case MeasureSpec.UNSPECIFIED:
        viewSize = drawableWidth;
        break;

    default:
        viewSize = size;
        break;
    }
    return viewSize;
}

/**
 * Gesture Listener detects a single click or long click and passes that on
 * to the view's listener.
 * @author Ortiz
 *
 */
private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e)
    {
        onTouch = true;
        _scaleChangeListener.onSingleTabfromTouchImage(true);            // set the boolean for single tap //  Coded by siva.
        savePreviousImageValues();
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
        performLongClick();
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if (fling != null) {
            //
            // If a previous fling is still active, it should be cancelled so that two flings
            // are not run simultaenously.
            //
            fling.cancelFling();
        }
        fling = new Fling((int) velocityX, (int) velocityY);
        compatPostOnAnimation(fling);
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        boolean consumed = false;



        if (state == State.NONE) {
            float targetZoom = (normalizedScale == minScale) ? maxScale : minScale;
            DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, e.getX(), e.getY(), false);
            compatPostOnAnimation(doubleTap);
            consumed = true;
        }
        return consumed;
    }
}

/**
 * Responsible for all touch events. Handles the heavy lifting of drag and also sends
 * touch events to Scale Detector and Gesture Detector.
 * @author Ortiz
 *
 */
private class TouchImageViewListener implements OnTouchListener {

    //
    // Remember last point position for dragging
    //
    private PointF last = new PointF();

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        mGestureDetector.onTouchEvent(event);
        PointF curr = new PointF(event.getX(), event.getY());

        if (state == State.NONE || state == State.DRAG || state == State.FLING) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    last.set(curr);
                    if (fling != null)
                        fling.cancelFling();
                    setState(State.DRAG);
                    break;

                case MotionEvent.ACTION_MOVE:
                    if (state == State.DRAG) {
                        float deltaX = curr.x - last.x;
                        float deltaY = curr.y - last.y;
                        float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth());
                        float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight());
                        matrix.postTranslate(fixTransX, fixTransY);
                        fixTrans();
                        last.set(curr.x, curr.y);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    setState(State.NONE);
                    break;
            }
        }

        setImageMatrix(matrix);
        //
        // indicate event was handled
        //
        return true;
    }
}

/**
 * ScaleListener detects user two finger scaling and scales image.
 * @author Ortiz
 *
 */
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        setState(State.ZOOM);
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scaleImage(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY(), true);
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        super.onScaleEnd(detector);
        setState(State.NONE);
        boolean animateToZoomBoundary = false;
        float targetZoom = normalizedScale;
        if (normalizedScale > maxScale) {
            targetZoom = maxScale;
            animateToZoomBoundary = true;

        } else if (normalizedScale < minScale) {
            targetZoom = minScale;
            animateToZoomBoundary = true;
        }

        if (animateToZoomBoundary) {
            DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom, viewWidth / 2, viewHeight / 2, true);
            compatPostOnAnimation(doubleTap);
        }
    }
}

private void scaleImage(float deltaScale, float focusX, float focusY, boolean stretchImageToSuper) {

    float lowerScale, upperScale;
    if (stretchImageToSuper) {
        lowerScale = superMinScale;
        upperScale = superMaxScale;

    } else {
        lowerScale = minScale;
        upperScale = maxScale;
    }

    float origScale = normalizedScale;
    normalizedScale *= deltaScale;
    if (normalizedScale > upperScale) {
        normalizedScale = upperScale;
        deltaScale = upperScale / origScale;
    } else if (normalizedScale < lowerScale) {
        normalizedScale = lowerScale;
        deltaScale = lowerScale / origScale;
    }

    matrix.postScale(deltaScale, deltaScale, focusX, focusY);
    fixScaleTrans();
}

解决方案

So solution for your problem is, In your switch case in TouchImageView class

switch (event.getAction()) { }

We need to add one more condition

case MotionEvent.ACTION_CANCEL:
     resetZoom();
     break;

This is worked for me and i am sure this one will work for you as well.

这篇关于如何重置缩放图像为原始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆