在ImageView上绘制矩形以突出显示可以在android中放大

Draw Rectangle Over ImageView for highlight that can be zoom in-out in android(在ImageView上绘制矩形以突出显示可以在android中放大)

本文介绍了在ImageView上绘制矩形以突出显示可以在android中放大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从下面的 SO 中引用了这个示例.

1..

现在我通过该示例中的以下代码在 Single Touch 上绘制矩形.

@Override受保护的无效onDraw(帆布画布){超级.onDraw(画布);Log.v("Log_tag", "绘制图像视图");//位图_scratch = BitmapFactory.decodeResource(getResources(), R.drawable.rect_image);//canvas.drawColor(Color.BLACK);//canvas.drawBitmap(_scratch, 10, 10, null);可绘制 d = getDrawable();//位图bitmap = ((BitmapDrawable)d).getBitmap();位图位图 = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);/*如果(位图!=空){如果 (x1 > 0 || x2 > 0 || y1 > 0 || y2 > 0){Log.v("Log_tag", "ImageBitmap is draw");//画布 mCanvas=new Canvas(位图);//mCanvas.drawRect(x1, y1, x2, y2, mPaint);//canvas.clipRect(left, top, right, bottom);paint.setStyle(Paint.Style.FILL_AND_STROKE);油漆.setStrokeWidth(1);油漆.setColor(0xFF000000+ ((int)(PRESET_PRESSURE * 压力) <<16)+ ((int)(PRESET_PRESSURE * 压力) << 8)+ (int)(PRESET_PRESSURE * 压力));//mCanvas.drawCircle(x1, y1, (PRESET_SIZE * size), paint);}}*///canvas.save();//canvas.translate(mPosX, mPosY);//canvas.scale(mScaleFactor, mScaleFactor);mBitmapDrawable.draw(画布);油漆 myPaint = new Paint();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(帆布画布){超级.onDraw(画布);//canvas.save();//canvas.scale(mScale, mScale);mCanvasMatrix=canvas.getMatrix();油漆 myPaint = new Paint();myPaint.setColor(Color.GREEN);myPaint.setStyle(Paint.Style.STROKE);myPaint.setStrokeWidth(1);如果(mCanvasMatrix!=空){如果(原始矩形!=空)mCanvasMatrix.mapRect(orignalRect);}如果(原始矩形!=空){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!=空){//mDrawable.draw(canvas);Log.v("Log_tag", "用 Canvas 绘制完成 W:"+ canavs_width+"H:"+ canavs_height);}//canvas.restore();}私人浮动 MAX_SCALE = 2f;私人int DOUBLE_TAP_SECOND = 400;私人浮动 CANVAS_MAX_SCALE=2f;浮动 rect_x1=50;浮动 rect_y1=150;私有矩阵 mMatrix;私有矩阵 mCanvasMatrix;私有最终浮点数[] mCanvasMatrixValues=新浮点数[9];私有最终浮点数[] mMatrixValues = 新浮点数[9];RectF 原始矩形;私有可绘制 mDrawable;私有ImageView mImageView;//显示宽度高度.私人 int mWidth;私人 int mHeight;私人 int mIntrinsicWidth;私人 int mIntrinsicHeight;私人 int mCanvasWidth;私人 int mCanvasHeight;私人浮动 mScale;私人浮动 mminScale;私人浮动 mCanvasMinScale;//双击确定私人长 mLastTime = 0;私有布尔 isDoubleTap;私人int mDoubleTapX;私人int mDoubleTapY;私人浮动 mPrevDistance;私有布尔 isScaling;私人int mPrevMoveX;私人int mPrevMoveY;String TAG = "ScaleImageView";公共 ImageViewScale(上下文上下文,AttributeSet attr){超级(上下文,属性);初始化();}公共ImageViewScale(上下文上下文){超级(上下文);初始化();}@覆盖公共无效 setImageBitmap(位图 bm){super.setImageBitmap(bm);this.initialize();}私人无效初始化(){this.setScaleType(ScaleType.MATRIX);this.mMatrix = new Matrix();可绘制 d = getDrawable();mDrawable=d;如果(d!= null){mIntrinsicWidth = d.getIntrinsicWidth();mIntrinsicHeight = d.getIntrinsicHeight();setOnTouchListener(this);}}@覆盖protected boolean setFrame(int l, int t, int r, int b) {Log.v("Log_tag", "这里是大小"+ l + t + r+ b);mWidth = r - l;mHeight = b - t;mMatrix.reset();mScale = (float) r/(float) mIntrinsicWidth;整数填充高度 = 0;int paddingWidth = 0;//垂直缩放if (mScale * mIntrinsicHeight > mHeight) {mScale = (float) mHeight/(float) mIntrinsicHeight;mMatrix.postScale(mScale, mScale);paddingWidth = (r - mWidth)/2;填充高度 = 0;//水平缩放} 别的 {mMatrix.postScale(mScale, mScale);paddingHeight = (b - mHeight)/2;填充宽度 = 0;}mMatrix.postTranslate(paddingWidth, paddingHeight);setImageMatrix(mMatrix);mMinScale = mScale;zoomTo(mScale, mWidth/2, mHeight/2);切割();返回 super.setFrame(l, t, r, b);}protected float getValue(Matrix matrix, int whichValue) {矩阵.getValues(mMatrixValues);返回 mMatrixValues[whichValue];}//新添加的protected float getCanvasValue(Matrix matrix,int 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);}受保护的 void maxZoomTo(int x, int y) {if (mMinScale != getScale() && (getScale() - mMinScale) > 0.1f) {//阈值 0.1f浮动比例 = mminScale/getScale();缩放到(缩放,x,y);} 别的 {浮动比例 = MAX_SCALE/getScale();缩放到(缩放,x,y);}}protected void zoomTo(float scale, int x, int y) {if (getScale() * scale < mminScale) {返回;}if (scale >= 1 && getScale() * scale > MAX_SCALE) {返回;}mMatrix.postScale(规模,规模);//移动到中心mMatrix.postTranslate(-(mWidth * scale - mWidth)/2,-(mHeight * 比例 - mHeight)/2);//移动 x 和 y 距离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()* 比例<mCanvasMinScale){返回;}if(scale>=1 && getCanvasScale()*scale> CANVAS_MAX_SCALE){返回;}mCanvasMatrix.postScale(规模,规模);}公共无效切割(){int width = (int) (mIntrinsicWidth * getScale());int 高度 = (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());}如果(宽度 < 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);}私人浮动距离(){返回 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:if (touchCount >= 2) {浮动距离=距离(event.getX(0),event.getX(1),event.getY(0), event.getY(1));mPrevDistance = 距离;isScaling =真;} 别的 {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();}休息;案例 MotionEvent.ACTION_MOVE:if (touchCount >= 2 && isScaling) {浮动距离=距离(event.getX(0),event.getX(1),event.getY(0), event.getY(1));浮动比例 = (dist - mPrevDistance)/dispDistance();mPrevDistance = 距离;比例+= 1;比例=比例*比例;zoomTo(缩放, mWidth/2, mHeight/2);切割();} 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);切割();}休息;案例 MotionEvent.ACTION_UP:案例 MotionEvent.ACTION_POINTER_UP:案例 MotionEvent.ACTION_POINTER_2_UP:if (event.getPointerCount() <= 1) {isScaling = 假;如果(isDoubleTap){if (30 > Math.abs(mDoubleTapX - event.getX())+ Math.abs(mDoubleTapY - event.getY())) {maxZoomTo(mDoubleTapX, mDoubleTapY);切割();}}}isDoubleTap = 假;休息;}返回真;}@覆盖public boolean onTouch(View v, MotionEvent 事件) {int count_touch=event.getPointerCount();开关(事件.getAction()){案例 MotionEvent.ACTION_UP:浮点数_x=event.getX();float point_y=event.getY();rect_x1=point_x;rect_y1=point_y;如果(count_touch==1){orignalRect=new RectF(rect_x1-30, rect_y1-30, rect_x1+30, rect_y1+30);无效();}休息;}返回 super.onTouchEvent(event);}}

解决方案

您可能需要查看 Matrix.mapRect.使用此方法将矩形变换为与 imageview 中的图像相同的量.

boolean onTouch(MotionEvent ev) {……//这个矩形尺寸应该是初始值并且应该是一个成员.mOriginalRect = new RectF(rect_x1-30, rect_y1-30, rect_x1+30, rect_y1+30);......}@覆盖受保护的无效onDraw(帆布画布){……mCanvasMatrix = canvas.getMatrix();///矩阵应该有比例值..mCanvasMatrix.mapRect(tempRect, mOriginalRect);//mOriginalRect 是 srccanvas.drawRect(tempRect, myPaint);//绘制 tempRect..……}

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

这篇关于在ImageView上绘制矩形以突出显示可以在android中放大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在ImageView上绘制矩形以突出显示可以在android中放大

基础教程推荐