Android ImageView Zoom-in and Zoom-Out

前端 未结 23 1890
死守一世寂寞
死守一世寂寞 2020-11-22 09:31

I want to Zoom-in and Zoom-out an Android ImageView. I tried most of the samples but in all of them the image in the ImageView itself is getting Zoomed-in and Zoomed-out, wh

相关标签:
23条回答
  • 2020-11-22 10:04

    The other implementations here all have some kind of a flaw. so i basically mixed them up and came up with this.

    Create a custom view like this:

    ZoomableImageView.java:

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Matrix;
    import android.graphics.PointF;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.ScaleGestureDetector;
    import android.view.View;
    import android.widget.ImageView;
    
    public class ZoomableImageView extends ImageView
    {
        Matrix matrix = new Matrix();
    
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        static final int CLICK = 3;
        int mode = NONE;
    
        PointF last = new PointF();
        PointF start = new PointF();
        float minScale = 1f;
        float maxScale = 4f;
        float[] m;
    
        float redundantXSpace, redundantYSpace;
        float width, height;
        float saveScale = 1f;
        float right, bottom, origWidth, origHeight, bmWidth, bmHeight;
    
        ScaleGestureDetector mScaleDetector;
        Context context;
    
        public ZoomableImageView(Context context, AttributeSet attr)
        {
            super(context, attr);
            super.setClickable(true);
            this.context = context;
            mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
            matrix.setTranslate(1f, 1f);
            m = new float[9];
            setImageMatrix(matrix);
            setScaleType(ScaleType.MATRIX);
    
            setOnTouchListener(new OnTouchListener()
            {
    
                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    mScaleDetector.onTouchEvent(event);
    
                    matrix.getValues(m);
                    float x = m[Matrix.MTRANS_X];
                    float y = m[Matrix.MTRANS_Y];
                    PointF curr = new PointF(event.getX(), event.getY());
    
                    switch (event.getAction())
                    {
                        //when one finger is touching
                        //set the mode to DRAG
                        case MotionEvent.ACTION_DOWN:
                            last.set(event.getX(), event.getY());
                            start.set(last);
                            mode = DRAG;
                            break;
                        //when two fingers are touching
                        //set the mode to ZOOM
                        case MotionEvent.ACTION_POINTER_DOWN:
                            last.set(event.getX(), event.getY());
                            start.set(last);
                            mode = ZOOM;
                            break;
                        //when a finger moves
                        //If mode is applicable move image
                        case MotionEvent.ACTION_MOVE:
                            //if the mode is ZOOM or
                            //if the mode is DRAG and already zoomed
                            if (mode == ZOOM || (mode == DRAG && saveScale > minScale))
                            {
                                float deltaX = curr.x - last.x;// x difference
                                float deltaY = curr.y - last.y;// y difference
                                float scaleWidth = Math.round(origWidth * saveScale);// width after applying current scale
                                float scaleHeight = Math.round(origHeight * saveScale);// height after applying current scale
                                //if scaleWidth is smaller than the views width
                                //in other words if the image width fits in the view
                                //limit left and right movement
                                if (scaleWidth < width)
                                {
                                    deltaX = 0;
                                    if (y + deltaY > 0)
                                        deltaY = -y;
                                    else if (y + deltaY < -bottom)
                                        deltaY = -(y + bottom);
                                }
                                //if scaleHeight is smaller than the views height
                                //in other words if the image height fits in the view
                                //limit up and down movement
                                else if (scaleHeight < height)
                                {
                                    deltaY = 0;
                                    if (x + deltaX > 0)
                                        deltaX = -x;
                                    else if (x + deltaX < -right)
                                        deltaX = -(x + right);
                                }
                                //if the image doesnt fit in the width or height
                                //limit both up and down and left and right
                                else
                                {
                                    if (x + deltaX > 0)
                                        deltaX = -x;
                                    else if (x + deltaX < -right)
                                        deltaX = -(x + right);
    
                                    if (y + deltaY > 0)
                                        deltaY = -y;
                                    else if (y + deltaY < -bottom)
                                        deltaY = -(y + bottom);
                                }
                                //move the image with the matrix
                                matrix.postTranslate(deltaX, deltaY);
                                //set the last touch location to the current
                                last.set(curr.x, curr.y);
                            }
                            break;
                        //first finger is lifted
                        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;
                        // second finger is lifted
                        case MotionEvent.ACTION_POINTER_UP:
                            mode = NONE;
                            break;
                    }
                    setImageMatrix(matrix);
                    invalidate();
                    return true;
                }
    
            });
        }
    
        @Override
        public void setImageBitmap(Bitmap bm)
        {
            super.setImageBitmap(bm);
            bmWidth = bm.getWidth();
            bmHeight = bm.getHeight();
        }
    
        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;
                }
                right = width * saveScale - width - (2 * redundantXSpace * saveScale);
                bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
                if (origWidth * saveScale <= width || origHeight * saveScale <= height)
                {
                    matrix.postScale(mScaleFactor, mScaleFactor, width / 2, height / 2);
                    if (mScaleFactor < 1)
                    {
                        matrix.getValues(m);
                        float x = m[Matrix.MTRANS_X];
                        float y = m[Matrix.MTRANS_Y];
                        if (mScaleFactor < 1)
                        {
                            if (Math.round(origWidth * saveScale) < width)
                            {
                                if (y < -bottom)
                                    matrix.postTranslate(0, -(y + bottom));
                                else if (y > 0)
                                    matrix.postTranslate(0, -y);
                            }
                            else
                            {
                                if (x < -right)
                                    matrix.postTranslate(-(x + right), 0);
                                else if (x > 0)
                                    matrix.postTranslate(-x, 0);
                            }
                        }
                    }
                }
                else
                {
                    matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
                    matrix.getValues(m);
                    float x = m[Matrix.MTRANS_X];
                    float y = m[Matrix.MTRANS_Y];
                    if (mScaleFactor < 1) {
                        if (x < -right)
                            matrix.postTranslate(-(x + right), 0);
                        else if (x > 0)
                            matrix.postTranslate(-x, 0);
                        if (y < -bottom)
                            matrix.postTranslate(0, -(y + bottom));
                        else if (y > 0)
                            matrix.postTranslate(0, -y);
                    }
                }
                return true;
            }
        }
    
        @Override
        protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);
            //Fit to screen.
            float scale;
            float scaleX =  width / bmWidth;
            float scaleY = height / bmHeight;
            scale = Math.min(scaleX, scaleY);
            matrix.setScale(scale, scale);
            setImageMatrix(matrix);
            saveScale = 1f;
    
            // Center the image
            redundantYSpace = height - (scale * bmHeight) ;
            redundantXSpace = width - (scale * bmWidth);
            redundantYSpace /= 2;
            redundantXSpace /= 2;
    
            matrix.postTranslate(redundantXSpace, redundantYSpace);
    
            origWidth = width - 2 * redundantXSpace;
            origHeight = height - 2 * redundantYSpace;
            right = width * saveScale - width - (2 * redundantXSpace * saveScale);
            bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
            setImageMatrix(matrix);
        }
    }
    

    Then add the image like this:

    ZoomableImageView touch = (ZoomableImageView)findViewById(R.id.IMAGEID);
    touch.setImageBitmap(bitmap);
    

    Add the view like this in XML:

    <PACKAGE.ZoomableImageView
    android:id="@+id/IMAGEID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
    
    0 讨论(0)
  • 2020-11-22 10:05

    I made my own custom imageview with pinch to zoom. There is no limits/borders on Chirag Ravals code, so user can drag the image off the screen.

    Here is the CustomImageView class:

        public class CustomImageVIew extends ImageView implements OnTouchListener {
    
    
        private Matrix matrix = new Matrix();
        private Matrix savedMatrix = new Matrix();
    
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
    
        private int mode = NONE;
    
        private PointF mStartPoint = new PointF();
        private PointF mMiddlePoint = new PointF();
        private Point mBitmapMiddlePoint = new Point();
    
        private float oldDist = 1f;
        private float matrixValues[] = {0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f};
        private float scale;
        private float oldEventX = 0;
        private float oldEventY = 0;
        private float oldStartPointX = 0;
        private float oldStartPointY = 0;
        private int mViewWidth = -1;
        private int mViewHeight = -1;
        private int mBitmapWidth = -1;
        private int mBitmapHeight = -1;
        private boolean mDraggable = false;
    
    
        public CustomImageVIew(Context context) {
            this(context, null, 0);
        }
    
        public CustomImageVIew(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CustomImageVIew(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.setOnTouchListener(this);
        }
    
        @Override
        public void onSizeChanged (int w, int h, int oldw, int oldh){
            super.onSizeChanged(w, h, oldw, oldh);
            mViewWidth = w;
            mViewHeight = h;
        }
    
        public void setBitmap(Bitmap bitmap){
            if(bitmap != null){
                setImageBitmap(bitmap);
    
                mBitmapWidth = bitmap.getWidth();
                mBitmapHeight = bitmap.getHeight();
                mBitmapMiddlePoint.x = (mViewWidth / 2) - (mBitmapWidth /  2);
                mBitmapMiddlePoint.y = (mViewHeight / 2) - (mBitmapHeight / 2);
    
                matrix.postTranslate(mBitmapMiddlePoint.x, mBitmapMiddlePoint.y);
                this.setImageMatrix(matrix);
            }
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event){
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                mStartPoint.set(event.getX(), event.getY());
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if(oldDist > 10f){
                    savedMatrix.set(matrix);
                    midPoint(mMiddlePoint, event);
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if(mode == DRAG){
                    drag(event);
                } else if(mode == ZOOM){
                    zoom(event);
                } 
                break;
            }
    
            return true;
        }
    
    
    
       public void drag(MotionEvent event){
           matrix.getValues(matrixValues);
    
           float left = matrixValues[2];
           float top = matrixValues[5];
           float bottom = (top + (matrixValues[0] * mBitmapHeight)) - mViewHeight;
           float right = (left + (matrixValues[0] * mBitmapWidth)) -mViewWidth;
    
           float eventX = event.getX();
           float eventY = event.getY();
           float spacingX = eventX - mStartPoint.x;
           float spacingY = eventY - mStartPoint.y;
           float newPositionLeft = (left  < 0 ? spacingX : spacingX * -1) + left;
           float newPositionRight = (spacingX) + right;
           float newPositionTop = (top  < 0 ? spacingY : spacingY * -1) + top;
           float newPositionBottom = (spacingY) + bottom;
           boolean x = true;
           boolean y = true;
    
           if(newPositionRight < 0.0f || newPositionLeft > 0.0f){
               if(newPositionRight < 0.0f && newPositionLeft > 0.0f){
                   x = false;
               } else{
                   eventX = oldEventX;
                   mStartPoint.x = oldStartPointX;
               }
           }
           if(newPositionBottom < 0.0f || newPositionTop > 0.0f){
               if(newPositionBottom < 0.0f && newPositionTop > 0.0f){
                   y = false;
               } else{
                   eventY = oldEventY;
                   mStartPoint.y = oldStartPointY;
               }
           }
    
           if(mDraggable){
               matrix.set(savedMatrix);
               matrix.postTranslate(x? eventX - mStartPoint.x : 0, y? eventY - mStartPoint.y : 0);
               this.setImageMatrix(matrix);
               if(x)oldEventX = eventX;
               if(y)oldEventY = eventY;
               if(x)oldStartPointX = mStartPoint.x;
               if(y)oldStartPointY = mStartPoint.y;
           }
    
       }
    
       public void zoom(MotionEvent event){
           matrix.getValues(matrixValues);
    
           float newDist = spacing(event);
           float bitmapWidth = matrixValues[0] * mBitmapWidth;
           float bimtapHeight = matrixValues[0] * mBitmapHeight;
           boolean in = newDist > oldDist;
    
           if(!in && matrixValues[0] < 1){
               return;
           }
           if(bitmapWidth > mViewWidth || bimtapHeight > mViewHeight){
               mDraggable = true;
           } else{
               mDraggable = false;
           }
    
           float midX = (mViewWidth / 2);
           float midY = (mViewHeight / 2);
    
           matrix.set(savedMatrix);
           scale = newDist / oldDist;
           matrix.postScale(scale, scale, bitmapWidth > mViewWidth ? mMiddlePoint.x : midX, bimtapHeight > mViewHeight ? mMiddlePoint.y : midY); 
    
           this.setImageMatrix(matrix);
    
    
       }
    
    
    
    
    
        /** Determine the space between the first two fingers */
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
    
            return (float)Math.sqrt(x * x + y * y);
        }
    
        /** Calculate the mid point of the first two fingers */
        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }
    
    
    }
    

    This is how you can use it in your activity:

    CustomImageVIew mImageView = (CustomImageVIew)findViewById(R.id.customImageVIew1);
    mImage.setBitmap(your bitmap);
    

    And layout:

    <your.package.name.CustomImageVIew
            android:id="@+id/customImageVIew1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:scaleType="matrix"/> // important
    
    0 讨论(0)
  • 2020-11-22 10:05

    I got the most helpful answer from @Nicolas Tyler but I had issues with how the syntax and logic worked. I also did not want any Alpha space and my Algebra was rusty! After 3 days I have put together my own version of this.

    My answer differs from @Nicolas Tyler with the following:

    1. Different variable names which I found made more sense in underlying contextual uses

    2. This Pinch-Zoom image class does NOT show alpha space and will let you zoom in and out and still not let you over/under pan an image and reveal alpha space

    3. Added deep comments to the matrix section to explain what was going on with the math involved

    4. This image class will also let you pass in a resourceId and it will create a bitmap from it

    5. Much simpler algorithms for both scaling and translation and few variables

    6. Changing the image within this will cause it to zoom in/out such that the new image takes up the view container

    A great resource refresher on Algebra can be found here: https://youtu.be/IiXB6tYtY4w?t=4m12s That video covers the core of Scalar and Translation matrixes (and will help you make sense of the MTRANS_X and MTRANS_Y stuff). If you have questions ask and I will do my best to answer (but I am NO Algebra expert).

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.graphics.PointF;
    import android.view.MotionEvent;
    import android.view.ScaleGestureDetector;
    import android.view.View;
    import android.widget.ImageView;
    
    public class iImage extends ImageView
    {
        static final int NONE_MODE = 0;
        static final int DRAG_MODE = 1;
        static final int ZOOM_MODE = 2;
        int _mode = NONE_MODE;
    
        Matrix _matrix = new Matrix();
        PointF _previousPoint = new PointF();
        PointF _startPoint = new PointF();
        float _currentScale = 1f;
        float _minScale = 1f;
        float _maxScale = 3f;
        float[] _arrayOf9Floats;
        float _bitmapWidth, _bitmapHeight,_displayWidth, _displayHeight;
        ScaleGestureDetector _scaleDetector;
        Context _context;
    
        public iImage(Context context)
        {
            super(context);
            super.setClickable(true);
            _context = context;
            _scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
            _arrayOf9Floats = new float[9];
            setScaleType(ScaleType.MATRIX);
            setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return handleTouch(v, event);
                }
            });
        }
    
        private boolean handleTouch(View v, MotionEvent event)
        {
            _scaleDetector.onTouchEvent(event);
            //Contrary to how this line looks, the matrix is not setting the values from the arrayOf9Floats, but rather taking the
            //matrix values and assigning them into the arrayOf9Floats. I extremely dislike this syntax and I think
            //it should have been written as _arrayOf9Floats = _matrix.getValues() but that's Android for you!!!
            _matrix.getValues(_arrayOf9Floats);
    
            //Look at https://youtu.be/IiXB6tYtY4w?t=4m12s , it shows scale, rotate, and translate matrices
            //If you look at the translate matrix, you'll see that the 3rd and 6th values are the values which represent x and y translations respectively
            //this corresponds to the 2nd and 5th values in the array and hence why the MTRANS_X and MTRANS_Y have the constants 2 and 5 respectively
            float xTranslate = _arrayOf9Floats[Matrix.MTRANS_X];
            float yTranslate = _arrayOf9Floats[Matrix.MTRANS_Y];
            PointF currentEventPoint = new PointF(event.getX(), event.getY());
            switch (event.getAction())
            {
                //First finger down only
                case MotionEvent.ACTION_DOWN:
                    _previousPoint.set(event.getX(), event.getY());
                    _startPoint.set(_previousPoint);
                    _mode = DRAG_MODE;
                    break;
                //Second finger down
                case MotionEvent.ACTION_POINTER_DOWN:
                    _previousPoint.set(event.getX(), event.getY());
                    _startPoint.set(_previousPoint);
                    _mode = ZOOM_MODE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (_mode == ZOOM_MODE || _mode == DRAG_MODE )
                    {
                        float deltaX = currentEventPoint.x - _previousPoint.x;
                        float deltaY = currentEventPoint.y - _previousPoint.y;
                        //In matrix terms, going right is + and going left is +
                        //Moving the image right past 0 means it will show alpha space on the left so we dont want that
                        //Keep in mind this is a TOP LEFT pivot point, so we dont want the top left to be past 0 lest we have alpha space
                        if(xTranslate + deltaX > 0)
                        {
                            //get absolute of how much into the negative we would have gone
                            float excessDeltaX = Math.abs(xTranslate + deltaX);
                            //take that excess away from deltaX so X wont got less than 0 after the translation
                            deltaX = deltaX - excessDeltaX;
                        }
    
                        //Going left we dont want the negative value to be less than the negative width of the sprite, lest we get alpha space on the right
                        //The width is going to be the width of the bitmap * scale and we want the - of it because we are checking for left movement
                        //We also need to account for the width of the DISPLAY CONTAINER (i.e. _displayWidth) so that gets subtracted
                        //i.e. we want the max scroll width value
                        float maxScrollableWidth = _bitmapWidth * _currentScale - _displayWidth;
                        if(xTranslate + deltaX < -maxScrollableWidth)
                        {
                            //this forces the max possible translate to always match the - of maxScrollableWidth
                            deltaX = -maxScrollableWidth - xTranslate;
                        }
    
                        //repeat for Y
                        if(yTranslate + deltaY > 0)
                        {
                            float excessDeltaY = Math.abs(yTranslate + deltaY);
                            deltaY = deltaY - excessDeltaY;
                        }
    
                        float maxScrollableHeight = _bitmapHeight * _currentScale - _displayWidth;
                        if(yTranslate + deltaY < -maxScrollableHeight)
                        {
                            //this forces the max possible translate to always match the - of maxScrollableWidth
                            deltaY = -maxScrollableHeight - yTranslate;
                        }
    
                        _matrix.postTranslate(deltaX, deltaY);
                        _matrix.getValues(_arrayOf9Floats);
                        //System.out.println(_matrix);
    
                        _previousPoint.set(currentEventPoint.x, currentEventPoint.y);
                    }
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    _mode = NONE_MODE;
                    break;
            }
            setImageMatrix(_matrix);
            invalidate();
            return true;
        }
    
        @Override
        public void setImageBitmap(Bitmap bm)
        {
            super.setImageBitmap(bm);
            _bitmapWidth = bm.getWidth();
            _bitmapHeight = bm.getHeight();
            invalidate();
        }
    
        @Override
        public void setImageResource(int resid)
        {
            Bitmap bitmapImage = BitmapFactory.decodeResource(_context.getResources(), resid);
            setImageBitmap(bitmapImage);
        }
    
        private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
        {
    
            @Override
            public boolean onScaleBegin(ScaleGestureDetector detector)
            {
                _mode = ZOOM_MODE;
                return true;
            }
    
            @Override
            public boolean onScale(ScaleGestureDetector detector)
            {
                float scaleFactor = detector.getScaleFactor();
                float originalScale = _currentScale;
                _currentScale *= scaleFactor;
                //Zoom in too much
                if (_currentScale > _maxScale) {
                    _currentScale = _maxScale;
                    scaleFactor = _maxScale / originalScale;
                }//Zoom out too much
                else if (_currentScale < _minScale) {
                    _currentScale = _minScale;
                    scaleFactor = _minScale / originalScale;
                }
               _matrix.postScale(scaleFactor,scaleFactor);
    
                return true;
            }
        }
    
        @Override
        protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            _displayWidth = MeasureSpec.getSize(widthMeasureSpec);
            _displayHeight = MeasureSpec.getSize(heightMeasureSpec);
            adjustScale();
        }
    
        private void adjustScale()
        {
            //Fit to display bounds with NO alpha space
            float scale;
            float scaleX =  _displayWidth / _bitmapWidth;
            float scaleY = _displayHeight / _bitmapHeight;
            scale = Math.max(scaleX, scaleY);
            _matrix.setScale(scale, scale);
            setImageMatrix(_matrix);
            _currentScale = scale;
            _minScale = scale;
        }
    
        public void setMaxZoom(float maxZoom){_maxScale = maxZoom;}
        public void setMinZoom(float minZoom) {_minScale = minZoom;}
    }
    
    0 讨论(0)
  • 2020-11-22 10:08

    Simple way:

    PhotoViewAttacher pAttacher;
    pAttacher = new PhotoViewAttacher(Your_Image_View);
    pAttacher.update();
    

    Add below line in build.gradle:

    compile 'com.commit451:PhotoView:1.2.4'
    
    0 讨论(0)
  • 2020-11-22 10:09

    Try the following:

    package com.example.nwssugeoinformationmobileapplication;
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.FloatMath;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View.OnTouchListener;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;
    import android.graphics.Matrix;
    import android.graphics.PointF;
    import android.graphics.RectF;
    import android.graphics.drawable.Drawable;
    import android.view.View;
    import android.widget.ImageView;
    public class MainActivity extends Activity implements  OnTouchListener {
    
        private static final String TAG = "Touch";
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        int mode = NONE;
        PointF start = new PointF();
        PointF mid = new PointF();
        float oldDist = 1f;
    
        private ImageView view;
        private float[] matrixValues = new float[9];
        private float maxZoom;
        private float minZoom;
        private float height;
        private float width;
        private RectF viewRect;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TabHost th = (TabHost) findViewById (R.id.tabhost);
            th.setup();
    
            TabSpec specs = th.newTabSpec("tag1");
            specs.setContent(R.id.tab1);
            specs.setIndicator("Map");
            th.addTab(specs);
    
    
            specs = th.newTabSpec("tag2");
            specs.setContent(R.id.tab2);
            specs.setIndicator("Search");
            th.addTab(specs);
    
    
            view = (ImageView) findViewById(R.id.imageView1);
            Drawable bitmap = getResources().getDrawable(R.drawable.map);
            view.setImageDrawable(bitmap);
            view.setOnTouchListener(this); 
    
            matrix.setTranslate(1f, 1f);
            view.setImageMatrix(matrix);
    
    
    
        } 
    
    
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
        init();
                }
        }
        private void init() {
            maxZoom = 2;
            minZoom = 1f;
            height = view.getDrawable().getIntrinsicHeight();
            width = view.getDrawable().getIntrinsicWidth();
            viewRect = new RectF(0, 0, view.getWidth(), view.getHeight());
            }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menus, menu);
            return true; 
            }
    
        public boolean onOptionsItemSelected(MenuItem item) {
    
            if(item.getItemId()== R.id.item1){
                Log.d("Tracks", "Track Us was Clicked");
    
                startActivity(new Intent (MainActivity.this, Tracklocation.class ));
                }
    
            if(item.getItemId()== R.id.item2){
                Log.d("Updates", "Updates was Clicked");
    
                startActivity(new Intent (MainActivity.this, Updates.class ));
                }
    
            if(item.getItemId()== R.id.item3){
                Log.d("About Us", "About Us was Clicked");
    
    
                startActivity(new Intent (MainActivity.this, Horoscope.class ));
                }
    
    
            return super.onOptionsItemSelected(item);
    
         }
    
    
        @Override
        public boolean onTouch(View v, MotionEvent rawEvent) {
            ImageView view = (ImageView) v;
            view.setScaleType(ImageView.ScaleType.MATRIX);
            dumpEvent(rawEvent);
        // Handle touch events here...
        switch (rawEvent.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        savedMatrix.set(matrix);
        start.set(rawEvent.getX(), rawEvent.getY());
        Log.d(TAG, "mode=DRAG");
        mode = DRAG;
        break;
        case MotionEvent.ACTION_POINTER_DOWN:
        oldDist = spacing(rawEvent);
        Log.d(TAG, "oldDist=" + oldDist);
        if (oldDist > 10f) {
        savedMatrix.set(matrix);
        midPoint(mid, rawEvent);
        mode = ZOOM;
        Log.d(TAG, "mode=ZOOM");
        }
        break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        Log.d(TAG, "mode=NONE");
        break;
        case MotionEvent.ACTION_MOVE:
        if (mode == DRAG) {
        matrix.set(savedMatrix);
        // limit pan
        matrix.getValues(matrixValues);
        float currentY = matrixValues[Matrix.MTRANS_Y];
        float currentX = matrixValues[Matrix.MTRANS_X];
        float currentScale = matrixValues[Matrix.MSCALE_X];
        float currentHeight = height * currentScale;
        float currentWidth = width * currentScale;
        float dx = rawEvent.getX() - start.x;
        float dy = rawEvent.getY() - start.y;
        float newX = currentX+dx;
        float newY = currentY+dy;
        RectF drawingRect = new RectF(newX, newY, newX+currentWidth, newY+currentHeight);
        float diffUp = Math.min(viewRect.bottom-drawingRect.bottom, viewRect.top-drawingRect.top);
        float diffDown = Math.max(viewRect.bottom-drawingRect.bottom, viewRect.top-drawingRect.top);
        float diffLeft = Math.min(viewRect.left-drawingRect.left, viewRect.right-drawingRect.right);
        float diffRight = Math.max(viewRect.left-drawingRect.left, viewRect.right-drawingRect.right);
    
        if(diffUp > 0 ){
        dy +=diffUp;
        }
        if(diffDown < 0){
        dy +=diffDown;
        }
        if( diffLeft> 0){
        dx += diffLeft;
        }
        if(diffRight < 0){
        dx += diffRight;
        }
        matrix.postTranslate(dx, dy);
        } else if (mode == ZOOM) {
        float newDist = spacing(rawEvent);
        Log.d(TAG, "newDist=" + newDist);
        if (newDist > 10f) {
        matrix.set(savedMatrix);
        float scale1 = newDist / oldDist;
        matrix.getValues(matrixValues);
        float currentScale = matrixValues[Matrix.MSCALE_X];
        // limit zoom
        if (scale1 * currentScale > maxZoom) {
        scale1 = maxZoom / currentScale;
        } else if (scale1 * currentScale < minZoom) {
        scale1 = minZoom / currentScale;
        }
        matrix.postScale(scale1, scale1, mid.x, mid.y);
        }
        }
        break;
        }
        view.setImageMatrix(matrix);
        return true; 
        }
    
        @SuppressWarnings("deprecation")
        private void dumpEvent(MotionEvent event) {
            String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
            "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
            StringBuilder sb = new StringBuilder();
            int action = event.getAction();
            int actionCode = action & MotionEvent.ACTION_MASK;
            sb.append("event ACTION_").append(names[actionCode]);
            if (actionCode == MotionEvent.ACTION_POINTER_DOWN
            || actionCode == MotionEvent.ACTION_POINTER_UP) {
            sb.append("(pid ").append(
            action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
            sb.append(")");
            }
            sb.append("[");
            for (int i = 0; i < event.getPointerCount(); i++) {
            sb.append("#").append(i);
            sb.append("(pid ").append(event.getPointerId(i));
            sb.append(")=").append((int) event.getX(i));
            sb.append(",").append((int) event.getY(i));
            if (i + 1 < event.getPointerCount())
            sb.append(";");
            }
            sb.append("]");
            Log.d(TAG, sb.toString());
            }
            /** Determine the space between the first two fingers */
            private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
            }
            /** Calculate the mid point of the first two fingers */
            @SuppressLint("FloatMath")
            private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
            }
    }
    
    0 讨论(0)
  • 2020-11-22 10:09

    I needed something similar, but needed the ability to get the dimensions easily and also drag/drop. I based this off of the answer @Nicolas Tyler gave and modified it from there.

    The features are pinch zoom in/out, long press to vibration/highlighted drag/drop.

    To use it add this CustomZoomView class to your project.

    public class CustomZoomView extends View implements View.OnTouchListener, View.OnLongClickListener{
    
    
    private Paint mPaint;
    
    Vibrator v;
    
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    static final int MOVE = 3;
    
    private int mode = NONE;
    
    Rect src;
    Rect mTempDst = new Rect();
    Rect dst = new Rect();
    
    Bitmap mBitmap;
    
    private int mBitmapWidth = -1;
    private int mBitmapHeight = -1;
    
    private PointF mStartPoint = new PointF();
    private PointF mMiddlePoint = new PointF();
    private PointF mStartDragPoint = new PointF();
    private PointF mMovePoint = new PointF();
    
    private float oldDist = 1f;
    private float scale;
    private float oldEventX = 0;
    private float oldEventY = 0;
    private float oldStartPointX = 0;
    private float oldStartPointY = 0;
    private int mViewWidth = -1;
    private int mViewHeight = -1;
    
    private boolean mDraggable = false;
    
    
    public CustomZoomView(Context context) {
        this(context, null, 0);
    
    }
    
    public CustomZoomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public CustomZoomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setOnTouchListener(this);
        this.setOnLongClickListener(this);
        v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        mPaint = new Paint();
        mPaint.setColorFilter(new PorterDuffColorFilter(Color.argb(100,255,255,255), PorterDuff.Mode.SRC_IN));
    }
    
    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = w;
        mViewHeight = h;
    }
    
    public void setBitmap(Bitmap bitmap) {
        if (bitmap != null) {
    
            src = new Rect();
            src.left = 0;
            src.top = 0;
            src.right = bitmap.getWidth();
            src.bottom = bitmap.getHeight();
            mBitmap = bitmap;
    
            mBitmapWidth = bitmap.getWidth() * 1;
            mBitmapHeight = bitmap.getHeight() * 1;
    
            dst = new Rect();
            dst.left = (mViewWidth / 2) - (mBitmapWidth / 2);
            dst.top = (mViewHeight / 2) - (mBitmapHeight / 2);
            dst.right = (mViewWidth / 2) + (mBitmapWidth / 2);
            dst.bottom = (mViewHeight / 2) + (mBitmapHeight / 2);
    
        }
    }
    
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mStartPoint.set(event.getX(), event.getY());
                mStartDragPoint.set(event.getX(), event.getY());
                mTempDst.set(dst.left, dst.top, dst.right, dst.bottom);
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    midPoint(mMiddlePoint, event);
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                if (mode == ZOOM) {
                    mBitmapWidth = dst.right - dst.left;
                    mBitmapHeight = dst.bottom - dst.top;
                }
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    mMovePoint.x = event.getX();
                    mMovePoint.y = event.getY();
                    drag(event);
                } else if (mode == ZOOM) {
                    zoom(event);
                } else if (mode == MOVE) {
                    move(event);
                }
                break;
        }
    
        return false;
    }
    
    public void move(MotionEvent event) {
    
        int xChange = (int) (event.getX() - mStartPoint.x);
        int yChange = (int) (event.getY() - mStartPoint.y);
    
        dst.left = mTempDst.left + (xChange);
        dst.top = mTempDst.top + (yChange);
    
        dst.right = mTempDst.right + (xChange);
        dst.bottom = mTempDst.bottom + (yChange);
    
        invalidate();
    }
    
    
    public void drag(MotionEvent event) {
    
        float eventX = event.getX();
        float eventY = event.getY();
        float spacingX = eventX - mStartDragPoint.x;
        float spacingY = eventY - mStartDragPoint.y;
        float newPositionLeft = (dst.left < 0 ? spacingX : spacingX * -1) + dst.left;
        float newPositionRight = (spacingX) + dst.right;
        float newPositionTop = (dst.top < 0 ? spacingY : spacingY * -1) + dst.top;
        float newPositionBottom = (spacingY) + dst.bottom;
        boolean x = true;
        boolean y = true;
    
        if (newPositionRight < 0.0f || newPositionLeft > 0.0f) {
            if (newPositionRight < 0.0f && newPositionLeft > 0.0f) {
                x = false;
            } else {
                eventX = oldEventX;
                mStartDragPoint.x = oldStartPointX;
            }
        }
        if (newPositionBottom < 0.0f || newPositionTop > 0.0f) {
            if (newPositionBottom < 0.0f && newPositionTop > 0.0f) {
                y = false;
            } else {
                eventY = oldEventY;
                mStartDragPoint.y = oldStartPointY;
            }
        }
    
        if (mDraggable) {
            if (x) oldEventX = eventX;
            if (y) oldEventY = eventY;
            if (x) oldStartPointX = mStartDragPoint.x;
            if (y) oldStartPointY = mStartDragPoint.y;
        }
    
    }
    
    public void zoom(MotionEvent event) {
        float newDist = spacing(event);
        boolean in = newDist > oldDist;
    
        if (!in && scale < .01f) {
            return;
        }
    
        scale = newDist / oldDist;
    
        int xChange = (int) ((mBitmapWidth * scale) / 2);
        int yChange = (int) ((mBitmapHeight * scale) / 2);
    
        if (xChange > 10 && yChange > 10) { //ADDED THIS TO KEEP IT FROM GOING INVERSE
    
        int xMidPoint = ((dst.right - dst.left) / 2) + dst.left;
        int yMidPoint = ((dst.bottom - dst.top) / 2) + dst.top;
    
        dst.left = (int) (float) (xMidPoint - xChange);
        dst.top = (int) (float) (yMidPoint - yChange);
    
        dst.right = (int) (float) (xMidPoint + xChange);
        dst.bottom = (int) (float) (yMidPoint + yChange);
    
        }
    
        invalidate();
    
    }
    
    
    /**
     * Determine the space between the first two fingers
     */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
    
        return (float) Math.sqrt(x * x + y * y);
    }
    
    /**
     * Calculate the mid point of the first two fingers
     */
    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }
    
    
    @Override
    public boolean onLongClick(View view) {
    
    
        if (mode == DRAG) {
            if ((mStartPoint.x > dst.left && mStartPoint.x < dst.right) && (mStartPoint.y < dst.bottom && mStartPoint.y > dst.top)
                    && (mMovePoint.x > dst.left && mMovePoint.x < dst.right) && (mMovePoint.y < dst.bottom && mMovePoint.y > dst.top)) {
                mode = MOVE;
                v.vibrate(500);
            }
        }
        return true;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mode == MOVE) {
            canvas.drawBitmap(mBitmap, src, dst, null);
            canvas.drawBitmap(mBitmap, src, dst, mPaint);
        } else {
            canvas.drawBitmap(mBitmap, src, dst, null);
        }
    
    }
    }
    

    ...then add this to your activity

    CustomZoomView customImageView = (CustomZoomView) findViewById(R.id.customZoomView);
    customImageView.setBitmap(yourBitmap);
    

    ...and this in your view in xml.

    <your.package.name.CustomZoomView
       android:id="@+id/customZoomView"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:longClickable="true"/>
    

    ...and add this to your manifest

    <uses-permission android:name="android.permission.VIBRATE"/>
    
    0 讨论(0)
提交回复
热议问题