0

I am trying to enable zoom in for the contents of Horizontal ScrollView. I created a custom class and handle events for zoom in. following is my code:

public class LockableScrollView extends HorizontalScrollView {

    private GestureDetector mGestureDetector;
    //private onDis mListener;
    Context context;

    public LockableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    // true if we can scroll the ScrollView
    // false if we cannot scroll
    private boolean scrollable = true;

    /**
     * This method used for setScrolling to ScrollView
     * 
     * @param is
     *            True in onInterceptTouchEvent returns true else false
     * 
     * */
    public void setScrollingEnabled(boolean scrollable) {
        this.scrollable = scrollable;
    }

    /**
     * Check is scrollview isScrollable or not
     * */
    public boolean isScrollable() {
        return scrollable;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // if we can scroll pass the event to the superclass
            if (scrollable)
                return super.onTouchEvent(ev);
            // only continue to handle the touch event if scrolling enabled
            return scrollable;
            // scrollable is always false at this point
        default:
            return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if
        // we are not scrollable
        switch (ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (scrollable) {
                // We're currently scrolling, so yes, intercept the
                // touch event!
                return super.dispatchTouchEvent(ev);
            }


        }
        return false;

    }
}

and in activity:

in oncreate
gestureDetector = new GestureDetector(FullImageView.this.getActivity(), new GestureListener());

        // animation for scalling
        mScaleDetector = new ScaleGestureDetector(FullImageView.this.getActivity(), new ScaleGestureDetector.SimpleOnScaleGestureListener() 
        {                                   
            @Override
            public boolean onScale(ScaleGestureDetector detector) 
            {
                float scale = 1 - detector.getScaleFactor();

                float prevScale = mScale;
                mScale += scale;

                if (mScale < 0.1f) // Minimum scale condition:
                    mScale = 0.1f;

                if (mScale > 10f) // Maximum scale condition:
                    mScale = 10f;
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
                scaleAnimation.setDuration(0);
                scaleAnimation.setFillAfter(true);
                //ScrollView layout =(ScrollView) findViewById(R.id.horizontalScrollView1);
                scrollView.startAnimation(scaleAnimation);
                return true;
            }
        });
        //zoom(4f,4f,new PointF(0,0));   
        //scrollView.setOnTouchListener(new MyScaleGestures(FullImageView.this.getActivity()));
        //scrollView.requestDisallowInterceptTouchEvent(true);
        scrollView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                mScaleDetector.onTouchEvent(event);
                gestureDetector.onTouchEvent(event);
                return gestureDetector.onTouchEvent(event);
            }
        });

    }


    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // double tap fired.
            return true;
        }
    }

But i am unable to get zoom in please help.

1 Answers1

0

You can try below links...

How to make zoomable scrollview?

http://www.zdnet.com/article/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/

Community
  • 1
  • 1
Anjali
  • 1,623
  • 5
  • 30
  • 50