Android ImageView Zoom-in and Zoom-Out

前端 未结 23 1910
死守一世寂寞
死守一世寂寞 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:15

    I am Using this one it is working perfectly.

    
    

    My a MyZoomableImageViewTouch class is below:

    public class MyZoomableImageViewTouch extends ImageViewTouch
    {
    
        static final float SCROLL_DELTA_THRESHOLD = 1.0f;
    
        public MyZoomableImageViewTouch(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
            init();
        }
    
        public MyZoomableImageViewTouch(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MyZoomableImageViewTouch(Context context)
        {
            super(context);
            init();
        }
    
        private void init() {
            View.OnTouchListener listener = new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (getScale() > 1f) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    } else {
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    return false;
                }
            };
            setOnTouchListener(listener);
            setDisplayType(DisplayType.FIT_TO_SCREEN);
        }
    
        @Override
        protected float onDoubleTapPost(float scale, float maxZoom) {
            if (scale != 1f) {
                mDoubleTapDirection = 1;
                return 1f;
            }
            if (mDoubleTapDirection == 1) {
                mDoubleTapDirection = -1;
                if ((scale + (mScaleFactor * 2)) <= maxZoom) {
                    return scale + mScaleFactor;
                } else {
                    mDoubleTapDirection = -1;
                    return maxZoom;
                }
            } else {
                mDoubleTapDirection = 1;
                return 1f;
            }
        }
    
        @Override
        public boolean canScroll(int direction) {
            RectF bitmapRect = getBitmapRect();
            updateRect(bitmapRect, mScrollRect);
            Rect imageViewRect = new Rect();
            getGlobalVisibleRect(imageViewRect);
    
            if (null == bitmapRect) {
                return false;
            }
    
            if (Math.abs(bitmapRect.right - imageViewRect.right) < SCROLL_DELTA_THRESHOLD) {
                if (direction < 0) {
                    return false;
                }
            }
    
            if (Math.abs(bitmapRect.left - mScrollRect.left) < SCROLL_DELTA_THRESHOLD) {
                if (direction > 0) {
                    return false;
                }
            }
            return true;
        }
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
            if (getScale() == 1f) return false;
            if (distanceX != 0 && !canScroll((int) -distanceX)) {
                getParent().requestDisallowInterceptTouchEvent(false);
                return false;
            } else {
                getParent().requestDisallowInterceptTouchEvent(true);
                mUserScaled = true;
                scrollBy(-distanceX, -distanceY);
                invalidate();
                return true;
            }
        }
    }
    

提交回复
热议问题