使用水平scrollview放大在android中不起作用

时间:2015-07-22 07:31:44

标签: android scrollview pinchzoom

我正在尝试放大水平ScrollView的内容。我创建了一个自定义类并处理放大的事件。以下是我的代码:

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;

    }
}

和活动:

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

但我无法放大请帮忙。