5

我有一个从ScrollView. 我想要做的是用这个类实现捏缩放,我想用它缩放到ScrollView的孩子并根据需要滚动缩放的内容。UIScrollView基本上,我想要与iOS 类具有相同的行为(这是一个耻辱,Android 不提供像 iOS 那样的任何内置缩放功能,我们必须从头开始编写所有内容)。我有一个ScaleGestureListener班级内部ScrollView,当我收到onScale事件时,我更新了内部子视图的布局尺寸:我将布局宽度和高度乘以我收到的比例因子ScaleGestureListener. 困扰我的是 ScrollView 不会根据其子项的新尺寸更新其滚动条。例如,如果 child 的宽度变得大于ScrollView's,它就不会显示水平滚动条。我在滚动视图及其子视图上都调用了invalidateand ,但它们失败了。requestLayout

下面我给出我的代码:

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 根据其子项的更新大小更新其滚动条,每次用户进行缩放时。

提前致谢

4

1 回答 1

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() 设置 x 和 y 的最小值而不是 0

于 2013-05-22T19:25:00.237 回答