如何将图像恢复到原来的位置 [英] How to reset an image to the original position

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

问题描述

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

我使用这个类来把一个放大和缩小功能,以我的形象问题是下一个,

当我改变TouchImageView它仍然有变焦和最后图像的位置,我想只是将其重置为初始位置什么的图像而没有任何缩放

这可能吗?

这是类

 包com.example.touch;进口android.content.Context;
进口android.graphics.Matrix;
进口android.graphics.PointF;
进口android.graphics.drawable.Drawable;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.ScaleGestureDetector;
进口android.view.View;
进口android.widget.ImageView;公共类TouchImageView扩展了ImageView的{矩阵矩阵;//我们可以在这3个国家之一
静态最终诠释NONE = 0;
静态最终诠释DRAG = 1;
静态最终诠释ZOOM = 2;
INT模式=无;//记住一些东西放大
去年的PointF =新的PointF();
的PointF开始=新的PointF();
浮minScale = 1F;
浮maxScale = 3F;
浮动[]米;
INT viewWidth,viewHeight;
静态最终诠释CLICK = 3;
浮saveScale = 1F;
保护浮球origWidth,origHeight;
INT oldMeasuredWidth,oldMeasuredHeight;
ScaleGestureDetector mScaleDetector;上下文语境;公共TouchImageView(上下文的背景下){
    超级(上下文);
    sharedConstructing(上下文);
}公共TouchImageView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    sharedConstructing(上下文);
}私人无效sharedConstructing(上下文的背景下){
    super.setClickable(真);
    this.context =背景;
    mScaleDetector =新ScaleGestureDetector(上下文,新ScaleListener());
    矩阵=新的Matrix();
    M =新的浮动[9];
    setImageMatrix(矩阵);
    setScaleType(ScaleType.MATRIX);    setOnTouchListener(新OnTouchListener(){        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            mScaleDetector.onTouchEvent(事件);
            的PointF CURR =新的PointF(event.getX(),event.getY());            开关(event.getAction()){
                案例MotionEvent.ACTION_DOWN:
                 last.set(CURR);
                    start.set(最后);
                    模式= DRAG;
                    打破;                案例MotionEvent.ACTION_MOVE:
                    如果(==模式拖动){
                        浮DELTAX = curr.x - last.x;
                        浮DELTAY = curr.y - last.y;
                        浮fixTransX = getFixDragTrans(DELTAX,viewWidth,origWidth * saveScale);
                        浮fixTransY = getFixDragTrans(DELTAY,viewHeight,origHeight * saveScale);
                        matrix.postTranslate(fixTransX,fixTransY);
                        fixTrans();
                        last.set(curr.x,curr.y);
                    }
                    打破;                案例MotionEvent.ACTION_UP:
                    模式=无;
                    INT xDiff =(INT)Math.abs(curr.x - start.x);
                    INT yDiff =(INT)Math.abs(curr.y - start.y);
                    如果(xDiff<单击&放大器;&安培; yDiff<点击)
                        performClick();
                    打破;                案例MotionEvent.ACTION_POINTER_UP:
                    模式=无;
                    打破;
            }            setImageMatrix(矩阵);
            无效();
            返回true; //表示事件已处理
        }    });
}公共无效setMaxZoom(浮X){
    maxScale = X;
}私有类ScaleListener扩展ScaleGestureDetector.SimpleOnScaleGestureListener {
    @覆盖
    公共布尔onScaleBegin(ScaleGestureDetector检测器){
        模式= ZOOM;
        返回true;
    }    @覆盖
    公共布尔onScale(ScaleGestureDetector检测器){
        浮mScaleFactor = detector.getScaleFactor();
        浮origScale = saveScale;
        saveScale * = mScaleFactor;
        如果(saveScale> maxScale){
            saveScale = maxScale;
            mScaleFactor = maxScale / origScale;
        }否则如果(saveScale< minScale){
            saveScale = minScale;
            mScaleFactor = minScale / origScale;
        }        如果(origWidth * saveScale< = || viewWidth * origHeight&saveScale LT = viewHeight)
            matrix.postScale(mScaleFactor,mScaleFactor,viewWidth / 2,viewHeight / 2);
        其他
            matrix.postScale(mScaleFactor,mScaleFactor,detector.getFocusX(),detector.getFocusY());        fixTrans();
        返回true;
    }
}无效fixTrans(){
    matrix.getValues​​(米);
    浮transX = M [Matrix.MTRANS_X]
    浮transY = M [Matrix.MTRANS_Y]    浮fixTransX = getFixTrans(transX,viewWidth,origWidth * saveScale);
    浮fixTransY = getFixTrans(transY,viewHeight,origHeight * saveScale);    如果(fixTransX!= 0 || fixTransY!= 0)
        matrix.postTranslate(fixTransX,fixTransY);
}浮动getFixTrans(反式浮动,浮动viewSize,浮contentSize){
    浮minTrans,MAXTRANS;    如果(contentSize< = viewSize){
        minTrans = 0;
        MAXTRANS = viewSize - contentSize;
    }其他{
        minTrans = viewSize - contentSize;
        MAXTRANS = 0;
    }    如果(反式LT; minTrans)
        返回 - 反+ minTrans;
    如果(反式GT; MAXTRANS)
        返回 - 反+ MAXTRANS;
    返回0;
}浮getFixDragTrans(浮动三角洲,浮viewSize,浮contentSize){
    如果(contentSize< = viewSize){
        返回0;
    }
    返回三角洲;
}@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    viewWidth = MeasureSpec.getSize(widthMeasureSpec);
    viewHeight = MeasureSpec.getSize(heightMeasureSpec);    //
    //在重新缩放旋转图片
    //
    如果(oldMeasuredHeight == viewWidth&放大器;&安培; oldMeasuredHeight == viewHeight
            || viewWidth == 0 || viewHeight == 0)
        返回;
    oldMeasuredHeight = viewHeight;
    oldMeasuredWidth = viewWidth;    如果(saveScale == 1){
        //适应屏幕。
        浮规模;        可绘制可绘制= getDrawable();
        如果(绘== NULL || drawable.getIntrinsicWidth()== 0 || drawable.getIntrinsicHeight()== 0)
            返回;
        INT bmWidth = drawable.getIntrinsicWidth();
        INT bmHeight = drawable.getIntrinsicHeight();        Log.d(bmSize,bmWidth:+ bmWidth +bmHeight:+ bmHeight);        漂浮的scaleX =(浮点)viewWidth /(浮点)bmWidth;
        漂浮的scaleY =(浮点)viewHeight /(浮点)bmHeight;
        规模= Math.min(将scaleX,的scaleY);
        matrix.setScale(秤,秤);        //中心图像
        浮动redundantYSpace =(浮点)viewHeight - (*规模(浮点)bmHeight);
        浮动redundantXSpace =(浮点)viewWidth - (*规模(浮点)bmWidth);
        redundantYSpace / =(浮点)2;
        redundantXSpace / =(浮点)2;        matrix.postTranslate(redundantXSpace,redundantYSpace);        origWidth = viewWidth - 2 * redundantXSpace;
        origHeight = viewHeight - 2 * redundantYSpace;
        setImageMatrix(矩阵);
    }
    fixTrans();
}
 }


解决方案

问题解决了,只需添加此功能的库

 公共无效reiniciarZoom()
{
    矩阵=新的Matrix(originalMatrix);
    saveScale = 1F;
    setImageMatrix(矩阵);
    无效();
}

和调用它,你要放置IMG原来的IMG每次

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

I use this class to put a zoom-in and zoom-out to my image the problem is the next one,

When i change the image of the TouchImageView it still have the zoom and the position of the last image and what i want is just reset it to the initial position without any zoom

is that possible?

this is the class

package com.example.touch;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;

public class TouchImageView extends ImageView {

Matrix matrix;

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;


int viewWidth, viewHeight;
static final int CLICK = 3;
float saveScale = 1f;
protected float origWidth, origHeight;
int oldMeasuredWidth, oldMeasuredHeight;


ScaleGestureDetector mScaleDetector;

Context context;

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

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

private void sharedConstructing(Context context) {
    super.setClickable(true);
    this.context = context;
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    matrix = new Matrix();
    m = new float[9];
    setImageMatrix(matrix);
    setScaleType(ScaleType.MATRIX);

    setOnTouchListener(new OnTouchListener() {

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

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                 last.set(curr);
                    start.set(last);
                    mode = DRAG;
                    break;

                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        float deltaX = curr.x - last.x;
                        float deltaY = curr.y - last.y;
                        float fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale);
                        float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale);
                        matrix.postTranslate(fixTransX, fixTransY);
                        fixTrans();
                        last.set(curr.x, curr.y);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    mode = NONE;
                    int xDiff = (int) Math.abs(curr.x - start.x);
                    int yDiff = (int) Math.abs(curr.y - start.y);
                    if (xDiff < CLICK && yDiff < CLICK)
                        performClick();
                    break;

                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
            }

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

    });
}

public void setMaxZoom(float x) {
    maxScale = x;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        mode = ZOOM;
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float mScaleFactor = detector.getScaleFactor();
        float origScale = saveScale;
        saveScale *= mScaleFactor;
        if (saveScale > maxScale) {
            saveScale = maxScale;
            mScaleFactor = maxScale / origScale;
        } else if (saveScale < minScale) {
            saveScale = minScale;
            mScaleFactor = minScale / origScale;
        }

        if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight)
            matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2, viewHeight / 2);
        else
            matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());

        fixTrans();
        return true;
    }
}

void fixTrans() {
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X];
    float transY = m[Matrix.MTRANS_Y];

    float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
    float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale);

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

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

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    viewWidth = MeasureSpec.getSize(widthMeasureSpec);
    viewHeight = MeasureSpec.getSize(heightMeasureSpec);

    //
    // Rescales image on rotation
    //
    if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
            || viewWidth == 0 || viewHeight == 0)
        return;
    oldMeasuredHeight = viewHeight;
    oldMeasuredWidth = viewWidth;

    if (saveScale == 1) {
        //Fit to screen.
        float scale;

        Drawable drawable = getDrawable();
        if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0)
            return;
        int bmWidth = drawable.getIntrinsicWidth();
        int bmHeight = drawable.getIntrinsicHeight();

        Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);

        float scaleX = (float) viewWidth / (float) bmWidth;
        float scaleY = (float) viewHeight / (float) bmHeight;
        scale = Math.min(scaleX, scaleY);
        matrix.setScale(scale, scale);

        // Center the image
        float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight);
        float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth);
        redundantYSpace /= (float) 2;
        redundantXSpace /= (float) 2;

        matrix.postTranslate(redundantXSpace, redundantYSpace);

        origWidth = viewWidth - 2 * redundantXSpace;
        origHeight = viewHeight - 2 * redundantYSpace;
        setImageMatrix(matrix);
    }
    fixTrans();
}
 }

解决方案

Problem solved, just add this function to the library

public void  reiniciarZoom()
{
    matrix = new Matrix(originalMatrix);
    saveScale = 1f;
    setImageMatrix(matrix);
    invalidate();
}

and call it every time you want to place your img to the original img

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

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