使用ScrollView进行缩放缩放:当子大小更改时更新它的滚动行为

时间:2013-04-08 13:52:11

标签: android android-layout scrollview pinchzoom

我有一个自定义类,我从ScrollView扩展而来。我想要做的是用这个类实现缩放缩放,我想用它缩放到ScrollView的子节点并根据需要滚动缩放的内容。基本上,我想要iOS UIScrollView类具有的相同行为(它是 SHAME ,Android不提供任何内置的缩放功能,如iOS我们必须从头开始写一切。我的班级内有ScaleGestureListener,当我收到ScrollView个事件时,我会更新onScale内部子视图的布局维度:我将布局宽度和高度乘以我从ScaleGestureListener收到的比例因子。困扰我的是ScrollView不会根据其子项的新维度更新其滚动条。例如,如果孩子的宽度变得大于ScrollView,则不会显示水平滚动条。我在滚动视图及其子项上调用了invalidaterequestLayout,但它们都失败了。

在下面我给出了我的代码:

public class PinchZoomScrollView extends ScrollView{

private ScaleGestureDetector mScaleDetector;// = new ScaleGestureDetector(context, new ScaleListener());
private float mScaleFactor = 1.0f;
private float referenceTextSize;
private static final float MIN_ZOOM_AMOUNT = 1.0f;
private static final float MAX_ZOOM_AMOUNT = 2.0f;

private float refChildWidth;
private float refChildHeight;

private boolean shownForTheFirstTime = true;
//private View content;


private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    private WeakReference<PinchZoomScrollView> containerRef;

    public ScaleListener(PinchZoomScrollView container)
    {
        super();

        containerRef = new WeakReference<PinchZoomScrollView>(container);
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(MIN_ZOOM_AMOUNT, Math.min(mScaleFactor, MAX_ZOOM_AMOUNT));

        //Get Child object
        View content = containerRef.get().getChildAt(0);

        if(shownForTheFirstTime)
        {
            shownForTheFirstTime = false;

            refChildWidth  = content.getWidth();
            refChildHeight = content.getHeight();
        }

        LayoutParams params = new LayoutParams(Math.round(mScaleFactor*refChildWidth), Math.round(mScaleFactor*refChildHeight));
        content.setLayoutParams(params);


        System.out.println("this.width="+containerRef.get().getWidth());
        System.out.println("content.width="+Math.round(mScaleFactor*refChildWidth));


        content.invalidate();
        content.requestLayout();

        containerRef.get().updateViewLayout(content, params);

        containerRef.get().invalidate();
        containerRef.get().requestLayout();

        return true;
    }
}


public PinchZoomScrollView(Context context)
{
    super(context);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
}

public PinchZoomScrollView(Context context, AttributeSet attrs)
{
    super(context,attrs);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
}

public PinchZoomScrollView(Context context, AttributeSet attrs, int defStyle)
{
    super(context,attrs,defStyle);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
}

@Override
public boolean onTouchEvent(MotionEvent ev) {

    super.onTouchEvent(ev);

    mScaleDetector.onTouchEvent(ev);

    return true;
}

}

简而言之,我需要ScrollView根据孩子的更新大小更新其滚动条,每次用户进行缩放放大。

提前致谢

1 个答案:

答案 0 :(得分:4)

这是一个解决方案:

  1. 我没有扩展ScrollView,而是编写了一个扩展FrameLayout的TwoDimensionScrollView,它是ScrollView和Horizo​​ntalScrollView的组合,因此它可以显示水平/垂直滚动条,即使缩放了子项也是如此。我也没有使用手势检测器。相反,我实现了自己的缩放。

  2. 要更新滚动条,请重写以下函数:

    @Override
    protected int computeVerticalScrollRange() {
        final int count = getChildCount();
        final int contentHeight = getHeight() - getPaddingBottom()
            - getPaddingTop();
        if (count == 0) {
            return contentHeight;
        }
    
    
    @Override
    protected int computeVerticalScrollOffset() {
        int result = Math.max(0, super.computeVerticalScrollOffset());
        return result - getScrollVerticalMin();
    }
    @Override
    protected int computeHorizontalScrollRange() {
        final int count = getChildCount();
        final int contentWidth = getWidth() - getPaddingLeft()
            - getPaddingRight();
        if (count == 0) {
            return contentWidth;
        }
    
        int scrollRange = getChildAt(0).getRight();
        final int scrollX = getScrollX();
        final int overscrollRight = Math.max(0, scrollRange - contentWidth);
        if (scrollX < 0) {
            scrollRange -= scrollX;
        } else if (scrollX > overscrollRight) {
            scrollRange += scrollX - overscrollRight;
        }
        return (int) (scrollRange * mScaleFactor + getScrollHorizontalMin());
    }
    
    @Override
    protected int computeHorizontalScrollOffset() {
        int result = Math.max(0, super.computeHorizontalScrollOffset());
        return result - getScrollHorizontalMin();
    }
    @Override
    protected int computeVerticalScrollExtent() {
        return getHeight() + getScrollVerticalMin();
    }
    
    @Override
    protected int computeHorizontalScrollExtent() {
        return getWidth() + getScrollHorizontalMin();
    }
    private int getScrollHorizontalMin() {
        if (this.getChildCount() < 1)
            return 0;
        View child = this.getChildAt(0);
        return (int) ((1 - mScaleFactor) * child.getPivotX());
    }
    
    private int getScrollVerticalMin() {
        if (this.getChildCount() < 1)
            return 0;
        View child = this.getChildAt(0);
        return (int) ((1 - mScaleFactor) * child.getPivotY());
    }
    private int getScrollVerticalRange() {
        int scrollRange = 0;
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            scrollRange = (int) Math.max(0, child.getHeight() * mScaleFactor
                    - (getHeight() - getPaddingBottom() - getPaddingTop())
                    + getScrollVerticalMin());
        }
    return scrollRange;
    }
    
    private int getScrollHorizontalRange() {
        int scrollRange = 0;
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            scrollRange = (int) Math.max(0, child.getWidth() * mScaleFactor
                    - (getWidth() - getPaddingRight() - getPaddingLeft())
                    + getScrollHorizontalMin());
        }
        return scrollRange;
    }
    
  3. 您还需要找到所有调用SpringBack()和fling()的代码,然后使用上面的函数getScrollHorizo​​ntalMin()和getScrollVerticalMin()而不是0来设置x和y的最小值