绘制矩形过的ImageView的亮点,可以放大出的机器人矩形、机器人、亮点、ImageView

2023-09-03 21:48:24 作者:与我私奔

我一直是指来自所以下面这个例子是他们。

1。绘制线在ImageView的

2。绘制图片在ImageView的

3.我已经使用这个ImageView的班我的形象成为放大出Image双指缩放code谷歌。

现在我所试图做的是:

我想要放大按捏屏幕和其他的一点是,当我做单一的水龙头一个矩形,应绘制在图像视图,我也希望这个矩形应该得到放大和放大器;用的ImageView的放大和缩小,我想通过 ScaleImageView 类使用它。

这个输出看起来应该像下面的图片。

我也知道,这是可以做到通过使用相对布局,也可以使用 SurfaceView 的安卓但我是新使用的表面观,我也担心,如果我用另一种浏览过的ImageView画那么做放大此两种观点工作和缩小。如果我使用SurfaceView了ImageView的话就图像可以能够放大和缩小。

通过使用这种双指缩放的输出的例子来自这里。例子,我使用双指缩放。

现在我画矩形在这个例子中单触按以下code。

  @覆盖
    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);
        Log.v(Log_tag,绘制图像视图);
        //位图_scratch = BitmapFactory.de codeResource(getResources(),R.drawable.rect_image);
        //canvas.drawColor(Color.BLACK);
        //canvas.drawBitmap(_scratch,10,10,空);
        绘制对象D = getDrawable();
        //位图位=((BitmapDrawable)D).getBitmap();
        点阵位图= Bitmap.createBitmap(100,100,Bitmap.Config.RGB_565);
        / *如果(位图!= NULL){
            如果(X1大于0 ||×2大于0 || Y1大于0 || Y2大于0){
                  Log.v(Log_tag,I​​mageBitmap是画);
                //帆布mCanvas =新的Canvas(位);
                //mCanvas.drawRect(x1,Y1,X2,Y2,mPaint);
                // canvas.cli preCT(左,上,右,下);
                   paint.setStyle(Paint.Style.FILL_AND_STROKE);
                    paint.setStrokeWidth(1);
                   paint.setColor(0xFF000000
                      +((INT)(preSET_ preSSURE * pressure)<< 16)
                      +((INT)(preSET_ preSSURE * pressure)<< 8)
                      +(INT)(preSET_ preSSURE * pressure));
                   //mCanvas.drawCircle(x1,Y1,(preSET_SIZE *大小),油漆);
            }

        } * /
        //canvas.save();
        //canvas.translate(mPosX,mPosY);
       // canvas.scale(mScaleFactor,mScaleFactor);
        mBitmapDrawable.draw(画布);
        油漆myPaint =新的油漆();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(1);
        Log.v(Log_tag,重绘,这点);
        canvas.drawRect(rect_x1-30,rect_y1-30,rect_x1 + 30,rect_y1 + 30,myPaint);
        mCanvasMatrix = canvas.getMatrix();
        mImageCanvas =帆布;

        canvas.setMatrix(mCanvasMatrix);
        //canvas.restore();

    }
 

更新

下面是我的类用于ImageView的双指缩放。

 公共类ImageViewScale扩展ImageView的实现OnTouchListener {
    @覆盖
    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);
        //canvas.save();
        //canvas.scale(mScale,mScale);
        mCanvasMatrix = canvas.getMatrix();
        油漆myPaint =新的油漆();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(1);
        如果(mCanvasMatrix!= NULL){
            如果(orignalRect!= NULL)
                mCanvasMatrix.ma preCT(orignalRect);
        }
        如果(orignalRect!= NULL){
            canvas.drawRect(orignalRect,myPaint);
        }
        //canvas.drawRect(rect_x1-30,rect_y1-30,rect_x1 + 30,rect_y1 + 30,myPaint);
        INT canavs_width = canvas.getWidth();
        INT canavs_height = canvas.getHeight();
        canvas.setMatrix(mCanvasMatrix);
        //canvas.setMatrix(mMatrix);
        如果(mDrawable!= NULL){
            //mDrawable.draw(canvas);
            Log.v(Log_tag,画用帆布做W:+ canavs_width +H:+ canavs_height);
        }
        //canvas.restore();
    }

    私人浮动MAX_SCALE = 2F;
    私人诠释DOUBLE_TAP_SECOND = 400;

    私人浮动CANVAS_MAX_SCALE = 2F;

    浮rect_x1 = 50;
    浮rect_y1 = 150;

    私人矩阵mMatrix;
    私人矩阵mCanvasMatrix;

    私人最终浮动[] mCanvasMatrixValues​​ =新的浮动[9];
    私人最终浮动[] mMatrixValues​​ =新的浮动[9];
    RectF orignalRect;
    私人绘制对象mDrawable;
    私人ImageView的mImageView;
    //显示宽度高度。
    私人诠释mWidth;
    私人诠释mHeight;

    私人诠释mIntrinsicWidth;
    私人诠释mIntrinsicHeight;

    私人诠释mCanvasWidth;
    私人诠释mCanvasHeight;


    私人浮动mScale;
    私人浮动mMinScale;

    私人浮动mCanvasMinScale;

    //枪王确定
    私人长mLastTime = 0;
    私人布尔isDoubleTap;
    私人诠释mDoubleTapX;
    私人诠释mDoubleTapY;

    私人浮动米prevDistance;
    私人布尔isScaling;

    私人诠释米prevMoveX;
    私人诠释米prevMoveY;

    字符串变量=ScaleImageView;

    公共ImageViewScale(上下文的背景下,的AttributeSet attr)使用{
        超(背景下,ATTR);
        初始化();
    }

    公共ImageViewScale(上下文的背景下){
        超(上下文);
        初始化();
    }

    @覆盖
    公共无效setImageBitmap(位图BM){
        super.setImageBitmap(BM);
        this.initialize();

    }

    私人无效初始化(){
        this.setScaleType(ScaleType.MATRIX);
        this.mMatrix =新的Matrix();
        绘制对象D = getDrawable();
        mDrawable = D;
        如果(D!= NULL){
            mIntrinsicWidth = d.getIntrinsicWidth();
            mIntrinsicHeight = d.getIntrinsicHeight();
            setOnTouchListener(本);
        }
    }

    @覆盖
    保护布尔的setFrame(INT L,INT T,INT R,int b)在{
        Log.v(Log_tag,大小在这里+ 1 + T + R + B);
        mWidth = R  -  1;
        mHeight = B  - 吨;

        mMatrix.reset();
        mScale =(浮点)R /(浮点)mIntrinsicWidth;
        INT paddingHeight = 0;
        INT paddingWidth = 0;
        //缩放垂直
        如果(mScale * mIntrinsicHeight> mHeight){
            mScale =(浮点)mHeight /(浮点)mIntrinsicHeight;
            mMatrix.postScale(mScale,mScale);
            paddingWidth =(R  -  mWidth)/ 2;
            paddingHeight = 0;
            //缩放水平
        } 其他 {
            mMatrix.postScale(mScale,mScale);
            paddingHeight =(二 -  mHeight)/ 2;
            paddingWidth = 0;
        }
        mMatrix.postTranslate(paddingWidth,paddingHeight);

        setImageMatrix(mMatrix);
        mMinScale = mScale;
        zoomTo(mScale,mWidth / 2,mHeight / 2);
        切割();
        返回super.setFrame(L,T,R,B);
    }

    保护浮动的getValue(矩阵矩阵,诠释whichValue){
        matrix.getValues​​(mMatrixValues​​);
        返回mMatrixValues​​ [whichValue]
    }
    //新添加
    保护浮动getCanvasValue(矩阵矩阵,诠释whichvalues​​){
        mCanvasMatrix.getValues​​(mCanvasMatrixValues​​);
        返回mCanvasMatrixValues​​ [whichvalues​​]
    }

    保护浮动中的getScale(){
        返回的getValue(mMatrix,Matrix.MSCALE_X);
    }

    //新添加的方法
    保护浮动getCanvasScale(){
        返回getCanvasValue(mCanvasMatrix,Matrix.MSCALE_X);
    }

    保护浮动getTranslateX(){
        返回的getValue(mMatrix,Matrix.MTRANS_X);
    }

    //新添加的方法
    保护浮动getCanvasTranslateX(){
        返回getCanvasValue(mCanvasMatrix,Matrix.MTRANS_X);
    }


    保护浮动getTranslateY(){
        返回的getValue(mMatrix,Matrix.MTRANS_Y);
    }

    //新增加的方法
    保护浮动getCanvasTranslateY(){
        返回getCanvasValue(mCanvasMatrix,Matrix.MTRANS_Y);
    }

    保护无效maxZoomTo(INT X,int y)对{
        如果(mMinScale =的getScale()及及(的getScale() -  mMinScale)GT;!0.1F){
            //门槛0.1F
            浮规模= mMinScale /中的getScale();
            zoomTo(规模,X,Y);
        } 其他 {
            浮规模= MAX_SCALE /中的getScale();
            zoomTo(规模,X,Y);
        }
    }




    保护无效zoomTo(浮点规模,诠释的x,int y)对{
        如果(中的getScale()*规模和LT; mMinScale){
            返回;
        }
        如果(规模> = 1&安培;&安培;中的getScale()*规模> MAX_SCALE){
            返回;
        }
        mMatrix.postScale(秤,秤);
        //移动到中心
        mMatrix.postTranslate( - (mWidth *标度 -  mWidth)/ 2,
                 - (mHeight *规模 -  mHeight)/ 2);

        //移动x和y的距离
        mMatrix.postTranslate( - (X  - (mWidth / 2))*标度,0);
        mMatrix.postTranslate(0, - (Y  - (mHeight / 2))*标度);
        setImageMatrix(mMatrix);
    }


    保护无效zoomToCanvas(浮点规模,诠释的x,int y)对{
        如果(getCanvasScale()*规模和LT; mCanvasMinScale){
            返回;
        }

        如果(规模> = 1&安培;&安培; getCanvasScale()*规模> CANVAS_MAX_SCALE){
            返回;
        }
        mCanvasMatrix.postScale(秤,秤);



    }

    公共无效切割(){
        INT宽度=(int)的(mIntrinsicWidth *的getScale());
        INT高度=(int)的(mIntrinsicHeight *的getScale());
        如果(getTranslateX()<  - (宽 -  mWidth)){
            mMatrix.postTranslate( - (getTranslateX()+宽 -  mWidth),0);
        }
        如果(getTranslateX()大于0){
            mMatrix.postTranslate(-getTranslateX(),0);
        }
        如果(getTranslateY()<  - (高度 -  mHeight)){
            mMatrix.postTranslate(0, - (getTranslateY()+高 -  mHeight));
        }
        如果(getTranslateY()大于0){
            mMatrix.postTranslate(0,-getTranslateY());
        }
        如果(宽度LT; mWidth){
            mMatrix.postTranslate((mWidth  - 宽度)/ 2,0);
        }
        如果(高度< mHeight){
            mMatrix.postTranslate(0,(mHeight  - 高度)/ 2);
        }
        setImageMatrix(mMatrix);
    }

    私人浮动距离(浮动X0,X1浮球,浮球Y0,Y1浮动){
        浮动X = X0  -  X1;
        浮动Y = Y0  -  Y1;
        返回FloatMath.sqrt(X * X + Y * Y);
    }

    私人浮动dispDistance(){
        返回FloatMath.sqrt(mWidth * mWidth + mHeight * mHeight);
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件){
        INT touchCount = event.getPointerCount();
        开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:
        案例MotionEvent.ACTION_POINTER_1_DOWN:
        案例MotionEvent.ACTION_POINTER_2_DOWN:
            如果(touchCount> = 2){
                浮距离=距离(event.getX(0),event.getX(1),
                        event.getY(0),event.getY(1));
                米prevDistance =距离;
                isScaling = TRUE;
            } 其他 {
                如果(System.currentTimeMillis的()&其中; = mLastTime + DOUBLE_TAP_SECOND){
                    如果(30> Math.abs(M prevMoveX  -  event.getX())
                            + Math.abs(M prevMoveY  -  event.getY())){
                        isDoubleTap = TRUE;
                        mDoubleTapX =(int)的event.getX();
                        mDoubleTapY =(int)的event.getY();
                    }
                }
                mLastTime = System.currentTimeMillis的();
                米prevMoveX =(INT)event.getX();
                米prevMoveY =(INT)event.getY();
            }
            打破;
        案例MotionEvent.ACTION_MOVE:
            如果(touchCount> = 2&安培;&安培; isScaling){
                浮DIST =距离(event.getX(0),event.getX(1),
                        event.getY(0),event.getY(1));
                浮规模=(DIST  - 米prevDistance)/ dispDistance();
                米prevDistance = DIST;
                规模+ = 1;
                规模=秤*规模;
                zoomTo(规模,mWidth / 2,mHeight / 2);
                切割();
            }否则,如果(!isScaling){
                INT distanceX = M prevMoveX  - (INT)event.getX();
                INT distanceY = M prevMoveY  - (INT)event.getY();
                米prevMoveX =(INT)event.getX();
                米prevMoveY =(INT)event.getY();
                mMatrix.postTranslate(-distanceX,-distanceY);
                切割();
            }
            打破;
        案例MotionEvent.ACTION_UP:
        案例MotionEvent.ACTION_POINTER_UP:
        案例MotionEvent.ACTION_POINTER_2_UP:
            如果(event.getPointerCount()&其中; = 1){
                isScaling = FALSE;
                如果(isDoubleTap){
                    如果(30> Math.abs(mDoubleTapX  -  event.getX())
                            + Math.abs(mDoubleTapY  -  event.getY())){
                        maxZoomTo(mDoubleTapX,mDoubleTapY);
                        切割();
                    }
                }
            }
            isDoubleTap = FALSE;
            打破;
        }
        返回true;
    }

    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        INT count_touch = event.getPointerCount();
        开关(event.getAction()){
        案例MotionEvent.ACTION_UP:
            浮point_x = event.getX();
            浮point_y = event.getY();
            rect_x1 = point_x;
            rect_y1 = point_y;
            如果(count_touch == 1){
                orignalRect =新RectF(rect_x1-30,rect_y1-30,rect_x1 + 30,rect_y1 + 30);
                无效();
            }
            打破;
        }

        返回super.onTouchEvent(事件);
    }
}
 
如图①,在Rt ABC中, C 90 ,AB 10,BC 6,点P从点A出发,沿折线AB﹣BC向终点C运动,在AB上以每秒5个单位长度的速度运动,在BC上以每秒3个单位长度的速度运动,点Q从点

解决方案

您可能希望签 Matrix.ma preCT 。使用这种方法用相同量来转换矩形作为imageview的图像

 布尔onTouch(MotionEvent EV){
    ....
    //这个矩形的尺寸应该是初始值,应该是一个成员。
    mOriginalRect =新RectF(rect_x1-30,rect_y1-30,rect_x1 + 30,rect_y1 + 30);
    .....
}

@覆盖
保护无效的OnDraw(帆布油画){
    ....
    mCanvasMatrix = canvas.getMatrix(); ///矩阵应该有刻度值..
    mCanvasMatrix.ma preCT(TEM $ P $厘,mOriginalRect); // mOriginalRect为src
    canvas.drawRect(TEM $ P $厘,myPaint); //画TEM $ P $厘..
    ....
}
 

I have been refer this example from SO below are they.

1.Draw Line Over ImageView

2.Draw Picture Over ImageView

3.I have use this ImageView Class for my image to become zoom in-out Image Pinch zoom code google.

Now what i am try to do is :

I want to make zoom in by pinching the screen and other point is that when i do single tap a rectangle should be drawn over image view and i also want this rectangle should get zoom in & out with imageView's Zoom in and Zoom Out and i want to use it through ScaleImageView class.

Output of this should look like below image.

And i also known that ,this can be done by Using Relative Layout or may be by using SurfaceViewin android but i am new for use Surface view and i also worry about if i use Another View over imageView to draw then do this both view work for zoom in and out . If i use SurfaceView over ImageView then do Image Can be able to zoom in and out .

By using this pinch zoom in-out example from here.example that i use pinch zoom.

Now i draw Rectangle on Single Touch by below code in that example.

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.v("Log_tag", "Draw Image View");
        //Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.rect_image);
        //canvas.drawColor(Color.BLACK);
        //canvas.drawBitmap(_scratch, 10, 10, null);
        Drawable d = getDrawable();
        //Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
        /*if(bitmap!=null){
            if (x1 > 0 || x2 > 0 || y1 > 0 || y2 > 0){
                  Log.v("Log_tag", "ImageBitmap is draw");
                //Canvas mCanvas=new Canvas(bitmap);
                //mCanvas.drawRect(x1, y1, x2, y2, mPaint);
                //  canvas.clipRect(left, top, right, bottom);
                   paint.setStyle(Paint.Style.FILL_AND_STROKE);
                    paint.setStrokeWidth(1);
                   paint.setColor(0xFF000000
                      + ((int)(PRESET_PRESSURE * pressure) <<16)
                      + ((int)(PRESET_PRESSURE * pressure) << 8)
                      + (int)(PRESET_PRESSURE * pressure));
                   //mCanvas.drawCircle(x1, y1, (PRESET_SIZE * size), paint);
            }

        }*/
        //canvas.save();
        //canvas.translate(mPosX, mPosY);
       // canvas.scale(mScaleFactor, mScaleFactor);
        mBitmapDrawable.draw(canvas);
        Paint myPaint = new Paint();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(1);
        Log.v("Log_tag", "Redraw with this point");
        canvas.drawRect(rect_x1-30,rect_y1-30, rect_x1+30, rect_y1+30, myPaint);
        mCanvasMatrix=canvas.getMatrix();
        mImageCanvas=canvas;

        canvas.setMatrix(mCanvasMatrix);
        //canvas.restore();

    }

UPDATE

Below is my class used for ImageView pinch zoom.

public class ImageViewScale extends ImageView implements OnTouchListener {
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.save();
        //canvas.scale(mScale, mScale);
        mCanvasMatrix=canvas.getMatrix();
        Paint myPaint = new Paint();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(1);
        if(mCanvasMatrix!=null){
            if(orignalRect!=null)
                mCanvasMatrix.mapRect(orignalRect);
        }
        if(orignalRect!=null){
            canvas.drawRect(orignalRect,myPaint);
        }
        //canvas.drawRect(rect_x1-30,rect_y1-30, rect_x1+30, rect_y1+30, myPaint);
        int canavs_width=canvas.getWidth();
        int canavs_height=canvas.getHeight();
        canvas.setMatrix(mCanvasMatrix);
        //canvas.setMatrix(mMatrix);
        if(mDrawable!=null){
            //mDrawable.draw(canvas);
            Log.v("Log_tag", "draw with Canvas is done  W:"+ canavs_width+"H:"+ canavs_height);
        }
        //canvas.restore();
    }

    private float MAX_SCALE = 2f;
    private int DOUBLE_TAP_SECOND = 400;

    private float CANVAS_MAX_SCALE=2f;

    float rect_x1=50;
    float rect_y1=150;

    private Matrix mMatrix;
    private Matrix mCanvasMatrix;

    private final float[] mCanvasMatrixValues=new float[9];
    private final float[] mMatrixValues = new float[9];
    RectF orignalRect;
    private Drawable mDrawable;
    private ImageView mImageView;
    // display width height.
    private int mWidth;
    private int mHeight;

    private int mIntrinsicWidth;
    private int mIntrinsicHeight;

    private int mCanvasWidth;
    private int mCanvasHeight;


    private float mScale;
    private float mMinScale;

    private float mCanvasMinScale;

    // double tap for determining
    private long mLastTime = 0;
    private boolean isDoubleTap;
    private int mDoubleTapX;
    private int mDoubleTapY;

    private float mPrevDistance;
    private boolean isScaling;

    private int mPrevMoveX;
    private int mPrevMoveY;

    String TAG = "ScaleImageView";

    public ImageViewScale(Context context, AttributeSet attr) {
        super(context, attr);
        initialize();
    }

    public ImageViewScale(Context context) {
        super(context);
        initialize();
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        this.initialize();

    }

    private void initialize() {
        this.setScaleType(ScaleType.MATRIX);
        this.mMatrix = new Matrix();
        Drawable d = getDrawable();
        mDrawable=d;
        if (d != null) {
            mIntrinsicWidth = d.getIntrinsicWidth();
            mIntrinsicHeight = d.getIntrinsicHeight();
            setOnTouchListener(this);
        }
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {
        Log.v("Log_tag", "Size are here "+ l + t + r+ b);
        mWidth = r - l;
        mHeight = b - t;

        mMatrix.reset();
        mScale = (float) r / (float) mIntrinsicWidth;
        int paddingHeight = 0;
        int paddingWidth = 0;
        // scaling vertical
        if (mScale * mIntrinsicHeight > mHeight) {
            mScale = (float) mHeight / (float) mIntrinsicHeight;
            mMatrix.postScale(mScale, mScale);
            paddingWidth = (r - mWidth) / 2;
            paddingHeight = 0;
            // scaling horizontal
        } else {
            mMatrix.postScale(mScale, mScale);
            paddingHeight = (b - mHeight) / 2;
            paddingWidth = 0;
        }
        mMatrix.postTranslate(paddingWidth, paddingHeight);

        setImageMatrix(mMatrix);
        mMinScale = mScale;
        zoomTo(mScale, mWidth / 2, mHeight / 2);
        cutting();
        return super.setFrame(l, t, r, b);
    }

    protected float getValue(Matrix matrix, int whichValue) {
        matrix.getValues(mMatrixValues);
        return mMatrixValues[whichValue];
    }
    //New Added
    protected float getCanvasValue(Matrix matrix,int whichvalues){
        mCanvasMatrix.getValues(mCanvasMatrixValues);
        return mCanvasMatrixValues[whichvalues];
    }

    protected float getScale() {
        return getValue(mMatrix, Matrix.MSCALE_X);
    }

    //New added  Method
    protected float getCanvasScale(){
        return getCanvasValue(mCanvasMatrix, Matrix.MSCALE_X);
    }

    protected float getTranslateX() {
        return getValue(mMatrix, Matrix.MTRANS_X);
    }

    //New added Method
    protected float getCanvasTranslateX(){
        return getCanvasValue(mCanvasMatrix, Matrix.MTRANS_X);
    }


    protected float getTranslateY() {
        return getValue(mMatrix, Matrix.MTRANS_Y);
    }

    //New Added Method
    protected float getCanvasTranslateY(){
        return getCanvasValue(mCanvasMatrix, Matrix.MTRANS_Y);
    }

    protected void maxZoomTo(int x, int y) {
        if (mMinScale != getScale() && (getScale() - mMinScale) > 0.1f) {
            // threshold 0.1f
            float scale = mMinScale / getScale();
            zoomTo(scale, x, y);
        } else {
            float scale = MAX_SCALE / getScale();
            zoomTo(scale, x, y);
        }
    }




    protected void zoomTo(float scale, int x, int y) {
        if (getScale() * scale < mMinScale) {
            return;
        }
        if (scale >= 1 && getScale() * scale > MAX_SCALE) {
            return;
        }
        mMatrix.postScale(scale, scale);
        // move to center
        mMatrix.postTranslate(-(mWidth * scale - mWidth) / 2,
                -(mHeight * scale - mHeight) / 2);

        // move x and y distance
        mMatrix.postTranslate(-(x - (mWidth / 2)) * scale, 0);
        mMatrix.postTranslate(0, -(y - (mHeight / 2)) * scale);
        setImageMatrix(mMatrix);
    }


    protected void zoomToCanvas(float scale,int x,int y){
        if(getCanvasScale()* scale<mCanvasMinScale){
            return;
        }

        if(scale>=1 && getCanvasScale()*scale> CANVAS_MAX_SCALE){
            return;
        }
        mCanvasMatrix.postScale(scale, scale);



    }

    public void cutting() {
        int width = (int) (mIntrinsicWidth * getScale());
        int height = (int) (mIntrinsicHeight * getScale());
        if (getTranslateX() < -(width - mWidth)) {
            mMatrix.postTranslate(-(getTranslateX() + width - mWidth), 0);
        }
        if (getTranslateX() > 0) {
            mMatrix.postTranslate(-getTranslateX(), 0);
        }
        if (getTranslateY() < -(height - mHeight)) {
            mMatrix.postTranslate(0, -(getTranslateY() + height - mHeight));
        }
        if (getTranslateY() > 0) {
            mMatrix.postTranslate(0, -getTranslateY());
        }
        if (width < mWidth) {
            mMatrix.postTranslate((mWidth - width) / 2, 0);
        }
        if (height < mHeight) {
            mMatrix.postTranslate(0, (mHeight - height) / 2);
        }
        setImageMatrix(mMatrix);
    }

    private float distance(float x0, float x1, float y0, float y1) {
        float x = x0 - x1;
        float y = y0 - y1;
        return FloatMath.sqrt(x * x + y * y);
    }

    private float dispDistance() {
        return FloatMath.sqrt(mWidth * mWidth + mHeight * mHeight);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int touchCount = event.getPointerCount();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_1_DOWN:
        case MotionEvent.ACTION_POINTER_2_DOWN:
            if (touchCount >= 2) {
                float distance = distance(event.getX(0), event.getX(1),
                        event.getY(0), event.getY(1));
                mPrevDistance = distance;
                isScaling = true;
            } else {
                if (System.currentTimeMillis() <= mLastTime + DOUBLE_TAP_SECOND) {
                    if (30 > Math.abs(mPrevMoveX - event.getX())
                            + Math.abs(mPrevMoveY - event.getY())) {
                        isDoubleTap = true;
                        mDoubleTapX = (int) event.getX();
                        mDoubleTapY = (int) event.getY();
                    }
                }
                mLastTime = System.currentTimeMillis();
                mPrevMoveX = (int) event.getX();
                mPrevMoveY = (int) event.getY();
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (touchCount >= 2 && isScaling) {
                float dist = distance(event.getX(0), event.getX(1),
                        event.getY(0), event.getY(1));
                float scale = (dist - mPrevDistance) / dispDistance();
                mPrevDistance = dist;
                scale += 1;
                scale = scale * scale;
                zoomTo(scale, mWidth / 2, mHeight / 2);
                cutting();
            } else if (!isScaling) {
                int distanceX = mPrevMoveX - (int) event.getX();
                int distanceY = mPrevMoveY - (int) event.getY();
                mPrevMoveX = (int) event.getX();
                mPrevMoveY = (int) event.getY();
                mMatrix.postTranslate(-distanceX, -distanceY);
                cutting();
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_POINTER_2_UP:
            if (event.getPointerCount() <= 1) {
                isScaling = false;
                if (isDoubleTap) {
                    if (30 > Math.abs(mDoubleTapX - event.getX())
                            + Math.abs(mDoubleTapY - event.getY())) {
                        maxZoomTo(mDoubleTapX, mDoubleTapY);
                        cutting();
                    }
                }
            }
            isDoubleTap = false;
            break;
        }
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int count_touch=event.getPointerCount();
        switch(event.getAction()){
        case MotionEvent.ACTION_UP:
            float point_x=event.getX();
            float point_y=event.getY();
            rect_x1=point_x;
            rect_y1=point_y;
            if(count_touch==1){
                orignalRect=new RectF(rect_x1-30, rect_y1-30, rect_x1+30,  rect_y1+30);
                invalidate();
            }
            break;
        }

        return super.onTouchEvent(event);
    }
}

解决方案

You might want to checkout Matrix.mapRect. Use this method to transform the rectangle by the same amount as the image in the imageview.

boolean onTouch(MotionEvent ev) {
    ....
    // this rect dimensions should be initial values and should be a member.
    mOriginalRect = new RectF(rect_x1-30, rect_y1-30, rect_x1+30, rect_y1+30); 
    .....
}

@Override
protected void onDraw(Canvas canvas) {
    ....
    mCanvasMatrix = canvas.getMatrix();  ///matrix should have scale values..
    mCanvasMatrix.mapRect(tempRect, mOriginalRect); // mOriginalRect is src 
    canvas.drawRect(tempRect, myPaint);   // draw tempRect..
    ....
}